void MsgProcessor::append( msg_ptr msg ) { if( QThread::currentThread() != thread() ) { qDebug() << "reinvoking msgprocessor::append in correct thread, ie not" << QThread::currentThread(); QMetaObject::invokeMethod( this, "append", Qt::QueuedConnection, Q_ARG(msg_ptr, msg) ); return; } m_msgs.append( msg ); m_msg_ready.insert( msg.data(), false ); m_totmsgsize += msg->payload().length(); if( m_mode & NOTHING ) { //qDebug() << "MsgProcessor::NOTHING"; handleProcessedMsg( msg ); return; } QFuture<msg_ptr> fut = QtConcurrent::run(&MsgProcessor::process, msg, m_mode, m_threshold); QFutureWatcher<msg_ptr> * watcher = new QFutureWatcher<msg_ptr>; connect( watcher, SIGNAL( finished() ), this, SLOT( processed() ), Qt::QueuedConnection ); watcher->setFuture( fut ); }
void Connection::sendMsg( msg_ptr msg ) { if ( d_func()->do_shutdown ) { tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "SHUTTING DOWN, NOT SENDING msg flags:" << (int)msg->flags() << "length:" << msg->length() << id(); return; } d_func()->tx_bytes_requested += msg->length() + Msg::headerSize(); d_func()->msgprocessor_out.append( msg ); }
void Connection::sendMsg( msg_ptr msg ) { if( m_do_shutdown ) { qDebug() << Q_FUNC_INFO << "SHUTTING DOWN, NOT SENDING msg flags:" << (int)msg->flags() << "length:" << msg->length() << id(); return; } m_tx_bytes_requested += msg->length() + Msg::headerSize(); m_msgprocessor_out.append( msg ); }
void ControlConnection::handleMsg( msg_ptr msg ) { if ( msg->is( Msg::PING ) ) { // qDebug() << "Received Connection PING, nice." << m_pingtimer_mark.elapsed(); m_pingtimer_mark.restart(); return; } // if small and not compresed, print it out for debug if ( msg->length() < 1024 && !msg->is( Msg::COMPRESSED ) ) { qDebug() << id() << "got msg:" << QString::fromLatin1( msg->payload() ); } // All control connection msgs are JSON if ( !msg->is( Msg::JSON ) ) { Q_ASSERT( msg->is( Msg::JSON ) ); markAsFailed(); return; } QVariantMap m = msg->json().toMap(); if ( !m.isEmpty() ) { if ( m.value( "conntype" ).toString() == "request-offer" ) { QString theirkey = m["key"].toString(); QString ourkey = m["offer"].toString(); QString theirdbid = m["controlid"].toString(); servent()->reverseOfferRequest( this, theirdbid, ourkey, theirkey ); } else if ( m.value( "method" ).toString() == "dbsync-offer" ) { m_dbconnkey = m.value( "key" ).toString() ; setupDbSyncConnection(); } else if ( m.value( "method" ) == "protovercheckfail" ) { qDebug() << "*** Remote peer protocol version mismatch, connection closed"; shutdown( true ); return; } else { tDebug() << id() << "Unhandled msg:" << QString::fromLatin1( msg->payload() ); } return; } tDebug() << id() << "Invalid msg:" << QString::fromLatin1( msg->payload() ); }
void MsgProcessor::handleProcessedMsg( msg_ptr msg ) { Q_ASSERT( QThread::currentThread() == thread() ); m_msg_ready.insert( msg.data(), true ); while( !m_msgs.isEmpty() ) { if( m_msg_ready.value( m_msgs.first().data() ) ) { msg_ptr m = m_msgs.takeFirst(); m_msg_ready.remove( m.data() ); //qDebug() << Q_FUNC_INFO << "totmsgsize:" << m_totmsgsize; emit ready( m ); } else { return; } } //qDebug() << Q_FUNC_INFO << "EMPTY, no msgs left."; emit empty(); }
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - vector<Resource*> SourceFacility::removeResource(msg_ptr msg) { Transaction trans = msg->trans(); double sent_amt = 0; // pull materials off of the inventory stack until you get the trans amount // start with an empty manifest vector<Resource*> toSend; while (trans.resource->quantity() > (sent_amt+EPS_KG) && !inventory_.empty() ) { Material* m = inventory_.front(); // if the inventory obj isn't larger than the remaining need, send it as is. if (m->quantity() <= (trans.resource->quantity() - sent_amt)) { sent_amt += m->quantity(); toSend.push_back(m); inventory_.pop_front(); LOG(LEV_DEBUG2) <<"SourceFacility "<< ID() <<" has decided not to split the object with size : "<< m->quantity(); } else if (m->quantity() > (trans.resource->quantity() - sent_amt)) { // if the inventory obj is larger than the remaining need, split it. Material* mat_to_send = m->extract(trans.resource->quantity() - sent_amt); sent_amt += mat_to_send->quantity(); LOG(LEV_DEBUG2) <<"SourceFacility "<< ID() <<" has decided to split the object to size : "<< mat_to_send->quantity(); toSend.push_back(mat_to_send); printSent(mat_to_send); } } return toSend; }
FakeConverterMarket() : ConverterMarket() { string kg = "kg"; string qual = "qual"; gen_rsrc_ptr res = gen_rsrc_ptr(new GenericResource(kg, qual, 1)); res->setOriginatorID(1); msg_ = msg_ptr(new Message(this)); msg_->setResource(res); }
FakeConverterMarket() : ConverterMarket() { string kg = "kg"; string qual = "qual"; gen_rsrc_ptr res = gen_rsrc_ptr(new GenericResource(kg, qual, 1)); Transaction trans(this, OFFER); msg_ = msg_ptr(new Message(this, this, trans)); msg_->trans().setResource(res); }
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void NullFacility::receiveMessage(msg_ptr msg) { // is this a message from on high? if (msg->supplier()==this) { // file the order ordersWaiting_.push_front(msg); } else { throw CycException("NullFacility is not the supplier of this msg."); } }
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - vector<rsrc_ptr> ConditioningFacility::removeResource(msg_ptr order) { vector<rsrc_ptr> toRet = vector<rsrc_ptr>() ; Transaction trans = order->trans(); double order_amount = trans.resource->quantity()*trans.minfrac; if (remaining_capacity_ >= order_amount){ toRet = processOrder(order); } else { string msg; msg += "The ConditioningFacility has run out of processing capacity. "; msg += "The order requested by "; msg += order->requester()->name(); msg += " will not be sent."; LOG(LEV_DEBUG2, "none!") << msg; gen_rsrc_ptr empty = gen_rsrc_ptr(new GenericResource("kg","kg",0)); toRet.push_back(empty); } return toRet; }
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void BatchReactor::receiveMessage(msg_ptr msg) { // is this a message from on high? if(msg->trans().supplier()==this){ // file the order ordersWaiting_.push_front(msg); LOG(LEV_INFO5, "BReact") << name() << " just received an order."; } else { throw CycException("BatchReactor is not the supplier of this msg."); } }
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void RecipeReactor::addResource(msg_ptr msg, vector<rsrc_ptr> manifest) { // grab each material object off of the manifest // and move it into the stocks. for (vector<rsrc_ptr>::iterator thisMat=manifest.begin(); thisMat != manifest.end(); thisMat++) { LOG(LEV_DEBUG2, "RReact") <<"RecipeReactor " << ID() << " is receiving material with mass " << (*thisMat)->quantity(); stocks_.push_front(make_pair(msg->trans().commod, boost::dynamic_pointer_cast<Material>(*thisMat))); } }
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void ConditioningFacility::addResource(msg_ptr msg, vector<rsrc_ptr> manifest) { // Put the material received in the stocks // grab each material object off of the manifest // and move it into the stocks. for (vector<rsrc_ptr>::iterator thisMat=manifest.begin(); thisMat != manifest.end(); thisMat++) { LOG(LEV_DEBUG2, "none!") <<"ConditiondingFacility " << ID() << " is receiving material with mass " << (*thisMat)->quantity(); mat_rsrc_ptr mat = boost::dynamic_pointer_cast<Material>(*thisMat); stocks_.push_front(make_pair(msg->trans().commod, mat)); } };
void DBSyncConnection::handleMsg( msg_ptr msg ) { Q_ASSERT( !msg->is( Msg::COMPRESSED ) ); if ( m_state == FETCHING ) changeState( PARSING ); // "everything is synced" indicated by non-json msg containing "ok": if ( !msg->is( Msg::JSON ) && msg->is( Msg::DBOP ) && msg->payload() == "ok" ) { changeState( SYNCED ); // calc the collection stats, to updates the "X tracks" in the sidebar etc // this is done automatically if you run a dbcmd to add files. DatabaseCommand_CollectionStats* cmd = new DatabaseCommand_CollectionStats( m_source ); connect( cmd, SIGNAL( done( const QVariantMap & ) ), m_source.data(), SLOT( setStats( const QVariantMap& ) ), Qt::QueuedConnection ); Database::instance()->enqueue( QSharedPointer<DatabaseCommand>(cmd) ); return; }
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void SeparationsMatrixFacility::addResource(msg_ptr msg, vector<Resource*> manifest) { LOG(LEV_DEBUG2) << "Entered the addResource file "; // grab each material object off of the manifest // and move it into the stocks. for (vector<Resource*>::iterator thisMat=manifest.begin(); thisMat != manifest.end(); thisMat++) { LOG(LEV_DEBUG2) <<"SeparationsFacility " << ID() << " is receiving material with mass " << (*thisMat)->quantity(); stocks_.push_back(make_pair(msg->trans().commod, dynamic_cast<Material*>(*thisMat))); } }
void StreamConnection::handleMsg( msg_ptr msg ) { Q_ASSERT( msg->is( Msg::RAW ) ); if ( msg->payload().startsWith( "block" ) ) { int block = QString( msg->payload() ).mid( 5 ).toInt(); m_readdev->seek( block * BufferIODevice::blockSize() ); qDebug() << "Seeked to block:" << block; QByteArray sm; sm.append( QString( "doneblock%1" ).arg( block ) ); sendMsg( Msg::factory( sm, Msg::RAW | Msg::FRAGMENT ) ); QTimer::singleShot( 0, this, SLOT( sendSome() ) ); } else if ( msg->payload().startsWith( "doneblock" ) ) { int block = QString( msg->payload() ).mid( 9 ).toInt(); ( (BufferIODevice*)m_iodev.data() )->seeked( block ); m_curBlock = block; qDebug() << "Next block is now:" << block; } else if ( msg->payload().startsWith( "data" ) ) { m_badded += msg->payload().length() - 4; ( (BufferIODevice*)m_iodev.data() )->addData( m_curBlock++, msg->payload().mid( 4 ) ); } //qDebug() << Q_FUNC_INFO << "flags" << (int) msg->flags() // << "payload len" << msg->payload().length() // << "written to device so far: " << m_badded; if ( m_iodev && ( (BufferIODevice*)m_iodev.data() )->nextEmptyBlock() < 0 ) { m_allok = true; // tell our iodev there is no more data to read, no args meaning a success: ( (BufferIODevice*)m_iodev.data() )->inputComplete(); shutdown(); } }
void on_protobuf_msg(session_ptr& impl, const msg_ptr& message) { callback_container::mapped_type tmp; callbacks_.op_if(message->GetDescriptor(), [&tmp](const callback_container::value_type &val) { tmp = val.second; }); if( tmp != 0 ) { tmp->on_msg(impl, message); } else { default_handler_(std::ref(impl), std::cref(message)); } }
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - vector<rsrc_ptr> RecipeReactor::removeResource(msg_ptr msg) { Transaction trans = msg->trans(); double newAmt = 0; mat_rsrc_ptr m; mat_rsrc_ptr newMat; mat_rsrc_ptr toAbsorb; // start with an empty manifest vector<rsrc_ptr> toSend; // pull materials off of the inventory stack until you get the trans amount while (trans.resource->quantity() > newAmt && !inventory_.empty() ) { for (deque<Fuel>::iterator iter = inventory_.begin(); iter != inventory_.end(); iter ++){ // be sure to get the right commodity if (iter->first == trans.commod) { m = iter->second; // start with an empty material newMat = mat_rsrc_ptr(new Material()); // if the inventory obj isn't larger than the remaining need, send it as is. if (m->quantity() <= (trans.resource->quantity() - newAmt)) { newAmt += m->quantity(); newMat->absorb(m); inventory_.pop_front(); } else { // if the inventory obj is larger than the remaining need, split it. toAbsorb = m->extract(trans.resource->quantity() - newAmt); newAmt += toAbsorb->quantity(); newMat->absorb(toAbsorb); } toSend.push_back(newMat); LOG(LEV_DEBUG2, "RReact") <<"RecipeReactor "<< ID() <<" is sending a mat with mass: "<< newMat->quantity(); } } } return toSend; }
void Connection::sendMsg_now( msg_ptr msg ) { Q_ASSERT( QThread::currentThread() == thread() ); // Q_ASSERT( this->isRunning() ); if ( m_sock.isNull() || !m_sock->isOpen() || !m_sock->isWritable() ) { tDebug() << "***** Socket problem, whilst in sendMsg(). Cleaning up. *****"; shutdown( false ); return; } if ( !msg->write( m_sock.data() ) ) { //qDebug() << "Error writing to socket in sendMsg() *************"; shutdown( false ); return; } }
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - vector<rsrc_ptr> StorageFacility::removeResource(msg_ptr order) { Transaction trans = order->trans(); // it should be of incommod Commodity type if(trans.commod != incommod_){ throw CycException("StorageFacility can only send incommodity type materials."); } // pull materials off of the inventory_ stack until you get the trans amount Mass complete = 0; // start with an empty manifest vector<rsrc_ptr> toSend; while(trans.amount > complete && !inventory_.empty() ){ mat_rsrc_ptr m = inventory_.front(); // if the inventory_ obj isn't larger than the remaining need, send it as is. if(m->quantity() <= (capacity_ - complete)){ complete += m->quantity(); toSend.push_back(m); LOG(LEV_DEBUG2, "none!") <<"StorageFacility "<< getSN() <<" is sending a mat with mass: "<< m->quantity(); inventory_.pop_front(); } else { // if the inventory_ obj is larger than the remaining need, split it. // start with an empty material mat_rsrc_ptr newMat = mat_rsrc_ptr(new Material(CompMap(), m->getUnits(), m->getName(), 0, ATOMBASED); mat_rsrc_ptr toAbsorb = m->extractMass(capacity_ - complete); complete += toAbsorb->quantity(); newMat->absorb(toAbsorb); toSend.push_back(newMat); LOG(LEV_DEBUG2, "none!") <<"StorageFacility "<< getSN() <<" is sending a mat with mass: "<< newMat->quantity(); } } return toSend; }
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - std::vector<Resource*> SeparationsMatrixFacility::removeResource(msg_ptr msg) { Transaction trans = msg->trans(); double newAmt = 0; // pull materials off of the inventory stack until you get the trans amount // start with an empty manifest vector<Resource*> toSend; while(trans.resource->quantity() > newAmt && !inventory_.empty() ){ for (deque<InSep>::iterator iter = inventory_.begin(); iter != inventory_.end(); iter ++){ Material* m = inventory_.front().second; // start with an empty material Material* newMat = new Material(); // if the inventory obj isn't larger than the remaining need, send it as is. if(m->quantity() <= (trans.resource->quantity() - newAmt)){ newAmt += m->quantity(); newMat->absorb(m); inventory_.pop_front(); } else{ // if the inventory obj is larger than the remaining need, split it. Material* toAbsorb = m->extract(trans.resource->quantity() - newAmt); newAmt += toAbsorb->quantity(); newMat->absorb(toAbsorb); } toSend.push_back(newMat); LOG(LEV_DEBUG2) <<"SeparationsMatrixFacility "<< ID() <<" is sending a mat with mass: "<< newMat->quantity(); } } return toSend; }
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - vector<rsrc_ptr> ConditioningFacility::processOrder(msg_ptr order) { // Send material from inventory to fulfill transactions Transaction trans = order->trans(); double newAmt = 0; // pull materials off of the inventory stack until you get the transaction amount // start with an empty manifest vector<rsrc_ptr> toSend; while(trans.resource->quantity() > newAmt && !inventory_.empty() ) { mat_rsrc_ptr m = inventory_.front().second; // start with an empty material mat_rsrc_ptr newMat = mat_rsrc_ptr(new Material()); // if the inventory obj isn't larger than the remaining need, send it as is. if(m->quantity() <= (trans.resource->quantity() - newAmt)) { newAmt += m->quantity(); newMat->absorb(m); inventory_.pop_front(); remaining_capacity_ = remaining_capacity_ - newAmt; } else { // if the inventory obj is larger than the remaining need, split it. mat_rsrc_ptr toAbsorb = m->extract(trans.resource->quantity() - newAmt); newMat->absorb(toAbsorb); newAmt += toAbsorb->quantity(); remaining_capacity_ = remaining_capacity_ - newAmt; } toSend.push_back(newMat); LOG(LEV_DEBUG2, "none!") <<"ConditioningFacility "<< ID() <<" is sending a mat with mass: "<< newMat->quantity(); } return toSend; };
void EventSourceHandler::message_handler(connection_hdl hdl, msg_ptr p_msg) { cerr << "Received a message:\n" << endl; // Handle message hdl.lock().get(); std::istringstream msg(p_msg->get_payload()); boost::property_tree::ptree msg_obj; boost::property_tree::read_json(msg, msg_obj); string type = msg_obj.get<std::string>("type"); if (type == "request") { std::string what = msg_obj.get<std::string>("what"); if (what == "source_list") { SendServerListEvent(hdl); } } else if (type == "add_source") { string mcast_ip_address = msg_obj.get<std::string>("ip"); unsigned short mcast_port = msg_obj.get<unsigned short>("port"); string mcast_interface = msg_obj.get<std::string>("interface"); CreateStreamServer(mcast_ip_address, mcast_port, mcast_interface); BroadcastServerListEvent(); } // std::string response_string; // Send response - to single client // controlPort->send(hdl, response_string); }
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - std::vector<Resource*> NullFacility::removeResource(msg_ptr order) { Transaction trans = order->trans(); // it should be of out_commod_ commodity type if (trans.commod != out_commod_) { std::string err_msg = "NullFacility can only send '" + out_commod_ ; err_msg += + "' materials."; throw CycException(err_msg); } double newAmt = 0; // pull materials off of the inventory stack until you get the trans amount // start with an empty manifest vector<Resource*> toSend; while (trans.resource->quantity() > newAmt && !inventory_.empty() ) { Material* m = inventory_.front(); // if the inventory obj isn't larger than the remaining need, send it as is. if (m->quantity() <= (trans.resource->quantity() - newAmt)) { newAmt += m->quantity(); inventory_.pop_front(); } else { // if the inventory obj is larger than the remaining need, split it. Material* leftover = m->extract(trans.resource->quantity() - newAmt); newAmt += m->quantity(); inventory_.pop_front(); inventory_.push_back(leftover); } toSend.push_back(m); LOG(LEV_DEBUG2) <<"NullFacility "<< ID() <<" is sending a mat with mass: "<< m->quantity(); } return toSend; }
/// This method is run by QtConcurrent: msg_ptr MsgProcessor::process( msg_ptr msg, quint32 mode, quint32 threshold ) { // uncompress if needed if( (mode & UNCOMPRESS_ALL) && msg->is( Msg::COMPRESSED ) ) { qDebug() << "MsgProcessor::UNCOMPRESSING"; msg->m_payload = qUncompress( msg->payload() ); msg->m_length = msg->m_payload.length(); msg->m_flags ^= Msg::COMPRESSED; } // parse json payload into qvariant if needed if( (mode & PARSE_JSON) && msg->is( Msg::JSON ) && msg->m_json_parsed == false ) { qDebug() << "MsgProcessor::PARSING JSON"; bool ok; QJson::Parser parser; msg->m_json = parser.parse( msg->payload(), &ok ); msg->m_json_parsed = true; } // compress if needed if( (mode & COMPRESS_IF_LARGE) && !msg->is( Msg::COMPRESSED ) && msg->length() > threshold ) { qDebug() << "MsgProcessor::COMPRESSING"; msg->m_payload = qCompress( msg->payload(), 9 ); msg->m_length = msg->m_payload.length(); msg->m_flags |= Msg::COMPRESSED; } return msg; }
int game_service::handle_msg(msg_ptr msg, remote_socket_ptr from_sock) { msg_from_client<remote_socket_ptr>* pmsg = dynamic_cast<msg_from_client<remote_socket_ptr>*>(msg.get()); if (pmsg){ pmsg->from_sock_ = from_sock; return pmsg->handle_this(); } else return SYS_ERR_CANT_FIND_CONNECTION; }
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - vector<rsrc_ptr> SourceFacility::removeResource(msg_ptr msg) { Transaction trans = msg->trans(); return ResourceBuff::toRes(inventory_.popQty(trans.resource->quantity())); }