1 |
torben |
67 |
|
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::operator()() |
32 |
|
|
{ |
33 |
|
|
mStopped = false; |
34 |
|
|
|
35 |
|
|
TCPServer& server = GlobalStorage::instance()->server; |
36 |
|
|
|
37 |
|
|
while(mContinue) |
38 |
|
|
{ |
39 |
|
|
if (server.wait4Connection(0,1500)) |
40 |
|
|
{ |
41 |
|
|
Address addr; |
42 |
|
|
mClients.push_back( server.getConnection(addr) ); |
43 |
|
|
std::cout << "Connection from " << addr.getIPString() << ":" |
44 |
|
|
<< addr.getPort() << std::endl; |
45 |
|
|
|
46 |
|
|
for (ClientIt it = mClients.begin(); it != mClients.end(); it++) |
47 |
|
|
{ |
48 |
|
|
if ( ! (*it)->isConnected() ) |
49 |
|
|
{ |
50 |
|
|
std::cout << "removing dead connection" << std::endl; |
51 |
|
|
delete (*it); |
52 |
|
|
mClients.erase(it); |
53 |
|
|
it = mClients.begin(); |
54 |
|
|
} |
55 |
|
|
} |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
for (ClientIt it = mClients.begin(); it != mClients.end(); it++) |
59 |
|
|
{ |
60 |
|
|
std::string str; |
61 |
|
|
TCPConnection& conn = *(*it); |
62 |
|
|
GlobalStorage* store = GlobalStorage::instance(); |
63 |
|
|
SerialThread& serial = store->serialThread; |
64 |
|
|
|
65 |
|
|
conn >> str; |
66 |
|
|
if (str.size() >0) |
67 |
|
|
{ |
68 |
|
|
cout << "Got " << str.size() << " bytes :" << str << endl; |
69 |
|
|
if ( str == "100") |
70 |
|
|
{ |
71 |
|
|
std::ostringstream tmp; |
72 |
|
|
|
73 |
|
|
if (store->led3) |
74 |
|
|
tmp << "111|"; |
75 |
|
|
else |
76 |
|
|
tmp << "110|"; |
77 |
|
|
|
78 |
|
|
if (store->led4) |
79 |
|
|
tmp << "121|"; |
80 |
|
|
else |
81 |
|
|
tmp << "120|"; |
82 |
|
|
|
83 |
|
|
if (store->led5) |
84 |
|
|
tmp << "131|"; |
85 |
|
|
else |
86 |
|
|
tmp << "130|"; |
87 |
|
|
|
88 |
|
|
if (store->switch2) |
89 |
|
|
tmp << "140|"; |
90 |
|
|
else |
91 |
|
|
tmp << "141|"; |
92 |
|
|
|
93 |
|
|
if (store->switch3) |
94 |
|
|
tmp << "150|"; |
95 |
|
|
else |
96 |
|
|
tmp << "151|"; |
97 |
|
|
|
98 |
|
|
tmp << ( store->temp + 4000) << "|"; |
99 |
|
|
tmp << ( store->potmeter+2000) << "|"; |
100 |
|
|
conn << tmp.str(); |
101 |
|
|
} else { |
102 |
|
|
|
103 |
|
|
boost::mutex::scoped_lock lock(store->writeMutex); |
104 |
|
|
if (str == "111") |
105 |
|
|
store->commands.push( WriteCommand(0,1) ); |
106 |
|
|
|
107 |
|
|
if (str == "110") |
108 |
|
|
store->commands.push( WriteCommand(0,0) ); |
109 |
|
|
|
110 |
|
|
if (str == "121") |
111 |
|
|
store->commands.push( WriteCommand(1,1) ); |
112 |
|
|
|
113 |
|
|
if (str == "120") |
114 |
|
|
store->commands.push( WriteCommand(1,0) ); |
115 |
|
|
|
116 |
|
|
if (str == "131") |
117 |
|
|
store->commands.push( WriteCommand(2,1) ); |
118 |
|
|
|
119 |
|
|
if (str == "130") |
120 |
|
|
store->commands.push( WriteCommand(2,0) ); |
121 |
|
|
} |
122 |
|
|
} |
123 |
|
|
str = ""; |
124 |
|
|
} |
125 |
|
|
Sleep(2); |
126 |
|
|
|
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
|
130 |
|
|
mStopped = true; |
131 |
|
|
} |