forked from mckeemseattleu/CPSC5042CSServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRPCServer.cpp
More file actions
221 lines (182 loc) · 5.19 KB
/
RPCServer.cpp
File metadata and controls
221 lines (182 loc) · 5.19 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
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <vector>
#include <iterator>
#include "RPCServer.h"
//#define PORT 8081
using namespace std;
RPCServer::RPCServer(const char *serverIP, int port)
{
m_rpcCount = 0;
m_serverIP = (char *) serverIP;
m_port = port;
};
RPCServer::~RPCServer() {};
/*
* StartServer will create a server on a Port that was passed in, and create a socket
*/
bool RPCServer::StartServer()
{
int opt = 1;
const int BACKLOG = 10;
// Creating socket file descriptor
if ((m_server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(m_server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
// Comment
m_address.sin_family = AF_INET;
m_address.sin_addr.s_addr = INADDR_ANY;
m_address.sin_port = htons(m_port);
// Forcefully attaching socket to the port 8080
if (bind(m_server_fd, (struct sockaddr*)&m_address,
sizeof(m_address)) < 0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(m_server_fd, BACKLOG) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
return true;
}
/*
* Will accept a new connection by listening on it's address
*
*/
bool RPCServer::ListenForClient()
{
int addrlen = sizeof(m_address);
if ((m_socket = accept(m_server_fd, (struct sockaddr*)&m_address,
(socklen_t*)&addrlen)) < 0)
{
perror("accept");
exit(EXIT_FAILURE);
}
this->ProcessRPC();
return true;
}
/*
* Going to populate a String vector with tokens extracted from the string the client sent.
* The delimter will be a ;
* An example buffer could be "connect;mike;mike;"
*/
void RPCServer::ParseTokens(char * buffer, std::vector<std::string> & a)
{
char* token;
char* rest = (char *) buffer;
while ((token = strtok_r(rest, ";", &rest)))
{
printf("%s\n", token);
a.push_back(token);
}
return;
}
/*
* ProcessRPC will examine buffer and will essentially control
*/
bool RPCServer::ProcessRPC()
{
const char* rpcs[] = { "connect", "disconnect", "status"};
char buffer[1024] = { 0 };
std::vector<std::string> arrayTokens;
int valread = 0;
bool bConnected = false;
bool bStatusOk = true;
const int RPCTOKEN = 0;
bool bContinue = true;
while ((bContinue) && (bStatusOk))
{
// Should be blocked when a new RPC has not called us yet
if ((valread = read(this->m_socket, buffer, sizeof(buffer))) <= 0)
{
printf("errno is %d\n", errno);
break;
}
printf("%s\n", buffer);
arrayTokens.clear();
this->ParseTokens(buffer, arrayTokens);
// Enumerate through the tokens. The first token is always the specific RPC
for (vector<string>::iterator t = arrayTokens.begin(); t != arrayTokens.end(); ++t)
{
printf("Debugging our tokens\n");
printf("token = %s\n", t);
}
// string statements are not supported with a switch, so using if/else logic to dispatch
string aString = arrayTokens[RPCTOKEN];
if ((bConnected == false) && (aString == "connect"))
{
bStatusOk = ProcessConnectRPC(arrayTokens); // Connect RPC
if (bStatusOk == true)
bConnected = true;
}
else if ((bConnected == true) && (aString == "disconnect"))
{
bStatusOk = ProcessDisconnectRPC();
printf("We are going to terminate this endless loop\n");
bContinue = false; // We are going to leave this loop, as we are done
}
else if ((bConnected == true) && (aString == "status"))
bStatusOk = ProcessStatusRPC(); // Status RPC
else
{
// Not in our list, perhaps, print out what was sent
}
}
return true;
}
bool RPCServer::ProcessConnectRPC(std::vector<std::string> & arrayTokens)
{
const int USERNAMETOKEN = 1;
const int PASSWORDTOKEN = 2;
// Strip out tokens 1 and 2 (username, password)
string userNameString = arrayTokens[USERNAMETOKEN];
string passwordString = arrayTokens[PASSWORDTOKEN];
char szBuffer[80];
// Our Authentication Logic. Looks like Mike/Mike is only valid combination
if ((userNameString == "MIKE") && (passwordString == "MIKE"))
{
strcpy(szBuffer, "1;"); // Connected
}
else
{
strcpy(szBuffer, "0;"); // Not Connected
}
// Send Response back on our socket
int nlen = strlen(szBuffer);
szBuffer[nlen] = 0;
send(this->m_socket, szBuffer, strlen(szBuffer) + 1, 0);
return true;
}
/* TDB
*/
bool RPCServer::ProcessStatusRPC()
{
return true;
}
/*
*/
bool RPCServer::ProcessDisconnectRPC()
{
char szBuffer[16];
strcpy(szBuffer, "disconnect");
// Send Response back on our socket
int nlen = strlen(szBuffer);
szBuffer[nlen] = 0;
send(this->m_socket, szBuffer, strlen(szBuffer) + 1, 0);
return true;
}