-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathComunicateWithSimulator.cpp
More file actions
69 lines (63 loc) · 1.85 KB
/
Copy pathComunicateWithSimulator.cpp
File metadata and controls
69 lines (63 loc) · 1.85 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
//
// Created by ofir8 on 12/19/18.
//
#include <unistd.h>
#include "ComunicateWithSimulator.h"
#include "MapsHandler.h"
int ComunicateWithSimulator::sockfd = -1;
int ComunicateWithSimulator::DataServerSocket = -1;
pthread_t* ComunicateWithSimulator::th = 0;
bool ComunicateWithSimulator::isServerOpen = false;
/**
* the method sends to the simulator the address st with the value val
*/
bool ComunicateWithSimulator::sendToServer(string st, double val) {
//Create string of the commnad
string command = "set " + MapsHandler::getVarAddress(st)
+ " " + std::to_string(val) + " \r\n";
//Send to server
int n = write(sockfd, command.c_str(), command.length());
//make sure the massage sent successfully
if (n < 0) {
return false;
}
return true;
}
/**
* the method asks from the simulator for value of some address and save the result
*/
double ComunicateWithSimulator::getFromServer(string address) {
//Get command
string query = "get " + address + " \r\n";
//Send to the server the command
int n = write(sockfd, query.c_str(), query.length());
//Get Result
char buffer[200];
n = read(sockfd,buffer,200);
if (n < 0) {
return 0;
}
//Substring only the double
string result = std::string(buffer);
int start = result.find("'");
result = result.substr(start + 1);
int end = result.find("'");
result = result.substr(0, end);
return stod(result);
}
/**
* the method closes the client and server sockets and the thread we created
*/
void ComunicateWithSimulator::closeAll() {
//Close the client socket to the simulator
if (sockfd > 0) {
close(sockfd);
}
//Close Data Server Socket
if (DataServerSocket > 0) {
isServerOpen = false;
close(DataServerSocket);
}
//Wait for the thread to close
sleep(1);
}