-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenServerCommand.cpp
169 lines (147 loc) · 4.43 KB
/
OpenServerCommand.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
#include "Command.h"
#include "OpenServerCommand.h"
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <thread>
#include <netdb.h>
#include <unistd.h>
#include <netinet/in.h>
#include "GlobalTables.h"
#include <string.h>
#include "ShuntingYard.h"
#include <sys/socket.h>
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreserved-user-defined-literal"
using namespace std;
static int socketServer;
int OpenServerCommand::execute(vector<string> cmdTemp, int index){
ShuntingYard sY = ShuntingYard();
string port, time;
int pos = 0;
int endLine = enterKey(cmdTemp, index);
string s = vectorToString(cmdTemp,index, index + endLine);
if(s.find(",", pos) == string::npos) {
string port = to_string(sY.calculate(cmdTemp.at(index + 1)));
string time = to_string(sY.calculate(cmdTemp.at(index + 2)));
openSocket(stoi(port), stoi(time));
}
else {
string port = to_string(sY.calculate(s.substr(0,pos)));
string time = to_string(sY.calculate(s.substr(pos + 1, s.length() - 1)));
openSocket(stoi(port), stoi(time));
}
return 3;
}
void OpenServerCommand::openSocket(int port, int time) {
int sockfd, newsockfd, portno, clilen;
struct sockaddr_in serv_addr, cli_addr;
/* First call to socket() function */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("ERROR opening socket");
exit(1);
}
/* Initialize socket structure */
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = port;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
/* Now bind the host address using bind() call.*/
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR on binding");
exit(1);
}
socketServer = sockfd;
/* Now start listening for the clients, here process will
* go in sleep mode and will wait for the incoming connection
*/
listen(sockfd,25);
clilen = sizeof(cli_addr);
/* Accept actual connection from the client */
newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, (socklen_t*)&clilen);
if (newsockfd < 0) {
perror("ERROR on accept");
exit(1);
}
socketServer = newsockfd;
pthread_t t2;
pthread_create(&t2,nullptr,&OpenServerCommand::runServer,this);
}
void *OpenServerCommand::runServer(void *args) {
char buffer[1024];
int n;
vector<double> vecTemp;
bzero(buffer,1025);
while(!endSignal) {
bzero(buffer,1025);
n = (int)read(socketServer, buffer, 1025);
if (n < 0) {
perror("ERROR reading from socket");
pthread_exit(NULL);
}
vecTemp = buffToDouble(buffer);
updateTable(vecTemp);
}
pthread_exit(&args);
}
vector<double> OpenServerCommand::buffToDouble(string buffer) {
string temp;
vector<double> vecTemp;
int base = 0, j = 0;
for(int i = 0; i < (int)(buffer.length()) && j < (int)(symbolServer.size()); i++) {
if(buffer[i] == ',') {
j++;
temp = buffer.substr(base,i - base);
base += i - base + 1;
try {
vecTemp.push_back(stod(temp));
}
catch(std::invalid_argument) {
printf("Not of double type\n");
vecTemp.push_back(0);
}
}
}
return vecTemp;
}
void *OpenServerCommand::updateTable(vector<double> vecTemp) {
string location;
while(!runClient) {
sleep(10);
}
try {
for (int i = 0; i < (int)(vecTemp.size()); i++) {
location = tableOrder.at(i);
symbolServer.at(location) = vecTemp.at(i);
cout << location + " " + to_string((int)symbolServer.at(location)) << endl;
}
}catch(std::out_of_range) {
printf("Index exceding vector's size\n");
}
cout << endl;
usleep(100);
}
int OpenServerCommand::enterKey(vector<string> vec, int index){
int i = 0;
while(vec.at(index + i) != "\n") {
i++;
}
return i;
}
string OpenServerCommand::vectorToString(vector<string> vector, int index, int end) {
string ret = vector.at(index) + " ";
int i = 1;
while(index + i < end){
if(ret.length() > 1)
ret += vector.at(index + i) + " ";
else {
break;
}
i++;
}
return ret;
}
OpenServerCommand::~OpenServerCommand() = default;
#pragma clang diagnostic pop