/[H7]/branches/linux-serial/server/serverthread.cpp
ViewVC logotype

Annotation of /branches/linux-serial/server/serverthread.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 68 - (hide annotations) (download)
Wed Feb 7 00:14:52 2007 UTC (17 years, 3 months ago) by torben
File size: 2659 byte(s)
Intermediate commit


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 torben 68 do
70 torben 67 {
71 torben 68 if ( str == "100")
72     {
73     std::ostringstream tmp;
74 torben 67
75 torben 68 if (store->led3)
76     tmp << "111|";
77     else
78     tmp << "110|";
79 torben 67
80 torben 68 if (store->led4)
81     tmp << "121|";
82     else
83     tmp << "120|";
84    
85     if (store->led5)
86     tmp << "131|";
87     else
88     tmp << "130|";
89 torben 67
90 torben 68 if (store->switch2)
91     tmp << "141|";
92     else
93     tmp << "140|";
94 torben 67
95 torben 68 if (store->switch3)
96     tmp << "151|";
97     else
98     tmp << "150|";
99 torben 67
100 torben 68 tmp << ( store->temp + 4000) << "|";
101     tmp << ( store->potmeter+2000) << "|";
102     conn << tmp.str();
103     } else {
104    
105     boost::mutex::scoped_lock lock(store->writeMutex);
106     if (str == "111")
107     store->commands.push( WriteCommand(0,1) );
108     else if (str == "110")
109     store->commands.push( WriteCommand(0,0) );
110     else if (str == "121")
111     store->commands.push( WriteCommand(1,1) );
112     else if (str == "120")
113     store->commands.push( WriteCommand(1,0) );
114     else if (str == "131")
115     store->commands.push( WriteCommand(2,1) );
116     else if (str == "130")
117     store->commands.push( WriteCommand(2,0) );
118     }
119     } while (str.length
120 torben 67 }
121     str = "";
122     }
123     Sleep(2);
124    
125     }
126    
127    
128     mStopped = true;
129     }

  ViewVC Help
Powered by ViewVC 1.1.20