summaryrefslogtreecommitdiff
path: root/ZKTest/src/ZKTest.cpp
blob: 7e22c214e7f57774b26a0f9ebaf96c66bd7e8d98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//============================================================================
// Name        : ZKTest.cpp
// Author      : Michael P Smith (AKA, Krin), Under Codethink .Ltd
// Version     : 0.0.1
// Copyright   : Your copyright notice
// Description : a first attempt at making a zookeeper client in C
//============================================================================

#include <iostream>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "zookeeper.h"

using namespace std;

static zhandle_t *zk;
static const clientid_t *session_id;
struct String_vector *list_of_children;
int timeout = 3000;
int responseCode = 0;

void safeShutdown(zhandle_t *zzh);
void watcher(zhandle_t *zzh, int type, int state, const char *path,
		void *watcherCtx);

int main(int argc, char **argv)
{
	session_id = NULL;
	char* p;
	cout << "Initialising Zookeeper" << endl; // prints !!!Hello World!!!

	zk = zookeeper_init("localhost:2181", watcher, timeout, session_id, NULL, 0);
	while (!zk)
	{

	}
	p = strtok(NULL, " ");
	std::cout << "starting authentication" << std::endl;

	zoo_add_auth(zk, "digest", p, p ? strlen(p) : 0, NULL, NULL);
	while (zoo_state(zk) == 0)
	{

	}
	std::cout << "authentication step done" << std::endl;

	while (zk)
	{

	}

	std::cout<<"why are we here?"<<std::endl;
	safeShutdown(zk);
	return 0;
}

///////////////////////////////////////////////////////////////////////
///                        END OF MAIN                              ///
///////////////////////////////////////////////////////////////////////

void safeShutdown(zhandle_t *zzh)
{
	if (zzh)
	{
		cout << "closing Zookeeper connection" << std::endl;
		zookeeper_close(zzh);
		zk = 0;
	}
}


static const char* state2String(int state)
{
	if (state == 0)
		return "CLOSED_STATE";
	if (state == ZOO_CONNECTING_STATE)
		return "CONNECTING_STATE";
	if (state == ZOO_ASSOCIATING_STATE)
		return "ASSOCIATING_STATE";
	if (state == ZOO_CONNECTED_STATE)
		return "CONNECTED_STATE";
	if (state == ZOO_EXPIRED_SESSION_STATE)
		return "EXPIRED_SESSION_STATE";
	if (state == ZOO_AUTH_FAILED_STATE)
		return "AUTH_FAILED_STATE";

	return "INVALID_STATE";
}

static const char* type2String(int type)
{
	if (type == ZOO_CREATED_EVENT)
		return "CREATED_EVENT";
	if (type == ZOO_DELETED_EVENT)
		return "DELETED_EVENT";
	if (type == ZOO_CHANGED_EVENT)
		return "CHANGED_EVENT";
	if (type == ZOO_CHILD_EVENT)
		return "CHILD_EVENT";
	if (type == ZOO_SESSION_EVENT)
		return "SESSION_EVENT";
	if (type == ZOO_NOTWATCHING_EVENT)
		return "NOTWATCHING_EVENT";

	return "UNKNOWN_EVENT_TYPE";
}
void watcher(zhandle_t *zzh, int type, int state, const char *path,
		void *watcherCtx)
{
	cout << "New event incoming: " << type2String(type) << " " << state2String(state) << " " << path << endl;
	if (type == ZOO_SESSION_EVENT)
	{
		if (state == ZOO_CONNECTED_STATE)
		{
			cout << "session Connected" << std::endl;
			zoo_create(zk, "/test","my_data",7, &ZOO_OPEN_ACL_UNSAFE, ZOO_EPHEMERAL, NULL, 0);
			zoo_create(zk, "/childTest","my_data",7, &ZOO_OPEN_ACL_UNSAFE, 0, NULL, 0);
			zoo_exists(zk, "/test", true, NULL);
			/*zoo_exists tests if a node exists, and if the 3rd argument is non 0 sets a watch
			 * that watch is triggered upon any change to the file specified in arg 2
			 * argument 1 is the zookeeper session to make the request too
			 * */

			responseCode = zoo_get_children(zk, "/childTest", true, NULL);
			/*
			 * as zoo_exists but watches children of the specified node.
			 * argument 4 is a string that can be used to return the path of child nodes.
			 */

			std::cout<<responseCode<<std::endl;
			session_id = zoo_client_id(zzh);
			return;
		}
		else if (state == ZOO_AUTH_FAILED_STATE)
		{
			cout << "Refused connection " << std::endl;
			safeShutdown(zzh);
		}
		else if (state == ZOO_EXPIRED_SESSION_STATE)
		{
			cout << "session expired attempting to re-connect" << std::endl;
			zk = zookeeper_init("localhost:2181", watcher, timeout, session_id, NULL,
					0);
		}
	}
	else if (type == ZOO_DELETED_EVENT)
	{
		cout << "node delete detected" << std::endl;
	}
	else if (type == ZOO_CHILD_EVENT)
	{
		//zoo_get_children(zk, path, true, NULL);
		cout << "child change detected!!" << std::endl;
		responseCode = zoo_get_children(zk, path, true, list_of_children);
		if(list_of_children)
		{
			for (int i = 0; i < list_of_children->count; i++)
			{
				std::cout<<list_of_children->count<<std::endl;
				responseCode = zoo_get_children(zk, path + '/' + *list_of_children->data[i] , true, NULL);
			}
		}
	}
	else if (type == ZOO_CHANGED_EVENT)
	{
		responseCode = zoo_exists(zk, path, true, NULL);
		std::cout<<state<<std::endl;
		cout << "change detected" << std::endl;
	}
}