-
Notifications
You must be signed in to change notification settings - Fork 1
/
ServerCommunication.cpp
131 lines (91 loc) · 3.35 KB
/
ServerCommunication.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
/*
* File: ServerCommunication.cpp
* Author: Jan Dufek
*/
#include "ServerCommunication.hpp"
ServerCommunication::ServerCommunication(FotokiteState * fotokiteState, const string send_pipe, const char * ip_address, const short port_receive) : Communication(fotokiteState) {
// Initialize pipe for sending
initializeSendPipe(send_pipe);
// Initialize socket for receiving
initializeReceiveSocket(ip_address, port_receive);
// Initialize Fotokite remote control mode
this->startRemoteControl();
// Start listener
this->startListener();
}
ServerCommunication::ServerCommunication(const ServerCommunication& orig) : Communication(orig) {
}
ServerCommunication::~ServerCommunication() {
close_connection();
}
void ServerCommunication::initializeSendPipe(const string pipePath) {
// Initialize pipe
sendPipe.open(pipePath);
cout << "Send pipe " << pipePath << " initialized." << endl;
}
void ServerCommunication::initializeReceiveSocket(const char * ip_address, const short port) {
// Create socket descriptor. We want to use datagram UDP.
socket_descriptor_receive = socket(AF_INET, SOCK_STREAM, 0);
if (socket_descriptor_receive < 0) {
cout << "Error creating receive socket descriptor." << endl;
}
// Create socket address
socket_address_receive.sin_family = AF_INET;
socket_address_receive.sin_addr.s_addr = inet_addr(ip_address);
socket_address_receive.sin_port = htons(port);
// Connect to the server
int status = connect(socket_descriptor_receive, (struct sockaddr *) &socket_address_receive, sizeof (socket_address_receive));
// Check if the connection was established
if (status < 0) {
cerr << "Error connecting to the TCP server at IP address " << ip_address << " at port " << to_string(port) << "! Make sure your machine is connected to the OCU server via ethernet and has IP address on the same network." << endl;
}
// Notify socket to start sending data
string message = "GET /fstat HTTP/1.0\r\n\r\n";
status = write(socket_descriptor_receive, message.data(), message.size());
// Check if the action was successful
if (status < 0) {
cerr << "Error initializing receive socket!" << endl;
}
cout << "Receive socket " << ip_address << ":" << to_string(port) << " initialized." << endl;
}
/**
* Send message to Fotokite.
*
* @param message
*/
void ServerCommunication::send(string message) {
// Send message
sendPipe << message;
sendPipe.flush();
}
/**
* Receive message from Fotokite.
*
* @return
*/
string ServerCommunication::receive() {
// Initialize buffer for the message
char buffer[512];
bzero(buffer, 512);
// Receive message
int status = read(socket_descriptor_receive, buffer, 511);
// Check if the action was successful
if (status < 0) {
cerr << "Error reading from the socket!" << endl;
}
// Return message
return buffer;
}
/**
* Close connection with Fotokite. Do not exit remote control mode as server is still using it.
*/
void ServerCommunication::close_connection() {
// Stop listening
listening = false;
// Join with listener
listener.join();
// Close send pipe
sendPipe.close();
// Close receive socket
close(socket_descriptor_receive);
}