Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
88eb6eab1781e037db299beea1f4f1f57d6634e9
[simgrid.git] / src / xbt / xbt_host.c
1 /* $Id$ */
2
3 /* xbt_host_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/host.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(host,xbt,"Host management");
15
16 /** \brief constructor */
17 xbt_host_t xbt_host_new(const char *name, int port)  {
18    xbt_host_t res=xbt_new(s_xbt_host_t, 1);
19    res->name = xbt_strdup(name);
20    res->port = port;
21    return res;
22 }
23
24 /** \brief constructor. Argument should be of form '<hostname>:<port>'. */
25 xbt_host_t xbt_host_from_string(const char *hostport)  {
26    xbt_host_t res=xbt_new(s_xbt_host_t, 1);
27    char *name=xbt_strdup(hostport);
28    char *port_str=strchr(hostport,':');
29    xbt_assert1(port_str,"argument of xbt_host_from_string should be of form <hostname>:<port>, it's '%s'", hostport);
30    *port_str='\0';
31    port_str++;
32    
33    res->name = xbt_strdup(name); /* it will be shorter now that we cut the port */
34    res->port = atoi(port_str);
35    free(name);
36    return res;
37 }
38
39 /** \brief destructor */
40 void xbt_host_free(xbt_host_t host) {
41    if (host) {
42       if (host->name) free(host->name);
43       free(host);
44    }
45 }
46
47 /** \brief Freeing function for dynars of xbt_host_t */
48 void xbt_host_free_voidp(void *d) {
49    xbt_host_free( (xbt_host_t) *(void**)d );
50 }