Imported Upstream version 5.3.21
[platform/upstream/libdb.git] / examples / cxx / excxx_repquote_gsg / RepConfigInfo.h
1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2006, 2012 Oracle and/or its affiliates.  All rights reserved.
5  *
6  * $Id$
7  */
8
9 #include <cstdlib>
10 #include <cstring>
11
12 #include <db_cxx.h>
13
14 // Chainable struct used to store host information.
15 typedef struct RepHostInfoObj{
16     char* host;
17     u_int16_t port;
18     RepHostInfoObj* next; // used for chaining multiple "other" hosts.
19     bool creator;
20 } REP_HOST_INFO;
21
22 class RepConfigInfo {
23 public:
24     RepConfigInfo();
25     virtual ~RepConfigInfo();
26
27     void addOtherHost(char* host, int port);
28 public:
29     u_int32_t start_policy;
30     char* home;
31     bool got_listen_address;
32     REP_HOST_INFO this_host;
33     int nrsites;
34     int priority;
35     // used to store a set of optional other hosts.
36     REP_HOST_INFO *other_hosts;
37 };
38
39
40 RepConfigInfo::RepConfigInfo()
41 {
42     start_policy = DB_REP_ELECTION;
43     home = NULL;
44     got_listen_address = false;
45     nrsites = 0;
46     priority = 100;
47     other_hosts = NULL;
48 }
49
50 RepConfigInfo::~RepConfigInfo()
51 {
52     // release any other_hosts structs.
53     if (other_hosts != NULL) {
54         REP_HOST_INFO *CurItem = other_hosts;
55         while (CurItem->next != NULL) {
56             REP_HOST_INFO *TmpItem = CurItem->next;
57             free(CurItem);
58             CurItem = TmpItem;
59         }
60         free(CurItem);
61     }
62     other_hosts = NULL;
63 }
64
65 void RepConfigInfo::addOtherHost(char* host, int port)
66 {
67     REP_HOST_INFO *newinfo;
68     newinfo = (REP_HOST_INFO*)malloc(sizeof(REP_HOST_INFO));
69     newinfo->host = host;
70     newinfo->port = port;
71     if (other_hosts == NULL) {
72         other_hosts = newinfo;
73         newinfo->next = NULL;
74     } else {
75         newinfo->next = other_hosts;
76         other_hosts = newinfo;
77     }
78     nrsites++;
79 }