-
Notifications
You must be signed in to change notification settings - Fork 1
/
Server.cpp
116 lines (90 loc) · 1.96 KB
/
Server.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
#include "Server.h"
#include "ServerSocket.h"
#include "SocketException.h"
#include "JsonControl.h"
#include "NodeHandler.h"
#include "Actions.h"
#include "rapidjson/include/rapidjson/document.h"
#include "rapidjson/include/rapidjson/writer.h"
#include "rapidjson/include/rapidjson/stringbuffer.h"
#include <stdint.h>
#include <cstdio>
#include <iostream>
#include <stdlib.h>
#include <thread>
#include <mutex>
NodeHandler node;
std::mutex NodeAccessMutex;
Server::Server()
{
RunServer();
}
void dataHandlerThread(std::string data)
{
SwitchDataPacket packet;
const char * c = data.c_str();
//JsonControl jsn;
//jsn.DecodeJsonObject(c);
rapidjson::Document doc;
doc.Parse(c);
uint64_t x = doc["nodeid"].GetUint64();
std::cout << x << std::endl;
node.SetWritingPipe(x);
switch ( doc["action"].GetUint() )
{
case Actions::SWITCH:
packet.state = doc["state"].GetUint();
NodeAccessMutex.lock();
node.WriteData(&packet,sizeof(packet)) != true;
NodeAccessMutex.unlock();
break;
default:
// Code
break;
}
//std::cout << data << std::endl;
}
void newServerThread()
{
std::cout << "running....\n";
system("fuser 30000/tcp -k");
node.InitRadio();
for(;;)
{
try
{
// Create the socket
ServerSocket server ( 30000 );
while ( true )
{
ServerSocket new_sock;
server.accept ( new_sock );
try
{
while ( true )
{
std::string data;
std::string send = "{\"sent\":true}";
new_sock >> data;
//new_sock << send;
std::thread t2(dataHandlerThread, data);
t2.detach();
}
}
catch ( SocketException& ) {}
}
}
catch ( SocketException& e )
{
std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";
}
}
}
void Server::RunServer()
{
std::thread t1(newServerThread);
t1.join();
}
Server::~Server()
{
}