Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove usage of xbt_assert[0-9].
[simgrid.git] / src / xbt / xbt_peer.c
1 /* xbt_peer_t management functions                                          */
2
3 /* Copyright (c) 2006, 2007, 2008, 2009, 2010. 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 xbt_peer_t xbt_peer_copy(xbt_peer_t h)
25 {
26   return xbt_peer_new(h->name, h->port);
27 }
28
29 /** \brief constructor. Argument should be of form '(peername):(port)'. */
30 xbt_peer_t xbt_peer_from_string(const char *peerport)
31 {
32   xbt_peer_t res = xbt_new(s_xbt_peer_t, 1);
33   char *name = xbt_strdup(peerport);
34   char *port_str = strchr(name, ':');
35   xbt_assert(port_str,
36               "argument of xbt_peer_from_string should be of form <peername>:<port>, it's '%s'",
37               peerport);
38   *port_str = '\0';
39   port_str++;
40
41   res->name = xbt_strdup(name); /* it will be shorter now that we cut the port */
42   res->port = atoi(port_str);
43   free(name);
44   return res;
45 }
46
47 /** \brief destructor */
48 void xbt_peer_free(xbt_peer_t peer)
49 {
50   if (peer) {
51     if (peer->name)
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 }