#include "serverthread.h" #include "globalstorage.h" #include #include #include using CPPSocket::TCPServer; using CPPSocket::TCPConnection; using CPPSocket::Address; using std::cout; using std::endl; typedef std::list::iterator ClientIt; ServerThread::ServerThread() : Thread() { } ServerThread::~ServerThread() { for (ClientIt it = mClients.begin(); it != mClients.end(); it++) { (*it)->disconnect(); delete (*it); } } void ServerThread::parsePackage(std::string str) { if ( str == "100") { std::ostringstream tmp; if (store->led3) tmp << "111|"; else tmp << "110|"; if (store->led4) tmp << "121|"; else tmp << "120|"; if (store->led5) tmp << "131|"; else tmp << "130|"; if (store->switch2) tmp << "141|"; else tmp << "140|"; if (store->switch3) tmp << "151|"; else tmp << "150|"; tmp << ( store->temp + 4000) << "|"; tmp << ( store->potmeter+2000) << "|"; conn << tmp.str(); } else { boost::mutex::scoped_lock lock(store->writeMutex); if (str == "111") store->commands.push( WriteCommand(0,1) ); else if (str == "110") store->commands.push( WriteCommand(0,0) ); else if (str == "121") store->commands.push( WriteCommand(1,1) ); else if (str == "120") store->commands.push( WriteCommand(1,0) ); else if (str == "131") store->commands.push( WriteCommand(2,1) ); else if (str == "130") store->commands.push( WriteCommand(2,0) ); } } void ServerThread::operator()() { mStopped = false; TCPServer& server = GlobalStorage::instance()->server; while(mContinue) { if (server.wait4Connection(0,1500)) { Address addr; mClients.push_back( server.getConnection(addr) ); std::cout << "Connection from " << addr.getIPString() << ":" << addr.getPort() << std::endl; for (ClientIt it = mClients.begin(); it != mClients.end(); it++) { if ( ! (*it)->isConnected() ) { std::cout << "removing dead connection" << std::endl; delete (*it); mClients.erase(it); it = mClients.begin(); } } } for (ClientIt it = mClients.begin(); it != mClients.end(); it++) { std::string str; TCPConnection& conn = *(*it); GlobalStorage* store = GlobalStorage::instance(); SerialThread& serial = store->serialThread; conn >> str; if (str.size() >0) { cout << "Got " << str.size() << " bytes :" << str << endl; do { } while (str.length } str = ""; } Sleep(2); } mStopped = true; }