Logo AND Algorithmique Numérique Distribuée

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