void KeyValueStore::handleInsert(StreamSocket& client, const string& buf, Config::ThreadControl& control) { static const char sucResp = Protocol::REQ_SUC; static const char failResp = Protocol::REQ_FAIL; const char* ptr = buf.c_str(); while(*ptr != '\0' && *ptr != '\r' && *ptr != '\n' && *ptr != ' ') ptr++; if(*ptr != ' ') { printKv("Invalid request"); client.sendBytes(&failResp, sizeof(failResp)); return; // Invalid } string key(buf.substr(0, (ptr - buf.c_str()))); string data(buf.substr((ptr - buf.c_str()) + 1)); if(isResponsible(key, control)) { printKv("INSERT request received"); printKv("Received key: "<< key << endl << "Value: "<< data); memDB.insert(key, data); // Respond to client if(data.compare(memDB.lookup(key)) != 0) client.sendBytes(&failResp, sizeof(failResp)); else client.sendBytes(&sucResp, sizeof(sucResp)); KeyValueStore::doReplicate(Protocol::REPL_INS, buf, control); } else { pair<char, string> resp(KeyValueStore::forwardRequest(Protocol::INSERT, buf, control)); client.sendBytes(&resp.first, sizeof(resp.first)); } }
/** Public thread methods (clients connect here) */ void KeyValueStore::PublicThread::run() { Thread threads[MAX_CLIENT_THREADS]; HandleClient threadInst[MAX_CLIENT_THREADS]; const Config::ServerInformation& info = config.getServerInformation(); Config::ThreadControl& control = config.getThreadControl(); int id = 0; char full = Protocol::SRV_FULL; char conn = Protocol::SRV_CONN; ServerSocket server; SocketAddress sock(info.address, info.pubPort); server.bind(sock, true); server.listen(5); printKv("Listening for clients on "<< info.address <<":"<< info.pubPort); while(control.isLive()) { // Simply do thread per client StreamSocket client = server.acceptConnection(); printKv("Received client connection request - waiting for thread to free up"); // Wait five seconds try { freeThreads.wait(5000); // This beats busy waiting } catch(TimeoutException& notUsed(e)) { printKv("Server full - closing connection to client"); client.sendBytes(&full, sizeof(full)); client.close(); continue; } // Send success client.sendBytes(&conn, sizeof(conn)); // tryJoin() doesn't work properly in linux, using isRunning() instead // actively search for the next available thread while(threads[id].isRunning()){ // Try to get an available thread id = (id + 1) % MAX_CLIENT_THREADS; Thread::sleep(250); // 250ms between each check } printKv("Serving client"); threadInst[id] = HandleClient(client, control); threads[id].start(threadInst[id]); } server.close(); freeThreads.set(); // Free a thread with semaphore }
void EchoServer::run() { _ready.set(); Poco::Timespan span(250000); char* pBuffer = new char[_bufferSize]; while (!_stop) { if (_socket.poll(span, Socket::SELECT_READ)) { StreamSocket ss = _socket.acceptConnection(); try { int n = ss.receiveBytes(pBuffer, _bufferSize); while (n > 0 && !_stop) { ss.sendBytes(pBuffer, n); n = ss.receiveBytes(pBuffer, _bufferSize); } } catch (Poco::Exception& exc) { std::cerr << "EchoServer: " << exc.displayText() << std::endl; } } } delete pBuffer; }
void SocketTest::testSelect() { Timespan timeout(250000); EchoServer echoServer; StreamSocket ss; ss.connect(SocketAddress("localhost", echoServer.port())); Socket::SocketList readList; Socket::SocketList writeList; Socket::SocketList exceptList; readList.push_back(ss); assert (Socket::select(readList, writeList, exceptList, timeout) == 0); assert (readList.empty()); assert (writeList.empty()); assert (exceptList.empty()); ss.sendBytes("hello", 5); ss.poll(timeout, Socket::SELECT_READ); readList.push_back(ss); writeList.push_back(ss); assert (Socket::select(readList, writeList, exceptList, timeout) == 2); assert (!readList.empty()); assert (!writeList.empty()); assert (exceptList.empty()); char buffer[256]; int n = ss.receiveBytes(buffer, sizeof(buffer)); assert (n == 5); assert (std::string(buffer, n) == "hello"); ss.close(); }
void KeyValueStore::insertMovieDB(StreamSocket& client, const string& buf, Config::ThreadControl& control, bool forward) { vector<string> tmp; tmp.push_back(buf); string title(KeyValueStore::extractQuotes(buf)); vector<string> tokens(KeyValueStore::tokenize(title)); movieDB.lock(); for(vector<string>::iterator it = tokens.begin() ; it != tokens.end(); ++it) { if(!forward || isResponsible(*it, control)) { map<string, vector<string> >::iterator itt = movieDB.rawLookup(*it); if(itt == movieDB.rawEnd()) { // printKv("Insert movie"); movieDB.rawInsert(*it, tmp); } else { // printKv("Updated movie"); itt->second.push_back(buf); } } else { printKv("Forwarding movie"); pair<char, string> resp(KeyValueStore::forwardRequest(Protocol::INS_MOVIE, buf, control)); client.sendBytes(&resp.first, sizeof(resp.first)); } } movieDB.unlock(); }
void EchoServer::run() { _ready.set(); pi::Timespan span(250000); while (!_stop) { if (_socket.poll(span, Socket::SELECT_READ)) { StreamSocket ss = _socket.acceptConnection(); try { char buffer[256]; int n = ss.receiveBytes(buffer, sizeof(buffer)); while (n > 0 && !_stop) { ss.sendBytes(buffer, n); n = ss.receiveBytes(buffer, sizeof(buffer)); } } catch (pi::Exception& exc) { std::cerr << "EchoServer: " << exc.displayText() << std::endl; } } } }
void HTTPTestServer::run() { _ready.set(); Poco::Timespan span(250000); while (!_stop) { if (_socket.poll(span, Socket::SELECT_READ)) { StreamSocket ss = _socket.acceptConnection(); try { _lastRequest.clear(); char buffer[256]; int n = ss.receiveBytes(buffer, sizeof(buffer)); while (n > 0 && !_stop) { _lastRequest.append(buffer, n); if (!requestComplete()) n = ss.receiveBytes(buffer, sizeof(buffer)); else n = 0; } std::string response = handleRequest(); ss.sendBytes(response.data(), (int) response.size()); Poco::Thread::sleep(1000); } catch (Poco::Exception& exc) { std::cerr << "HTTPTestServer: " << exc.displayText() << std::endl; } } } }
void KeyValueStore::sendRequest(StreamSocket& sock, const pair<char, string>& req) { unsigned contentLength = req.second.size(); unsigned rem = contentLength; unsigned totalSent = 0; sock.sendBytes(&req.first, sizeof(req.first)); sock.sendBytes(&contentLength, sizeof(contentLength)); const char* msg = req.second.c_str(); while((totalSent += sock.sendBytes(msg, rem)) != contentLength) { msg = req.second.c_str() + totalSent; rem = contentLength - totalSent; } }
void KeyValueStore::handleLookup(StreamSocket& client, const string& buf, Config::ThreadControl& control) { static const char noLookup = Protocol::LOOKUP_NO; static const char scLookup = Protocol::LOOKUP_SUC; if(isResponsible(buf, control)) { string str(memDB.lookup(buf)); printKv("LOOKUP request received"); if(!str.empty()) { printKv("Handling LOOKUP"); unsigned contentLength = str.size(); client.sendBytes(&scLookup, sizeof(scLookup)); // Respond client.sendBytes(&contentLength, sizeof(contentLength)); // How much info client.sendBytes(str.c_str(), contentLength); } else { client.sendBytes(&noLookup, sizeof(noLookup)); } } else { printKv("Forwarding LOOKUP"); pair<char, string> resp(KeyValueStore::forwardRequest(Protocol::LOOKUP, buf, control)); client.sendBytes(&resp.first, sizeof(resp.first)); if(resp.first != Protocol::LOOKUP_NO && resp.first != Protocol::REQ_FAIL) { unsigned size = resp.second.size(); client.sendBytes(&size, sizeof(size)); client.sendBytes(resp.second.c_str(), size); } } }
void SocketTest::testFIFOBuffer() { Buffer<char> b(5); b[0] = 'h'; b[1] = 'e'; b[2] = 'l'; b[3] = 'l'; b[4] = 'o'; FIFOBuffer f(5, true); f.readable += delegate(this, &SocketTest::onReadable); f.writable += delegate(this, &SocketTest::onWritable); assert(0 == _notToReadable); assert(0 == _readableToNot); assert(0 == _notToWritable); assert(0 == _writableToNot); f.write(b); assert(1 == _notToReadable); assert(0 == _readableToNot); assert(0 == _notToWritable); assert(1 == _writableToNot); EchoServer echoServer; StreamSocket ss; ss.connect(SocketAddress("localhost", echoServer.port())); int n = ss.sendBytes(f); assert (n == 5); assert(1 == _notToReadable); assert(1 == _readableToNot); assert(1 == _notToWritable); assert(1 == _writableToNot); assert (f.isEmpty()); n = ss.receiveBytes(f); assert (n == 5); assert(2 == _notToReadable); assert(1 == _readableToNot); assert(1 == _notToWritable); assert(2 == _writableToNot); assert (f[0] == 'h'); assert (f[1] == 'e'); assert (f[2] == 'l'); assert (f[3] == 'l'); assert (f[4] == 'o'); f.readable -= delegate(this, &SocketTest::onReadable); f.writable -= delegate(this, &SocketTest::onWritable); ss.close(); }
void KeyValueStore::handleRemove(StreamSocket& client, const string& buf, Config::ThreadControl& control) { static const char sucResp = Protocol::REQ_SUC; static const char failResp = Protocol::REQ_FAIL; printKv("REMOVE request received"); if(isResponsible(buf, control)) { memDB.remove(buf); if(memDB.lookup(buf).empty()) client.sendBytes(&sucResp, sizeof(sucResp)); else client.sendBytes(&failResp, sizeof(failResp)); KeyValueStore::doReplicate(Protocol::REPL_REM, buf, control, control.getMachineCount()); } else { pair<char, string> resp(KeyValueStore::forwardRequest(Protocol::REMOVE, buf, control)); client.sendBytes(&resp.first, sizeof(resp.first)); } }
void SocketTest::testEcho() { EchoServer echoServer; StreamSocket ss; ss.connect(SocketAddress("localhost", echoServer.port())); int n = ss.sendBytes("hello", 5); assert (n == 5); char buffer[256]; n = ss.receiveBytes(buffer, sizeof(buffer)); assert (n == 5); assert (std::string(buffer, n) == "hello"); ss.close(); }
void SocketTest::testAvailable() { EchoServer echoServer; StreamSocket ss; ss.connect(SocketAddress("localhost", echoServer.port())); Timespan timeout(1000000); ss.sendBytes("hello", 5); char buffer[256]; assert (ss.poll(timeout, Socket::SELECT_READ)); int av = ss.available(); assert (av > 0 && av <= 5); int n = ss.receiveBytes(buffer, sizeof(buffer)); assert (n == 5); assert (std::string(buffer, n) == "hello"); ss.close(); }
int main() { StreamSocket sock; SocketAddress addr("70.79.74.121:80"); sock.connect(addr); const char* data="GET / HTTP/1.1\r\nHost: xa.us.to\r\n\r\n"; sock.sendBytes(data,strlen(data)); const int bs=4096; char* buf=new char[bs]; int br; while((br=sock.receiveBytes(buf,bs))>0) write(1,buf,br); }
void CSimpleSocket::test(){ using namespace Poco; using namespace Poco::Net; StreamSocket sk; bool connected = false; try{ sk.connect(SocketAddress("s1.goyou.cn", 80), Timespan(6, 0)); connected = true; }catch(Poco::Exception& exp){ std::cout << exp.displayText() << std::endl; } if (connected) { try{ UInt8 buffer[2048] = "GET / HTTP/1.0\r\nUser-Agent: Mozilla/5.0\r\n\r\n"; int nSendLen = strlen((char*)buffer); int nHasSendLen = 0; while (nHasSendLen != nSendLen) { int nLen = sk.sendBytes(buffer+nHasSendLen, nSendLen-nHasSendLen); nHasSendLen += nLen; } int nRecvLen = 0; do { Socket::SocketList readList, writeList, expList; readList.push_back(sk); Socket::select(readList, writeList, expList, Timespan(3, 0)); nRecvLen = 0; if (readList.size()) { nRecvLen = sk.receiveBytes(buffer, 1024); std::cout << nSendLen << " -- " << nRecvLen << std::endl; buffer[nRecvLen] = 0; std::cout << buffer << std::endl; } } while (nRecvLen>0); sk.close(); }catch(Poco::Exception& exp){ std::cout <<exp.displayText() << std::endl; } } }
void regTest2() { tracef("reg test 2 begin: reg send data."); StreamSocket ss; SocketAddress sa("127.0.0.1", 13333); ss.connect(sa, Timespan(3, 0)); DynamicStruct ds; ds["type"] = "request"; ds["action"] = "server.register"; DynamicStruct param; param["token"] = "1234567890"; param["uuid"] = "SC000000001"; ds["param"] = param; ss.sendBytes(ds.toString().c_str(), ds.toString().length()); tracef("reg data send: %s.", ds.toString().c_str()); ss.close(); tracef("socket closed."); tracef("register test 2 finished.\n"); }
void regTest3() { tracef("reg test 3 begin: reg send and receive data, register fail"); StreamSocket ss; SocketAddress sa("127.0.0.1", 13333); ss.connect(sa, Timespan(3, 0)); DynamicStruct ds; ds["type"] = "request"; ds["action"] = "server.register"; DynamicStruct param; param["token"] = "1234567890"; param["uuid"] = "SC000000001"; ds["param"] = param; ss.sendBytes(ds.toString().c_str(), ds.toString().length()); tracef("reg data send: %s.", ds.toString().c_str()); char buf[1024] = {0, }; ss.receiveBytes(buf, 1024); tracef("receive bytes: %s.", buf); tracef("socket closed."); tracef("register test 3 finished.\n"); }
void SocketTest::testPoll() { EchoServer echoServer; StreamSocket ss; ss.connect(SocketAddress("localhost", echoServer.port())); Stopwatch sw; sw.start(); Timespan timeout(1000000); assert (!ss.poll(timeout, Socket::SELECT_READ)); assert (sw.elapsed() >= 900000); sw.restart(); assert (ss.poll(timeout, Socket::SELECT_WRITE)); assert (sw.elapsed() < 100000); ss.sendBytes("hello", 5); char buffer[256]; sw.restart(); assert (ss.poll(timeout, Socket::SELECT_READ)); assert (sw.elapsed() < 100000); int n = ss.receiveBytes(buffer, sizeof(buffer)); assert (n == 5); assert (std::string(buffer, n) == "hello"); ss.close(); }
void LocalSocketTest::testSocketsPerformance() { Timestamp::TimeDiff local = 0, net = 0; std::size_t initBufSize = 1; std::size_t initReps = 1; bool noDelay[2] = { true, false }; std::cout << std::endl << "OS Name: " << Environment::osName() << std::endl; std::cout << "OS Version: " << Environment::osVersion() << std::endl; std::cout << "OS Architecture: " << Environment::osArchitecture() << std::endl; for (int d = 0; d < 2; ++d) { double locData = 0.0, locTime = 0.0, netData = 0.0, netTime = 0.0; std::ostringstream os; os << Environment::osName() << '-' << Environment::osVersion() << '-' << Environment::osArchitecture() << "-TCP"; if (noDelay[d]) os << "-nodelay"; os << ".csv"; File f(os.str()); if (f.exists()) f.remove(); FileOutputStream fos(os.str()); for (std::size_t repetitions = initReps; repetitions <= 100000; repetitions *= 10) { for (std::size_t bufSize = initBufSize; bufSize < 20000; bufSize *= 2) { char* pBuf = new char[bufSize]; { SocketAddress sas("/tmp/poco.server.tcp.sock"); EchoServer echoServer(sas, bufSize); SocketAddress sac("/tmp/poco.client.tcp.sock"); StreamSocket ss(sas, &sac); int recv = 0, sent = 0; Stopwatch sw; int i = 0; for (; i < repetitions; ++i) { sent = 0; recv = 0; local = 0; do { int s; sw.restart(); s = ss.sendBytes(pBuf + sent, bufSize - sent); sw.stop(); local += sw.elapsed(); sent += s; } while (sent < bufSize); do { int r; sw.restart(); r = ss.receiveBytes(pBuf + recv, bufSize - recv); sw.stop(); local += sw.elapsed(); recv += r; } while (recv < bufSize); locData += sent; locData += recv; locTime += local; poco_assert (sent == bufSize && recv == bufSize); } std::cout << "Local TCP socket, " << i << " repetitions, " << bufSize << " bytes, " << local << " [us]." << std::endl; ss.close(); } { SocketAddress sa("localhost", 12345); EchoServer echoServer(sa, bufSize); StreamSocket ss; ss.connect(SocketAddress(sa.host(), echoServer.port())); if (noDelay[d]) ss.setNoDelay(true); int recv = 0, sent = 0; Stopwatch sw; int i = 0; for (; i < repetitions; ++i) { sent = 0; recv = 0; net = 0; do { int s; sw.restart(); s = ss.sendBytes(pBuf + sent, bufSize - sent); sw.stop(); net += sw.elapsed(); sent += s; } while (sent < bufSize); do { int r; sw.restart(); r = ss.receiveBytes(pBuf + recv, bufSize - recv); sw.stop(); net += sw.elapsed(); recv += r; } while (recv < bufSize); netData += sent; netData += recv; netTime += net; poco_assert (sent == bufSize && recv == bufSize); } std::cout << "Network TCP socket, " << i << " repetitions, " << bufSize << " bytes, " << net << " [us]." << std::endl; fos << i << ',' << bufSize << ','; ss.close(); } delete pBuf; double ratio = ((double) net) / ((double) local); double diff = ((double) net) - ((double) local); std::cout << "Ratio: " << ratio << "(" << diff / 1000.0 << "[us/msg]" << std::endl; fos << ratio << std::endl; } } poco_assert (locData == netData); double locDTR = ((locData / (locTime / Timestamp::resolution())) * 8) / 1000000; double netDTR = ((netData / (netTime / Timestamp::resolution())) * 8) / 1000000; std::cout << (d ? "NO DELAY" : "DELAY") << std::endl << "=================================" << std::endl << "Local DTR: " << ((locData / (locTime / Timestamp::resolution())) * 8) / 1000000 << " [Mbit/s]" << std::endl << "Network DTR: " << ((netData / (netTime / Timestamp::resolution())) * 8) / 1000000 << " [Mbit/s]" << std::endl << "Local sockets speedup: " << ((locDTR / netDTR) * 100) - 100 << '%' << std::endl << "=================================" << std::endl; fos << "=================================" << std::endl << "Local DTR: " << ((locData / (locTime / Timestamp::resolution())) * 8) / 1000000 << " [Mbit/s]" << std::endl << "Network DTR: " << ((netData / (netTime / Timestamp::resolution())) * 8) / 1000000 << " [Mbit/s]" << std::endl << "Local sockets speedup: " << ((locDTR / netDTR) * 100) - 100 << '%' << std::endl << "=================================" << std::endl; fos.close(); } }
int testOption(char option, const string userId = "", const string passwd = "", const string filename = "") { Buffer buffer; int code = option; int len = 0; int totalLen = 0; buffer.append((const char*)&code, 1); totalLen = totalLen + 1; len = userId.length(); buffer.append((const char*)&len, 4); buffer.append(userId); totalLen = totalLen + 4 + len; if (code == addUser || code == updateUser) { len = passwd.length(); buffer.append((const char*)&len, 4); buffer.append(passwd); totalLen = totalLen + 4 + len; std::ifstream ifs(filename.c_str(), std::ifstream::binary | std::ifstream::in); if (ifs.is_open()) { if (ifs.good()) { ifs.seekg(0, ifs.end); len = ifs.tellg(); ifs.seekg(0, ifs.beg); unsigned char bin[FINGER_LEN] = { 0 }; buffer.append((const char*)&len, 4); ifs.read((char*)bin, len); buffer.append((const char*)bin, len); totalLen = totalLen + 4 + len; } } else { cout << "finger file " << filename << " is not OK!" << endl; return -2; } } Buffer sendBuf; sendBuf.append((const char*)&totalLen, 4); sendBuf.append(buffer.retrieveAllAsString()); int sendLen = sendBuf.readableBytes(); ss.sendBytes(sendBuf.retrieveAllAsString().c_str(), sendLen); char buf[2048] = { 0 }; int n = ss.receiveBytes(buf, sizeof(buf)); std::copy((char*)buf, (char*)(buf + 4), (char*)&totalLen); std::copy((char*)(buf + 4), (char*)(buf + 5), &code); cout << "totalLen: " << totalLen << ", code: " << (int)code << endl; if (option == queryUserInfo) { int useridlen = 0; int lenPlus = 5; std::copy((char*) (buf + lenPlus), (char*) (buf + lenPlus + 4), (char*) &useridlen); string userid; userid.resize(useridlen); lenPlus = 9; std::copy((char*) (buf + lenPlus), (char*) (buf + lenPlus + useridlen), &(*userid.begin())); int passwdlen = 0; lenPlus = 5 + 4 + useridlen; std::copy((char*) (buf + lenPlus), (char*) (buf + lenPlus + 4), (char*) &passwdlen); string passwd; lenPlus = 5 + 4 + useridlen + 4; passwd.resize(passwdlen); std::copy((char*) (buf + lenPlus), (char*) (buf + lenPlus + passwdlen), &(*passwd.begin())); int fingerLen = 0; lenPlus = 5 + 4 + useridlen + 4 + passwdlen; std::copy((char*) (buf + lenPlus), (char*) (buf + lenPlus + 4), (char*) &fingerLen); char finger[2048] = {0}; lenPlus = 5 + 4 + useridlen + 4 + passwdlen + 4; std::copy((char*) (buf + lenPlus), (char*) (buf + lenPlus + fingerLen), (char*) finger); cout << "useridlen: " << useridlen << ", userid: " << userid << ", passwdlen: " << passwdlen << ", passwd: " << passwd << ", fingerLen" << fingerLen << endl; unsigned char bin[FINGER_LEN] = { 0 }; std::ifstream ifs(filename.c_str(), std::ifstream::binary | std::ifstream::in); if (ifs.is_open()) { if (ifs.good()) { ifs.seekg(0, ifs.end); len = ifs.tellg(); ifs.seekg(0, ifs.beg); ifs.read((char*) bin, len); } } if (memcmp(bin, finger, fingerLen ) == 0) cout << "finger data ok!" << endl; } else if (option == queryAllUserInfo) { int userLen1 = 0; std::copy((char*) (buf + 5), (char*) (buf + 9), (char*)&userLen1); char user1[32] = {0}; std::copy((char*) (buf + 9), (char*) (buf + 9 + userLen1), user1); int userLen2 = 0; std::copy((char*) (buf + 9 + userLen1), (char*) (buf + 13 + userLen1), (char*) &userLen2); char user2[32] = {0}; std::copy((char*) (buf + 13 + userLen1), (char*) (buf + 13 + userLen1 + userLen2), user2); cout << "user1: " << user1 << ", user2" << user2 << endl; } return 0; }
void regTest5() { tracef("reg test 5 begin:reg twice"); Context::Ptr pContext = new Context(Context::TLSV1_CLIENT_USE, "", Context::VERIFY_NONE); SecureStreamSocket sss(pContext); SocketAddress sa("127.0.0.1", 12222); sss.connect(sa, Timespan(3, 0)); DynamicStruct ds; ds["type"] = "request"; ds["action"] = "server.token"; Timestamp t; UInt64 tms = t.epochMicroseconds(); char tms_str[32]; snprintf(tms_str, 31, "%llu", tms); std::string key = "alpha2015"; key += tms_str; MD5Engine md5; md5.update(key); const DigestEngine::Digest& digest = md5.digest(); std::string md5key = DigestEngine::digestToHex(digest); DynamicStruct param; param["key"] = md5key; param["timestamp"] = tms_str; param["dev_name"] = "lock3"; param["dev_type"] = "sc-01"; param["uuid"] = "SC00000003"; ds["param"] = param; tracef("data send: %s.", ds.toString().c_str()); sss.sendBytes(ds.toString().c_str(), ds.toString().length()); char buf[1024] = {0, }; sss.receiveBytes(buf, 1024); JSON::Parser parser; Dynamic::Var var = parser.parse(buf); JSON::Object::Ptr pObj = var.extract<JSON::Object::Ptr>(); DynamicStruct ds_recv = *pObj; std::string token = ds_recv["param"]["token"]; DynamicStruct ds_reg; ds_reg["type"] = "request"; ds_reg["action"] = "server.register"; DynamicStruct param_reg; param_reg["token"] = token; param_reg["uuid"] = "SC00000003"; ds_reg["param"] = param_reg; StreamSocket ss; SocketAddress sa2("127.0.0.1", 13333); ss.connect(sa2, Timespan(3, 0)); ss.sendBytes(ds_reg.toString().c_str(), ds_reg.toString().length()); tracef("reg data send: %s.", ds_reg.toString().c_str()); memset(buf, 0, 1024); ss.receiveBytes(buf, 1024); tracef("receive bytes: %s.", buf); ss.close(); Thread::sleep(2000); StreamSocket ss2; ss2.connect(sa2, Timespan(3, 0)); tracef("reg data send again: %s.", ds_reg.toString().c_str()); ss2.sendBytes(ds_reg.toString().c_str(), ds_reg.toString().length()); memset(buf, 0, 1024); ss2.receiveBytes(buf, 1024); tracef("recv data: %s.", buf); ss2.close(); tracef("reg test 5 finished."); }
void KeyValueStore::handleLookupMovie(StreamSocket& client, const string& buf, Config::ThreadControl& control) { static const char suc = Protocol::LOOKUP_SUC; static const char no = Protocol::LOOKUP_NO; static const char done = Protocol::MLOOKUP_DONE; string ret; unsigned sent = 0; const char* msg = NULL; map<string, bool> isSent; printKv("Received movie lookup request"); if(isResponsible(buf, control)) { movieDB.lock(); map<string, vector<string> >::iterator it = movieDB.rawLookup(buf); printKv("Hi, I'm a server and I am serving this request: " << buf); if(it == movieDB.rawEnd()) { client.sendBytes(&no, sizeof(no)); } else { vector<string>::iterator vit = it->second.begin(); for(; vit != it->second.end(); ++vit) { if(!isSent[*vit]) { ret.append(*vit); ret.append("\n"); isSent[*vit] = true; } } client.sendBytes(&suc, sizeof(suc)); unsigned length = ret.size(); unsigned rem = length; unsigned totalSent = 0; client.sendBytes(&length, sizeof(length)); msg = ret.c_str(); printKv("Sending..."); while((totalSent += client.sendBytes(msg, rem)) != length) { msg = ret.c_str() + totalSent; rem = length - totalSent; printKv("Remaining: "<< rem); } //client.sendBytes(&done, sizeof(done)); printKv("Done sending"); } movieDB.unlock(); } else { //vector<pair<char, string> > resps(KeyValueStore::forwardRequest(Protocol::LUP_MOVIE, buf, control)); pair<char, string> resps(KeyValueStore::forwardRequest(Protocol::LUP_MOVIE, buf, control)); #if 0 vector<pair<char, string> >::iterator it = resps.begin(); for(; it != resps.end() ; ++it) { if(it->first == Protocol::LOOKUP_SUC) { client.sendBytes(&it->first, sizeof(it->first)); unsigned length = it->second.size(); client.sendBytes(&length, sizeof(length)); //it++; client.sendBytes(it->second.c_str(), length); printKv("Protocol::LOOKUP_SUC"); } else { client.sendBytes(&it->first, sizeof(it->first)); if(it->first == Protocol::LOOKUP_NO) printKv("Protocol::LOOKUP_NO"); else if(it->first == Protocol::MLOOKUP_DONE) printKv("Protocol::MLOOKUP_DONE"); } } #else string str(resps.second); //printKv(str); unsigned length = str.size(); unsigned rem = length; unsigned totalSent = 0; const char* msg = str.c_str(); client.sendBytes(&resps.first, sizeof(resps.first)); if(resps.first == Protocol::LOOKUP_SUC) { client.sendBytes(&length, sizeof(length)); while((totalSent += client.sendBytes(msg, rem)) != length) { msg = str.c_str() + totalSent; rem = length - totalSent; printKv("Remaining: "<< rem); } } #endif } printKv("MovieDB size: "<< movieDB.size()); }
int main(int argc, char** argv) { if (argc > 1) serverAddr = string(argv[1]); if (argc > 2) port = atoi(argv[2]); if (argc > 3) filename = argv[3]; Buffer buffer; int len = 0; int totalLen = 0; char code = validateUser; buffer.append((const char*)&code, 1); totalLen = totalLen + 1; std::ifstream ifs(filename.c_str(), std::ifstream::binary | std::ifstream::in); if (ifs.is_open()) { if (ifs.good()) { ifs.seekg(0, ifs.end); len = ifs.tellg(); ifs.seekg(0, ifs.beg); buffer.append((const char*)&len, 4); unsigned char bin[FINGER_LEN] = { 0 }; ifs.read((char*)bin, len); buffer.append((const char*)bin, len); totalLen = totalLen + 4 + len; } } else { cout << "finger file " << filename << " is not OK!" << endl; return -2; } Buffer sendBuf; sendBuf.append((const char*)&totalLen, 4); sendBuf.append(buffer.retrieveAllAsString()); int sendLen = sendBuf.readableBytes(); StreamSocket ss; try { ss.connect(SocketAddress(serverAddr, port)); } catch (Poco::Exception& exc) { cerr << "Connect server failed, err: " << exc.displayText() << endl; return -1; } ss.sendBytes(sendBuf.retrieveAllAsString().c_str(), sendLen); char buf[FINGER_LEN] = { 0 }; int n = ss.receiveBytes(buf, sizeof(buf)); std::copy(buf, buf + 4, (char*)&totalLen); std::copy(buf + 4, buf + 5, &code); if (code == 0) { char *p = buf; p += 5; char tmp[32] = { 0 }; int nameIdLen = 0; std::copy(p, (char*)(p + 4), (char*)&nameIdLen); p += 4; string nameId; nameId.resize(nameIdLen); std::copy(p, (char*)(p + nameIdLen), nameId.begin()); p += nameIdLen; memset(tmp, 0, 32); int passwdLen = 0; std::copy(p, p + 4, (char*)&passwdLen); p += 4; string passwd; passwd.resize(passwdLen); std::copy(p, (char*)(p + passwdLen), passwd.begin()); cout << "nameId: " << nameId << ", passwd: " << passwd << endl; } cout << "totalLen: " << totalLen << ", code: " << (int)code << endl; //char c(' '); //while (c != 'q' && c != 'Q') //{ // cout << "Press q then enter to quit: \n"; // cin >> c; //} return 0; }