void Gatekeeper::Handle() { std::stringstream r(message_); std::string s; r >> s; if( iequals( s, "GIV" ) ) { std::getline(r, s); const std::size_t pos = s.find(':'); std::string hex = s.substr(pos + 1, 2 * 16); GUID guid; assert(hex.size() == 32); Conv::Hex::Decode(hex.begin(), hex.end(), guid.begin()); std::vector< DownloadPtr > v; System::GetDownloadMgr()->Dump(std::back_inserter(v)); for(uint i = 0; i < v.size(); ++i) if(v[i]->HandleGIV(this, guid)) break; return; } else if(iequals(s, "GET")) { if(message_.find("\r\n\r\n") == std::string::npos) throw MessageIncomplete(); System::GetUploadMgr()->Accept(this); } else throw std::runtime_error("Unhandled"); }
void Gatekeeper::OnConnected(const bs::error_code& err) { if(closed_) return; if(!err) { System::LogBas() << "Connected by push to " << endpoint_ << std::endl; std::string greet = "GIV 0:"; const GUID guid = System::Guid(); Conv::Hex::Encode(guid.begin(), guid.end(), std::back_inserter(greet)); greet += "/\r\n"; buffer_.assign(greet.begin(), greet.end()); ba::async_write(*pSock_, ba::buffer(buffer_), boost::bind(&Gatekeeper::OnGreetingSent, shared_from_this(), _1)); } else if(err != ba::error::operation_aborted) { System::LogDev() << "Gatekeeper::OnConnected " << endpoint_ << " error => " << err.message() << std::endl; DetachMe(); } }
GUID GUID::operator + (const GUID& num) { GUID temp; u16 sum = 0; u8 remain = 0; for (int i = 0; i < 7; ++i) { sum = this->getByte(i) + num.getByte(i) + remain; //add digits and the previous digits remainder remain = sum / 256; //new remainder temp[i] = sum % 256; //new digit } return temp; }
void Cache::refresh( void ) { { os::Synchronized sync( _localMutex ); { os::Synchronized sync2( _toUpdateMutex ); for( byteBuffersIter_t it = _toUpdate.begin(); it != _toUpdate.end(); ++it ) { io::ByteBuffer * update = *it; GUID id = GUID ::unserialize( *update ); ClassID classId = ClassID::unserialize( *update ); localIter_t itemIter = _local.find( id ); if( itemIter == _local.end()) { Shareable * item = _participant.newInstance( classId, *update ); if( item ) { item->_id.set( id ); _local[id] = item; } else { fprintf( stderr, "Unknown %s of %s\n", classId.toString().c_str(), id.toString().c_str()); } } else if( ! _ownershipCheck || ! owns( id )) { itemIter->second->unserialize( *update ); } delete update; } _toUpdate.clear(); } { os::Synchronized sync3( _toDeleteMutex ); for( guidsIter_t it = _toDelete.begin(); it != _toDelete.end(); ++it ) { _local.erase( *it ); } _toDelete.clear(); } } }
int main(int argc, char* argv[]) { int iterations = 0; if (argc < 4) { print_usage(argv[0]); return 1; } int guid_num = atoi(argv[3]); if (argc > 4) { iterations = atoi(argv[4]); } //string server_addr_s("127.0.0.1:5001"); //string local_addr_s("192.168.1.1:3001"); string server_addr_s(argv[1]); string local_addr_s(argv[2]); NetAddr server_addr(NET_ADDR_TYPE_IPV4_PORT, server_addr_s); NetAddr local_addr(NET_ADDR_TYPE_IPV4_PORT, local_addr_s); try { //configure service endpoints Gnrs *gnrs = new Gnrs(server_addr, local_addr); GUID guid = GUID(); guid.init(guid_num); struct timeval t1, t2; gettimeofday(&t1, NULL); list<NetAddr> lkup_addrs = gnrs->lookup(guid); gettimeofday(&t2, NULL); long long delay = 1000000 * (t2.tv_sec - t1.tv_sec); delay += t2.tv_usec - t1.tv_usec; cout << lkup_addrs.size() << " result(s) on lookup for GUID: " << guid.to_int() << "( took " << delay << " us)" << endl; int num_addrs = 0; for (list<NetAddr>::const_iterator it = lkup_addrs.begin(); it != lkup_addrs.end(); it++) { num_addrs++; cout << "INFO: \t Addr #" << num_addrs << " : " << (*it).get_value() << endl; } delete(gnrs); if (!iterations) return 0; long long total_delay = 0; vector<long long>delay_v; for(int i = 0; i < iterations; i++) { Gnrs *gnrs_heap = new Gnrs(server_addr, local_addr); //lookup operation gettimeofday(&t1, NULL); lkup_addrs = gnrs_heap->lookup(guid); gettimeofday(&t2, NULL); delay = 1000000 * (t2.tv_sec - t1.tv_sec); delay += t2.tv_usec - t1.tv_usec; total_delay += delay; delay_v.push_back(delay); delete(gnrs_heap); } /* assuming idempotent, results should remain same despite iterations */ int iter = 0; cout << "Iteration\tLatency(us)" << endl; for (vector<long long>::const_iterator it = delay_v.begin(); it != delay_v.end(); it++) { cout << ++iter << "\t" << *it << endl; } double avg_delay = total_delay / (double)(1000 * iterations); cout <<"INFO: " << "Performed " << iterations << " lookups for GUID '" << guid.to_int() << "' with avg latency of " << avg_delay << " ms" << endl; } catch (NetException &ne) { cerr << "ERROR: " << ne.what() << endl; } return 0; }
bool operator == (GUID const & lhs, GncGUID const & rhs) noexcept { auto ret = std::mismatch (lhs.begin (), lhs.end (), rhs.reserved); return ret.first == lhs.end (); }
bool operator== (const GUID& left, const GUID& right) { return left.inline_eq (right); }
GUID::GUID(const GUID &theOther) { for (int i = 0; i < 7; ++i) { guid_[i] = theOther.getByte (i); } }