-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenServerCommand.cpp
48 lines (44 loc) · 1.88 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
//
// Created by tomer on 12/16/18.
//
#include "OpenServerCommand.h"
#include "ShuntingYard.h"
#include "DataReaderServer.h"
/*the function recieves vector of strings and caculates the 2 arguments since they might be
* written as mathimatical expression*/
void OpenServerCommand::doCommand(std::vector<string> *inputVec) {
//cout << "open server com" << endl;
//input validation
ShuntingYard myAlgo;
//todo add another variable in algorithm
this->port = myAlgo.evaluate(inputVec->at(1), this->symbolTable);
this->frequency = myAlgo.evaluate(inputVec->at(2), this->symbolTable);
//open thread and read a line in frequency, update symbol table
pthread_t threadID;
struct ServerData *serverData = new struct ServerData;
serverData->port = this->port;
serverData->frequency = this->frequency;
//wont run if symbolTable is NULL
if (this->symbolTable == NULL) {
throw "can not start server thread, bad pointer to symbol Table";
}
serverData->symbolTable = this->symbolTable;
//start the thread
//int rc = pthread_create(&threadID, NULL, serverThreadFunc, (void *)serverData);
pthread_create(&threadID, NULL, serverThreadFunc, (void *)serverData);
}
void OpenServerCommand::setSymbolTable(SymbolsTable *symbolTable) {
this->symbolTable = symbolTable;
}
//run in the thread. open a a dataReaderServer and in an infinite loop update the symbol table
//argument: ServerData struct with 0.int port 1.int frequency 2.pointer to symbolTable
void* serverThreadFunc(void *serverData) {
//serverData is of type ServerData
struct ServerData *serverData1;
serverData1 = (struct ServerData *) serverData;
//open the server, infinite loop with updates for symbolTable
DataReaderServer server(serverData1->port, serverData1->frequency, serverData1->symbolTable);
delete(serverData1);
serverData = NULL;
return nullptr;
}