Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
53e900453d3bf1ee812099fcdc35d8b25704d5b1
[simgrid.git] / src / xbt / xbt_peer.c
1 /* xbt_peer_t management functions                                          */
2
3 /* Copyright (c) 2006 Martin Quinson. All rights reserved.                  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10 #include "xbt/peer.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(peer, xbt, "peer management");
13
14 /** \brief constructor */
15 xbt_peer_t xbt_peer_new(const char *name, int port)
16 {
17   xbt_peer_t res = xbt_new(s_xbt_peer_t, 1);
18   res->name = xbt_strdup(name);
19   res->port = port;
20   return res;
21 }
22
23 xbt_peer_t xbt_peer_copy(xbt_peer_t h)
24 {
25   return xbt_peer_new(h->name, h->port);
26 }
27
28 /** \brief constructor. Argument should be of form '(peername):(port)'. */
29 xbt_peer_t xbt_peer_from_string(const char *peerport)
30 {
31   xbt_peer_t res = xbt_new(s_xbt_peer_t, 1);
32   char *name = xbt_strdup(peerport);
33   char *port_str = strchr(name, ':');
34   xbt_assert1(port_str,
35               "argument of xbt_peer_from_string should be of form <peername>:<port>, it's '%s'",
36               peerport);
37   *port_str = '\0';
38   port_str++;
39
40   res->name = xbt_strdup(name); /* it will be shorter now that we cut the port */
41   res->port = atoi(port_str);
42   free(name);
43   return res;
44 }
45
46 /** \brief destructor */
47 void xbt_peer_free(xbt_peer_t peer)
48 {
49   if (peer) {
50     if (peer->name)
51       free(peer->name);
52     free(peer);
53   }
54 }
55
56 /** \brief Freeing function for dynars of xbt_peer_t */
57 void xbt_peer_free_voidp(void *d)
58 {
59   xbt_peer_free((xbt_peer_t) * (void **) d);
60 }