1 |
|
2 |
#include "serverthread.h" |
3 |
#include "globalstorage.h" |
4 |
|
5 |
#include <list> |
6 |
#include <iostream> |
7 |
#include <sstream> |
8 |
|
9 |
using CPPSocket::TCPServer; |
10 |
using CPPSocket::TCPConnection; |
11 |
using CPPSocket::Address; |
12 |
|
13 |
using std::cout; |
14 |
using std::endl; |
15 |
|
16 |
typedef std::list<TCPConnection*>::iterator ClientIt; |
17 |
ServerThread::ServerThread() |
18 |
: Thread() |
19 |
{ |
20 |
} |
21 |
|
22 |
ServerThread::~ServerThread() |
23 |
{ |
24 |
for (ClientIt it = mClients.begin(); it != mClients.end(); it++) |
25 |
{ |
26 |
(*it)->disconnect(); |
27 |
delete (*it); |
28 |
} |
29 |
} |
30 |
|
31 |
void ServerThread::parsePackage(std::string str) |
32 |
{ |
33 |
if ( str == "100") |
34 |
{ |
35 |
std::ostringstream tmp; |
36 |
|
37 |
if (store->led3) |
38 |
tmp << "111|"; |
39 |
else |
40 |
tmp << "110|"; |
41 |
|
42 |
if (store->led4) |
43 |
tmp << "121|"; |
44 |
else |
45 |
tmp << "120|"; |
46 |
|
47 |
if (store->led5) |
48 |
tmp << "131|"; |
49 |
else |
50 |
tmp << "130|"; |
51 |
|
52 |
if (store->switch2) |
53 |
tmp << "141|"; |
54 |
else |
55 |
tmp << "140|"; |
56 |
|
57 |
if (store->switch3) |
58 |
tmp << "151|"; |
59 |
else |
60 |
tmp << "150|"; |
61 |
|
62 |
tmp << ( store->temp + 4000) << "|"; |
63 |
tmp << ( store->potmeter+2000) << "|"; |
64 |
conn << tmp.str(); |
65 |
} else { |
66 |
|
67 |
boost::mutex::scoped_lock lock(store->writeMutex); |
68 |
if (str == "111") |
69 |
store->commands.push( WriteCommand(0,1) ); |
70 |
else if (str == "110") |
71 |
store->commands.push( WriteCommand(0,0) ); |
72 |
else if (str == "121") |
73 |
store->commands.push( WriteCommand(1,1) ); |
74 |
else if (str == "120") |
75 |
store->commands.push( WriteCommand(1,0) ); |
76 |
else if (str == "131") |
77 |
store->commands.push( WriteCommand(2,1) ); |
78 |
else if (str == "130") |
79 |
store->commands.push( WriteCommand(2,0) ); |
80 |
} |
81 |
} |
82 |
|
83 |
void ServerThread::operator()() |
84 |
{ |
85 |
mStopped = false; |
86 |
|
87 |
TCPServer& server = GlobalStorage::instance()->server; |
88 |
|
89 |
while(mContinue) |
90 |
{ |
91 |
if (server.wait4Connection(0,1500)) |
92 |
{ |
93 |
Address addr; |
94 |
mClients.push_back( server.getConnection(addr) ); |
95 |
std::cout << "Connection from " << addr.getIPString() << ":" |
96 |
<< addr.getPort() << std::endl; |
97 |
|
98 |
for (ClientIt it = mClients.begin(); it != mClients.end(); it++) |
99 |
{ |
100 |
if ( ! (*it)->isConnected() ) |
101 |
{ |
102 |
std::cout << "removing dead connection" << std::endl; |
103 |
delete (*it); |
104 |
mClients.erase(it); |
105 |
it = mClients.begin(); |
106 |
} |
107 |
} |
108 |
} |
109 |
|
110 |
for (ClientIt it = mClients.begin(); it != mClients.end(); it++) |
111 |
{ |
112 |
std::string str; |
113 |
TCPConnection& conn = *(*it); |
114 |
GlobalStorage* store = GlobalStorage::instance(); |
115 |
SerialThread& serial = store->serialThread; |
116 |
|
117 |
conn >> str; |
118 |
if (str.size() >0) |
119 |
{ |
120 |
cout << "Got " << str.size() << " bytes :" << str << endl; |
121 |
do |
122 |
{ |
123 |
} while (str.length |
124 |
} |
125 |
str = ""; |
126 |
} |
127 |
Sleep(2); |
128 |
|
129 |
} |
130 |
|
131 |
|
132 |
mStopped = true; |
133 |
} |