-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVisualization.cpp
More file actions
98 lines (70 loc) · 2.25 KB
/
Copy pathVisualization.cpp
File metadata and controls
98 lines (70 loc) · 2.25 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
/*
* File: Visualization.cpp
* Author: Jan Dufek
*
* Created on October 10, 2018, 8:24 PM
*/
#include "Visualization.hpp"
Visualization::Visualization(const char * ip_address, const short port_send) {
initializeSocket(ip_address, port_send);
}
Visualization::Visualization(const Visualization& orig) {
}
Visualization::~Visualization() {
close_connection();
}
void Visualization::initializeSocket(const char * ip_address, const short port) {
// Create socket descriptor. We want to use datagram UDP.
socket_descriptor = socket(AF_INET, SOCK_STREAM, 0);
if (socket_descriptor < 0) {
cout << "Error creating visualization socket descriptor." << endl;
}
// Create socket address
socket_address.sin_family = AF_INET;
socket_address.sin_addr.s_addr = inet_addr(ip_address);
socket_address.sin_port = htons(port);
// Connect to the server
int status = connect(socket_descriptor, (struct sockaddr *) &socket_address, sizeof (socket_address));
// 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 visualization server.
*
* @param message
*/
void Visualization::send(string message) {
// Send message
int status = write(socket_descriptor, message.data(), message.size());
// Check if the action was successful
if (status < 0) {
cerr << "Error writing to the socket!" << endl;
}
}
/**
* Receive message from visualization server.
*
* @return
*/
string Visualization::receive() {
// Initialize buffer for the message
char buffer[512];
bzero(buffer, 512);
// Receive message
int status = read(socket_descriptor, 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 visualization server.
*/
void Visualization::close_connection() {
// Close send socket
close(socket_descriptor);
}