-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketProcessSender.hpp
More file actions
94 lines (80 loc) · 2.13 KB
/
Copy pathSocketProcessSender.hpp
File metadata and controls
94 lines (80 loc) · 2.13 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
// Copyright 2016
#ifndef SOCKETPROCESSSENDER_H
#define SOCKETPROCESSSENDER_H
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <exception>
#include <string>
#include "SchedulerResult.hpp"
class SocketProcessSender {
private:
struct sockaddr_in serverAddress;
struct hostent *server;
int port;
int socketd;
void createSocket() {
socketd = socket(AF_INET, SOCK_STREAM, 0);
if (socketd < 0) {
throw std::runtime_error("Error opening socket");
}
}
void startServer() {
server = gethostbyname("127.0.0.1");
if (server == 0) {
throw std::runtime_error("Error no host available");
}
}
void setServerAddress() {
bzero(reinterpret_cast<char *>(&serverAddress), sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
bcopy(reinterpret_cast<char *> (server->h_addr),
reinterpret_cast<char *> (&serverAddress.sin_addr.s_addr),
server->h_length);
serverAddress.sin_port = htons(port);
}
void connectToSocket() {
auto connectionResult = connect(socketd,
(struct sockaddr *) &serverAddress,
sizeof(serverAddress));
if (connectionResult < 0)
throw std::runtime_error("Cannot connect to socket");
}
void writeSafely(const char* message) {
int n = ::send(socketd, message, 1024, 0);
if (n < 0) {
end();
throw std::runtime_error("Error writing to socket.");
}
}
void writeSafely(std::string string) {
writeSafely(string.c_str());
}
void start() {
createSocket();
startServer();
setServerAddress();
connectToSocket();
}
void end() {
close(socketd);
}
public:
explicit SocketProcessSender(int port) : port(port) {}
virtual ~SocketProcessSender() {}
void send(std::string title, const SchedulerResult& result) {
start();
writeSafely(title +
"\n" +
"Average wait time: " +
std::to_string(result.getAverageWaitTime()) +
"\n" +
"Average turn around time: " +
std::to_string(result.getAverageTurnAroundTime()) +
"\n\n");
end();
}
};
#endif // SOCKETPROCESSSENDER_H