-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataReaderServer.cpp
More file actions
133 lines (126 loc) · 5.49 KB
/
DataReaderServer.cpp
File metadata and controls
133 lines (126 loc) · 5.49 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
#include <netinet/in.h>
#include <cstdio>
#include <strings.h>
#include <unistd.h>
#include <vector>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <cstring>
#include "DataReaderServer.h"
#include "DataBinds.h"
using namespace std;
DataReaderServer::DataReaderServer(int &port, int &perSec, DataBinds *dataBinds, DataVars *dataVars, pthread_mutex_t &mutex) {
this->perSec = perSec;
this->port = port;
this->dataBinds = dataBinds;
this->dataVars = dataVars;
this->mutex = mutex;
}
void DataReaderServer::openServer() {
int socketFd, newSockFd, clientLen;
char buffer[1024];
struct sockaddr_in serv_addr{}, cli_addr{};
int n;
// First call to socket() function
socketFd = socket(AF_INET, SOCK_STREAM, 0);
if (socketFd < 0) {
perror("ERROR opening socket");
return;
}
// Initialize socket structure
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(static_cast<uint16_t>(this->port));
// Now bind the host address using bind() call
if (bind(socketFd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR on binding");
return;
}
/* Now start listening for the clients, here process will go in sleep mode and
* will wait for the incoming connection */
listen(socketFd, 5);
clientLen = sizeof(cli_addr);
// Accept actual connection from the client
newSockFd = accept(socketFd, (struct sockaddr *) &cli_addr, (socklen_t *) &clientLen);
if (newSockFd < 0) {
perror("ERROR on accept");
return;
}
// Create a vector arranged in order of variables in the XML file
vector<string> xmlVariables = {"/instrumentation/airspeed-indicator/indicated-speed-kt",
"/instrumentation/altimeter/indicated-altitude-ft",
"/instrumentation/altimeter/pressure-alt-ft",
"/instrumentation/attitude-indicator/indicated-pitch-deg",
"/instrumentation/attitude-indicator/indicated-roll-deg",
"/instrumentation/attitude-indicator/internal-pitch-deg",
"/instrumentation/attitude-indicator/internal-roll-deg",
"/instrumentation/encoder/indicated-altitude-ft",
"/instrumentation/encoder/pressure-alt-ft",
"/instrumentation/gps/indicated-altitude-ft",
"/instrumentation/gps/indicated-ground-speed-kt",
"/instrumentation/gps/indicated-vertical-speed",
"/instrumentation/heading-indicator/indicated-heading-deg",
"/instrumentation/magnetic-compass/indicated-heading-deg",
"/instrumentation/slip-skid-ball/indicated-slip-skid",
"/instrumentation/turn-indicator/indicated-turn-rate",
"/instrumentation/vertical-speed-indicator/indicated-speed-fpm",
"/controls/flight/aileron",
"/controls/flight/elevator",
"/controls/flight/rudder",
"/controls/flight/flaps",
"/controls/engines/current-engine/throttle",
"/engines/engine/rpm"};
string dataFromSimulator;
while (true) {
char c = 'n';
int idx = 0;
while (c != '\n') {
n = static_cast<int>(read(newSockFd, &c, 1));
if (n < 0) {
perror("ERROR reading from socket");
return;
} else if (n == 0) {
perror("Socket is closed");
return;
}
buffer[idx++] = c;
}
buffer[idx] = 0;
// put the data in vector
dataFromSimulator = buffer;
replace(dataFromSimulator.begin(), dataFromSimulator.end(), ',', ' '); // replace ' , ' by ' '
vector<double> dataValues;
stringstream ss(dataFromSimulator);
double temp;
while (ss >> temp) {
dataValues.push_back(temp);
}
// Replaces any vector name found on the Bind map in the name of Var
vector<string> xmlVariablesCopy = xmlVariables;
auto itXml1 = xmlVariablesCopy.begin();
while (itXml1 != xmlVariablesCopy.end()) {
for (auto &itBinds : this->dataBinds->getVarToNameInSimulator()) {
string bindPath=itBinds.second;
bindPath=bindPath.substr(1,bindPath.length()-2);
if (strcmp(bindPath.c_str(), (*itXml1).c_str())== 0) {
*itXml1 = itBinds.first;
}
}
itXml1++;
}
pthread_mutex_lock(&this->mutex);
// Updating the symbolTable according to the values received
auto itXml2 = xmlVariablesCopy.begin();
auto itValues = dataValues.begin();
while (itXml2 != xmlVariablesCopy.end()) {
if(this->dataVars->getSymbolTable().count(*itXml2)>=1){
this->dataVars->setSymbolTableValue(*itXml2, *itValues);
}
itXml2++;
itValues++;
}
pthread_mutex_unlock(&this->mutex);
}
}