[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r27987 - msh/src
From: |
gnunet |
Subject: |
[GNUnet-SVN] r27987 - msh/src |
Date: |
Sat, 13 Jul 2013 11:33:50 +0200 |
Author: harsha
Date: 2013-07-13 11:33:50 +0200 (Sat, 13 Jul 2013)
New Revision: 27987
Added:
msh/src/test_addressmap.c
Modified:
msh/src/
msh/src/Makefile.am
msh/src/addressmap.c
msh/src/addressmap.h
Log:
- merging in addressmap
Index: msh/src
===================================================================
--- msh/src 2013-07-13 09:10:10 UTC (rev 27986)
+++ msh/src 2013-07-13 09:33:50 UTC (rev 27987)
Property changes on: msh/src
___________________________________________________________________
Modified: svn:ignore
## -6,3 +6,4 ##
test-suite.log
test-scheduler*
test-bitmap*
+test-addressmap*
Modified: msh/src/Makefile.am
===================================================================
--- msh/src/Makefile.am 2013-07-13 09:10:10 UTC (rev 27986)
+++ msh/src/Makefile.am 2013-07-13 09:33:50 UTC (rev 27987)
@@ -11,7 +11,8 @@
check_PROGRAMS = \
test-scheduler \
test-scheduler-socket \
- test-bitmap
+ test-bitmap \
+ test-addressmap
test_scheduler_SOURCES = test_scheduler.c scheduler.c scheduler.h common.h \
util.c util.h common.h
@@ -27,7 +28,10 @@
test_bitmap_SOURCES = test_bitmap.c bitmap.c bitmap.h
+test_addressmap_SOURCES = test_addressmap.c addressmap.c addressmap.h
+
TESTS = \
test-scheduler \
test-scheduler-socket \
- test-bitmap
+ test-bitmap \
+ test-addressmap
Modified: msh/src/addressmap.c
===================================================================
--- msh/src/addressmap.c 2013-07-13 09:10:10 UTC (rev 27986)
+++ msh/src/addressmap.c 2013-07-13 09:33:50 UTC (rev 27987)
@@ -14,8 +14,14 @@
*/
struct InstanceAddr
{
+ /**
+ * DLL pointer
+ */
struct InstanceAddr *next;
+ /**
+ * DLL pointer
+ */
struct InstanceAddr *prev;
/**
@@ -35,8 +41,14 @@
*/
struct InstanceAddrInfo
{
+ /**
+ * DLL head
+ */
struct InstanceAddr *addr_head;
+ /**
+ * DLL tail
+ */
struct InstanceAddr *addr_tail;
/**
@@ -51,6 +63,9 @@
};
+/**
+ * Get the 32bit IP addresses from an instance address
+ */
#define instance_address_ip(iaddr) \
((uint32_t) ((struct sockaddr_in *)iaddr->saddr)->sin_addr.s_addr)
@@ -198,29 +213,50 @@
*/
struct AddressMap
{
- struct InstanceAddrInfo **map;
+ /**
+ * the size of the above array
+ */
unsigned int size;
+
+ /**
+ * The array for address map
+ */
+ struct InstanceAddrInfo *map[0];
};
-struct AddressMap *
+/**
+ * Create an address map for mapping instances to IP addresses
+ *
+ * @param nproc number of instances
+ * @return address map
+ */
+AddressMap *
addressmap_create (unsigned int nproc)
{
struct AddressMap *m;
- m = MSH_malloc (sizeof (struct InstanceAddrInfo *) * nproc);
+ m = MSH_malloc (sizeof (struct AddressMap) + sizeof (struct InstanceAddrInfo
+ *) * nproc);
m->size = nproc;
return m;
}
+/**
+ * Add an address to the address map
+ *
+ * @param m the address map
+ * @param new the new instance address info object to add into the address map
+ */
void
-addressmap_add (struct AddressMap *m, struct InstanceAddrInfo *new)
+addressmap_add (AddressMap *m, struct InstanceAddrInfo *new)
{
struct InstanceAddrInfo *old;
struct InstanceAddr *old_ia;
struct InstanceAddr *new_ia;
+ struct InstanceAddr *iacpy;
unsigned int rank;
rank = new->rank;
@@ -234,22 +270,68 @@
old_ia = old->addr_head;
new_ia = new->addr_head;
while (NULL != new_ia)
- {
- while ((NULL != old_ia) && (instance_address_ip (new_ia) >=
- instance_address_ip (old_ia)))
+ {
+ while (NULL != old_ia)
{
+ if (instance_address_ip (new_ia) == instance_address_ip (old_ia))
+ goto next;
+ if (instance_address_ip (new_ia) < instance_address_ip (old_ia))
+ break;
old_ia = old_ia->next;
}
+ iacpy = MSH_malloc (sizeof (struct InstanceAddr) + new_ia->addrlen);
+ (void) memcpy (iacpy->saddr, new_ia->saddr, new_ia->addrlen);
+ iacpy->addrlen = new_ia->addrlen;
if (NULL == old_ia)
{
- DLL_insert_tail (old->addr_head, old->addr_tail, new_ia);
+ DLL_insert_tail (old->addr_head, old->addr_tail, iacpy);
goto next;
}
- DLL_insert_before (old->addr_head, old->addr_tail, old_ia, new_ia);
+ DLL_insert_before (old->addr_head, old->addr_tail, old_ia, iacpy);
- next:
+ next:
new_ia = new_ia->next;
}
}
+
+/**
+ * Function to iterate over address of an instance in a given address map
+ *
+ * @param m the address map
+ * @param rank the instance rank
+ * @param cb the iterator callback
+ * @param cls the closure for the callback
+ * @return MSH_OK if the iteration was successfully completed; MSH_SYSERR if
the
+ * iteration was aborted in the iteration callback; MSH_NO if no
+ * addresses were found in the address map
+ */
+int
+addressmap_iterate_instance_addresses (AddressMap *m, unsigned int rank,
+ instance_address_info_address_iterate_cb
+ cb, void *cls)
+{
+ struct InstanceAddrInfo *iainfo;
+
+ MSH_assert (rank < m->size);
+ iainfo = m->map[rank];
+ if (NULL == iainfo)
+ return MSH_NO;
+ return instance_address_info_iterate_addresses (iainfo, cb, cls);
+}
+
+
+/**
+ * Destroy the address map. Note that the instance address information object
is
+ * not destroyed
+ *
+ * @param m the address map to destroy
+ */
+void
+addressmap_destroy (AddressMap *m)
+{
+ free (m);
+}
+
+
Modified: msh/src/addressmap.h
===================================================================
--- msh/src/addressmap.h 2013-07-13 09:10:10 UTC (rev 27986)
+++ msh/src/addressmap.h 2013-07-13 09:33:50 UTC (rev 27987)
@@ -86,11 +86,49 @@
/**
+ * Iterate over all addresses in the given instance address info object
+ *
+ * @param iainfo the instance address info object
+ * @param cb the callback to call for each iterated address
+ * @param cls the closure for the above callback
+ * @return MSH_OK if the iteration completed successfully; MSH_SYSERR if the
+ * iteration was terminated
+ */
+int
+instance_address_info_iterate_addresses (struct InstanceAddrInfo *iainfo,
+
instance_address_info_address_iterate_cb
+ cb,
+ void *cls);
+
+
+/**
* Opaque handle for address map
*/
typedef struct AddressMap AddressMap;
+/**
+ * Create an address map for mapping instances to IP addresses
+ *
+ * @param nproc number of instances
+ * @return address map
+ */
+AddressMap *
+addressmap_create (unsigned int nproc);
+
+
+/**
+ * Add an address to the address map
+ *
+ * @param m the address map
+ * @param new the new instance address info object to add into the address map
+ */
+void
+addressmap_add (AddressMap *m, struct InstanceAddrInfo *new);
+
+
+
+
#endif /* ADDRESSMAP_H_ */
/* End of addressmap.h */
Added: msh/src/test_addressmap.c
===================================================================
--- msh/src/test_addressmap.c (rev 0)
+++ msh/src/test_addressmap.c 2013-07-13 09:33:50 UTC (rev 27987)
@@ -0,0 +1,74 @@
+/**
+ * @file test_addressmap.c
+ * @brief test case for address map implementation
+ * @author Sree Harsha Totakura <address@hidden>
+ */
+
+#include "common.h"
+#include "util.h"
+#include "addressmap.h"
+
+
+static unsigned int cnt;
+
+/**
+ * Callback for iterating all the instance addresses present in an instance
+ * address info object
+ *
+ * @param cls the closure passed to instance_address_info_iterate_addresses()
+ * @param addr the address of the instance
+ * @param addrlen the address length
+ * @return MSH_OK to continue iteration; MSH_SYSERR to terminate
+ */
+static int
+iterator_cb (void *cls, struct sockaddr *addr, socklen_t addrlen)
+{
+ cnt++;
+ return MSH_OK;
+}
+
+
+int
+main ()
+{
+ AddressMap *m;
+ struct InstanceAddr *addr1, *addr2, *addr3;
+ struct InstanceAddrInfo *iainfo, *iainfo2;
+
+ addr1 = instance_address_create_sockaddr_in (0, 12);
+ iainfo = instance_address_info_create (0);
+ instance_address_info_add_address (iainfo, addr1);
+ addr2 = instance_address_create_sockaddr_in (0, 9);
+ instance_address_info_add_address (iainfo, addr2);
+ addr3 = instance_address_create_sockaddr_in (0, 13);
+ instance_address_info_add_address (iainfo, addr3);
+ m = addressmap_create (2);
+ addressmap_add (m, iainfo);
+ MSH_assert (MSH_OK ==
+ instance_address_info_iterate_addresses (iainfo,
+ &iterator_cb, NULL));
+ MSH_assert (3 == cnt);
+ cnt = 0;
+ MSH_assert (MSH_OK ==
+ addressmap_iterate_instance_addresses (m, 0, &iterator_cb,
NULL));
+ MSH_assert (3 == cnt);
+ iainfo2 = instance_address_info_create (0);
+ addr1 = instance_address_create_sockaddr_in (0, 12);
+ instance_address_info_add_address (iainfo2, addr1);
+ addr2 = instance_address_create_sockaddr_in (0, 19);
+ instance_address_info_add_address (iainfo2, addr2);
+ cnt = 0;
+ MSH_assert (MSH_OK ==
+ instance_address_info_iterate_addresses (iainfo2,
+ &iterator_cb, NULL));
+ MSH_assert (2 == cnt);
+ addressmap_add (m, iainfo2);
+ cnt = 0;
+ MSH_assert (MSH_OK ==
+ addressmap_iterate_instance_addresses (m, 0, &iterator_cb,
NULL));
+ MSH_assert (4 == cnt);
+ addressmap_destroy (m);
+ instance_address_info_destroy (iainfo);
+ instance_address_info_destroy (iainfo2);
+ return 0;
+}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r27987 - msh/src,
gnunet <=