Logo AND Algorithmique Numérique Distribuée

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