-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_thread.cpp
More file actions
222 lines (191 loc) · 5.05 KB
/
Copy pathcmd_thread.cpp
File metadata and controls
222 lines (191 loc) · 5.05 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <string>
#include <iostream>
#include <vector>
#include "servant.h"
//Please call this function under the lock of g_peerid2sd
//Will create a status req packet
int create_status_res(Hdr** hdr_p, void** vmsg_p, Hdr *shdr)
{
printf("Entering create_status_res() \n");
Hdr *hdr;
void *vmsg;
string hostid = "", hostname = "", hostport;
size_t pos;
uint16_t port;
uint32_t len;
char c_port[3] = "AA";
char c_len[5] = "AAAA";
string s_len;
vector< string > v_hostname;
vector< uint16_t > v_hostport;
vector< int > v_length;
int sum_len = 0;
printf("Initially c_port is %s", c_port);
map<string, int>::iterator itr;
for(itr = g_hostid2sd.begin(); itr != g_hostid2sd.end(); ++itr) {
cout<<"I have a connection establised\n";
hostid = itr->first;
pos = hostid.find("_");
hostname = hostid.substr(0, pos);
hostport = hostid.substr(pos + 1);
istringstream buffer(hostport);
//converting a string to int
uint16_t temp;
int some_int;
buffer >> some_int;
//coneverting into byte form
temp = some_int;
port = htons(temp);
v_hostname.push_back(hostname);
v_hostport.push_back(port);
some_int = hostname.length() + 2;
//temp = htonl(some_int);
v_length.push_back(some_int);
sum_len += hostname.length() + 2 + 4;
}
sum_len = sum_len + 4;
printf("Vectors initialized\n");
printf("\nSum_len = %d\n", sum_len);
char *reply = (char *)calloc(sum_len, sizeof(char));
int j=0;
for(int i = 0; i<v_hostport.size(); i++) {
int l_temp = htonl(v_length[i]);
memcpy(reply + j, &l_temp, 4);
int s_temp = v_hostport[i];
memcpy(reply + j + 4, &s_temp, 2);
printf("Hope !!\n");
const char *c_hostname = v_hostname[i].c_str();
memcpy(reply + j + 6, c_hostname, v_length[i] - 2);
j = j + v_length[i] + 4;
}
for(unsigned int i =0; i< sum_len; i++)
printf("%02x ", (unsigned char)reply[i]);
printf("\n");
status_res(&hdr, &vmsg, shdr, sum_len, reply);
*hdr_p = hdr;
*vmsg_p = vmsg;
printf("Leaving the create_status_reply\n");
return 1;
}
using namespace std;
void * cmd_write(void *)
{
// queue<Event> g_cmd_que;
// pthread_mutex_t g_mutex_cmd_que;
// pthread_cond_t g_cond_cmd_que;
Event e1;
map<int><int> nodes_done;
map<int, int>::iterator itr;
Status_res *msg;
while(1) {
pthread_mutex_lock(&(g_mutex_cmd_que));
while(g_cmd_que.size() == 0) {
if(short_circuit == 1) {
pthread_mutex_unlock(&(g_mutex_cmd_que));
return NULL;
}
pthread_cond_wait(&g_cond_cmd_que, &g_mutex_cmd_que);
}
e1 = g_cmd_que.front();
g_cmd_que.pop();
pthread_mutex_unlock(&(g_mutex_cmd_que));
msg = (Status_res *)e1->vmsg;
uint32_t temp,record_len, i = 0;
uint16_t stemp, port;
memcpy(&temp, msg->data, 4);
record_len = ntohl(temp);
i=0;
while(record_len != 0) {
memcpy(&stemp, msg->data + 4 + i, 2);
port = ntohs(stemp);
itr = nodes_done.find(port);
if(itr == nodes_done.end()) {
nodes_done[port] = 1;
//Add the node to the nam file
}
//Add the link to the name file
i = i + record_len;
}
}
return NULL;
}
}
void Tokenize(const string& str, vector<string>& tokens, const string& delimiters = " ")
{
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
void* cmd_thread(void *)
{
fd_set readfds;
struct timeval tv;
printf("\n");
fflush(stdout);
char *line;
int line_len;
int flag = 1;
char ch;
Cmd* cmd = new cmd;
while (1) {
if(flag == 1) {
printf("servant:>" );//ini.port);
fflush(stdout);
flag = 0;
}
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);
tv.tv_sec = 1;
tv.tv_usec = 0;
int select_retval = select(STDIN_FILENO+1, &readfds, NULL, NULL, &tv);
if (select_retval < 0) {
perror("select");
} else if (select_retval > 0) {
if (FD_ISSET(STDIN_FILENO, &readfds)) {
string s = "";
getline(cin,s);
vector<string> words;
Tokenize(s, words);
cout<<words[0];
flag = 1;
if(words[0] == "status") {
if(words[1] == "neighbors") {
cout<<"I was here :)";
fflush(stdout);
//cmd->type = CMD_STATUS_N;
const char *w = words[2].c_str();
cout<<"Is it printed"<<atoi(w);
//cmd->ttl = atoi(w);
//cmd->ext_file = words[3];
//Please add cmd to the event queue ....
Event ev(EV_CMD, cmd);
}
} else if(words[0] == "shutdown") {
kill_this_node(1);
exit(1);
}
}
} else {
//printf("Time out :)");
fflush(stdout);
}
}
return NULL;
}