-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.cpp
191 lines (168 loc) · 4.39 KB
/
Server.cpp
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
//
// Created by matan on 12/20/19.
//
#include <cstring>
#include "Server.h"
map<string, Var*> inSymbolTable;
/*
* a command that runs the server with
* 2 argumentsL one for terminsting the server and one for
* telling the main thread that we have connected:
*/
int Server::execute(bool& g_isReady, bool* terminate) {
//create socket
int socketfd = socket(AF_INET, SOCK_STREAM, 0);
if (socketfd == -1) {
//error
std::cerr << "Could not create a socket"<<std::endl;
}
//bind socket to IP address
// we first need to create the sockaddr obj.
sockaddr_in address; //in means IP4
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY; //give me any IP allocated for my machine
address.sin_port = htons(port);
//we need to convert our number
// to a number that the network understands.
//the actual bind command
if (bind(socketfd, (struct sockaddr *) &address, sizeof(address)) == -1) {
std::cerr<<"Could not bind the socket to an IP"<<std::endl;
}
//making socket listen to the port
if (listen(socketfd, 5) == -1) { //can also set to SOMAXCON (max connections)
std::cerr<<"Error during listening command"<<std::endl;
} else{
std::cout<<"Server is now listening ..."<<std::endl;
}
// accepting a client
int client_socket = accept(socketfd, (struct sockaddr *)&address,
(socklen_t*)&address);
if (client_socket == -1) {
std::cerr<<"Error accepting client"<<std::endl;
}
close(socketfd); //closing the listening socket
//reading from client
char buffer[1024] = {0};
read( client_socket , buffer, 1024);
buffer[1023] = '\0';
bool firstFirst = true;
//translating the buffer into lines:
list<string> listOfLinesFirst = toLines(buffer);
for(string currLine : listOfLinesFirst)
{
list<double> valsList1 = bufferToList(currLine);
(Server::updateAllIn)(valsList1, firstFirst);
firstFirst = false;
}
//telling the main thread that we have connected:
g_isReady = true;
while(true)
{
//checking if we shold terminate:
if(*terminate)
{
close(client_socket);
return 2;
}
//reading from the client:
char inWhileBuffer[1024] = {0};
bool first = true;
read( client_socket , inWhileBuffer, 1024);
inWhileBuffer[1023] = '\0';
list<string> listOfLines = toLines(inWhileBuffer);
//updating the symbol table for each line:
for(string currLine : listOfLines)
{
list<double> valsList1 = bufferToList(currLine);
(Server::updateAllIn)(valsList1, first);
first = false;
}
}
}
/*
* a method for updating all the
* vars in the symbol tabel:
*/
void Server::updateAllIn(list<double> vals, bool first)
{
//if(!first || vals.size() == indexToPath.size())
if(vals.size() == indexToPath.size() && (first || ! first))
{
int index = 0;
for(double val : vals)
{
string path = '\"' + indexToPath[index] + '\"';
//going through each val:
if(inSymbolTable.find(path) != inSymbolTable.end())
{
protectSymbolTable.lock();
inSymbolTable[path]->changeVal(val);
protectSymbolTable.unlock();
}
index++;
}
}
}
/*
* a method for converting a buffer into list:
*/
list<double> bufferToList(string buff)
{
list<double> listOfVals;
string builder = "";
for(auto curr : buff)
{
//checking for special characters:
if(curr == '\0' || curr == '\n' || curr == '\r' || curr == ',')
{
listOfVals.push_back(toNumber(builder));
builder = "";
}
else
{
builder += curr;
}
}
return listOfVals;
}
//constructor:
Server::Server(int port1) {
Server::port = port1;
}
//a method for checking the amount of chars to the enter:
int Server::sizeToEnter(char *buffer) {
int count = 0;
while(buffer[count] != '\0' && buffer[count] != '\n' && count < 1024)
{
count++;
}
return count;
}
//chnverting the buffer into seprate lines:
list<string> Server::toLines(char *buffer) {
list<string> lines;
int i = 0;
//going through all the biffer:
while(i < 1024 && buffer[i] != '\0')
{
string currLine = "";
while(buffer[i] != '\0' && buffer[i] != '\n' && i < 1024)
{
//adding the curr character:
currLine += buffer[i];
i++;
if(i >= 1024)
{
break;
}
}
currLine += "\n";
lines.push_back(currLine);
i++;
if(i >= 1024)
{
break;
}
}
return lines;
}