-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenServerCommand.cpp
More file actions
111 lines (96 loc) · 3.46 KB
/
OpenServerCommand.cpp
File metadata and controls
111 lines (96 loc) · 3.46 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
//
// Created by duni on 20/12/2019.
//
#include <netinet/in.h>
#include <unistd.h>
#include "OpenServerCommand.h"
#include <iostream>
#include <thread>
#include "parseBufferToSymbleTable.h"
#include "globalVariables.h"
#include "ExpressionCalculate.h"
#include <algorithm>
// This function reads messages from the simulator
void listenForSimulatorMessages(int newSocket) {
char buffer1[1] = {0};
string bufferWithLine = "";
map<string, pair<float, string>>::iterator it;
auto *parseBufferToSymbolTable1 = new parseBufferToSymbolTable();
while (read(newSocket, buffer1, 1)) {
globalVariables::symbolTableMutex.lock();
char currChar = buffer1[0];
if (currChar != '\n') {
// Insert each letter to the current buffer - each buffer contains a single line
bufferWithLine += buffer1[0];
} else {
// If the current line is over - send it to the parsing function
parseBufferToSymbolTable1->parseBufferAnsSymbolTables(bufferWithLine);
bufferWithLine = "";
// Insert the given values into the xml map
for (it = globalVariables::symbolTableSimToClient.begin();
it != globalVariables::symbolTableSimToClient.end();
it++) {
string sim = it->second.second;
float value = globalVariables::xmlMap[sim];
it->second.first = value;
}
}
globalVariables::symbolTableMutex.unlock();
}
close(newSocket);
}
void connectServer(int port) {
int serverFd, newSocket, readValue;
struct sockaddr_in socketAddress;
int optionNumber = 1;
int addressLength = sizeof(socketAddress);
char *message;
// Create socket
if ((serverFd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// Attaching socket
if (setsockopt(serverFd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &optionNumber, sizeof(optionNumber))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
socketAddress.sin_family = AF_INET;
socketAddress.sin_addr.s_addr = INADDR_ANY;
socketAddress.sin_port = htons(port);
if (bind(serverFd, (struct sockaddr *) &socketAddress, sizeof(socketAddress)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(serverFd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
// Accept clients
cout << "accepting clients" << endl;
if ((newSocket = accept(serverFd, (struct sockaddr *) &socketAddress, (socklen_t *) &addressLength)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
cout << "client connected, listen for messages" << endl;
// Read from the simulator every 10 milliseconds
thread simSyncThread(listenForSimulatorMessages, newSocket);
simSyncThread.detach();
}
int OpenServerCommand::execute(vector<string> params) {
Interpreter *i = new Interpreter();
Expression *answer = nullptr;
// Remove spaces
string::iterator end_pos = remove(params[1].begin(), params[1].end(), ' ');
params[1].erase(end_pos, params[1].end());
// Remove tabs
end_pos = remove(params[1].begin(), params[1].end(), '\t');
params[1].erase(end_pos, params[1].end());
// Calculate the port
answer = i->interpret(params[1]);
int port = answer->calculate();
// Open the thread
thread threadServer(connectServer, port);
threadServer.join();
return 2;
}