/[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 70 - (hide annotations) (download)
Wed Feb 7 08:03:48 2007 UTC (17 years, 4 months ago) by torben
File size: 2794 byte(s)
Split a tcp package into multiple requests


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

  ViewVC Help
Powered by ViewVC 1.1.20