-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSocketCommunication.cpp
145 lines (102 loc) · 3.96 KB
/
SocketCommunication.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* File: SocketCommunication.cpp
* Author: Jan Dufek
*/
#include "SocketCommunication.hpp"
SocketCommunication::SocketCommunication(FotokiteState * fotokiteState, const char * ip_address, const short port_send, const short port_receive) : Communication(fotokiteState) {
// Initialize socket for sending
initializeSendSocket(ip_address, port_send);
// Initialize socket for receiving
initializeReceiveSocket(ip_address, port_receive);
// Initialize Fotokite remote control mode
this->startRemoteControl();
// Start listener
this->startListener();
}
SocketCommunication::SocketCommunication(const SocketCommunication& orig) : Communication(orig) {
}
SocketCommunication::~SocketCommunication() {
close_connection();
}
void SocketCommunication::initializeSendSocket(const char * ip_address, const short port) {
// Create socket descriptor. We want to use datagram UDP.
socket_descriptor_send = socket(AF_INET, SOCK_STREAM, 0);
if (socket_descriptor_send < 0) {
cout << "Error creating send socket descriptor." << endl;
}
// Create socket address
socket_address_send.sin_family = AF_INET;
socket_address_send.sin_addr.s_addr = inet_addr(ip_address);
socket_address_send.sin_port = htons(port);
// Connect to the server
int status = connect(socket_descriptor_send, (struct sockaddr *) &socket_address_send, sizeof (socket_address_send));
// 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;
}
}
void SocketCommunication::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;
}
}
/**
* Send message to Fotokite.
*
* @param message
*/
void SocketCommunication::send(string message) {
// Send message
int status = write(socket_descriptor_send, message.data(), message.size());
// Check if the action was successful
if (status < 0) {
cerr << "Error writing to the socket!" << endl;
}
}
/**
* Receive message from Fotokite.
*
* @return
*/
string SocketCommunication::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.
*/
void SocketCommunication::close_connection() {
// Stop listening
listening = false;
// Join with listener
listener.join();
// Stop Ground Station remote control mode
send("stop\n");
// Stop the pass-throught OCU server
send("\x07");
// Close send socket
close(socket_descriptor_send);
// Close receive socket
close(socket_descriptor_receive);
}