学习eXtremeDB之余,用Cluster API写了一段测试代码。对于Cluster中的每一个node都有一个唯一的node id,而node id的值通过cluster node IP与本机IP的比较来获得。代码如下:

点击(此处)折叠或打开

  1. const char* NodesAddr[NODES_NUM] = {"192.168.1.1",
  2.     "192.168.1.2",
  3.     "192.168.1.3",
  4.     "192.168.1.4"};

  5. const char* NodesPort[NODES_NUM] = {"20000","20000","20000","20000"};

  6. .......

  7. /* according to ip address to set node id */
  8.     gethostname(host_name,sizeof(host_name));
  9.     ht = gethostbyname(host_name);
  10.     if(NULL != ht)
  11.     {
  12.         mco_memcpy(host_addr,inet_ntoa (*(struct in_addr *)*ht->h_addr_list),sizeof(host_addr));
  13.     }        

  14.     for (i = 0; i < NODES_NUM; ++i) {
  15.         if (mco_memcmp(host_addr,NodesAddr[i],sizeof(NodesAddr[i]))) {
  16.             cl_params.node_id = i;
  17.         }
  18.     }

09-21 20:40