void ServerNetworkComm::callback_send( TCPConnection::Ptr conn,
                                       const boost::system::error_code & error )
{
  if ( error )
    CFerror << "Data could not be sent to [" << conn->socket().remote_endpoint()
            << "]: " << error.message() << CFendl;
}
示例#2
0
文件: rstat.cpp 项目: ISP-SST/redux
void printPeerList( TcpConnection::Ptr conn, int ns ) {

    uint8_t cmd = CMD_PSTAT;
    boost::asio::write(conn->socket(),boost::asio::buffer(&cmd,1));

    uint64_t blockSize;
    shared_ptr<char> buf = conn->receiveBlock( blockSize );

    if ( !blockSize ) return;

    const char* ptr = buf.get();
    uint64_t count(0);
    try {
        Host peer;
        cout << "    " << peer.printHeader() << endl;
        int hostCount(0);
        while( count < blockSize ) {
            count += peer.unpack(ptr+count,conn->getSwapEndian());
            if ( hostCount == 0 ) cout << "    " << peer.print() << endl;
            else if ( ns==0 || hostCount < ns) cout << alignRight(to_string(hostCount),4) << peer.print() << endl;
            hostCount++;
        }
        if (ns!=0 && hostCount > ns) cout << "   :" << endl << alignRight(to_string(hostCount),4) << peer.print() << endl;
    } catch ( const exception& e) {
        cerr << "printPeerList: Exception caught while parsing block: " << e.what() << endl;
    }
    if( count != blockSize ) {
        cerr << "printPeerList: Parsing of datablock failed, count = " << count << "  blockSize = " << blockSize << "  bytes." << endl;
    }
}
  void init_accept()
  {
    TCPConnection::Ptr conn = TCPConnection::create( m_acceptor.get_io_service() );

    m_acceptor.async_accept( conn->socket(),
                             boost::bind( &Server::callback_accept,
                                          this,
                                          conn,// 1st param for the callback fct
                                          asio::placeholders::error ) ); // 2nd param
  }
void ServerNetworkComm::init_accept()
{
  TCPConnection::Ptr conn = TCPConnection::create( *m_io_service );

  m_acceptor->async_accept( conn->socket(),
                            boost::bind( &ServerNetworkComm::callback_accept,
                                         this,
                                         conn,
                                         asio::placeholders::error
                                       )
                           );
}
void ServerNetworkComm::callback_accept( TCPConnection::Ptr conn,
                                         const boost::system::error_code & error )
{
  if( !error )
  {
    // create client info
    ClientInfo& info = m_clients[conn];

    info.connection = conn;
    info.buffer = SignalFrame( "message", "cpath:/", "cpath:/" );
    info.error_handler = boost::shared_ptr<ErrorHandler>(new ErrorHandler());

    info.connection->set_error_handler( info.error_handler );

    CFinfo << "New client connected from " << conn->socket().remote_endpoint().address()
           << CFendl;

    init_read( info ); // init read operation for the new client
    init_accept();     // wait for other clients
  }
  else
    CFinfo << "Failed to accept a client connection: " << error.message() << CFendl;
}
void ServerNetworkComm::callback_read( TCPConnection::Ptr conn,
                                       const boost::system::error_code & error )
{
  if( !error )
  {
    std::string error_msg;
    ClientInfo& info = m_clients[conn];
    SignalFrame & buffer = info.buffer;

    std::string target = buffer.node.attribute_value( "target" );
    std::string receiver = buffer.node.attribute_value( "receiver" );
    std::string clientid = buffer.node.attribute_value( "clientid" );
    std::string frameid = buffer.node.attribute_value( "frameid" );

    // check if the client is attempting to register
    if( target == "client_registration" )
    {
      if( !info.uuid.empty() )
        error_msg = "This client has already been registered.";
      else
      {
        info.uuid = clientid;

        // Build and send the reply
        SignalFrame reply = buffer.create_reply();
        SignalOptions & roptions = reply.options();

        roptions.add("accepted", true);
        roptions.flush();

        this->init_send(conn, buffer );

        // tell listeners a new client has arrived
        SignalFrame frame("new_client_connected", "cpath:/", "cpath:/");
        frame.options().add( "clientid", clientid );
        call_signal( "new_client_connected", frame );
      }
    }
    else
    {
      if( info.uuid.empty() )
        error_msg = "The signal came from an unregistered client.";
      else if( info.uuid != clientid )
        error_msg = "The client id '" + info.uuid + "' (used for registration) "
            + "and '" + clientid + "' (used for identification) do not match.";
      else
        ServerRoot::instance().process_signal(target, receiver, clientid, frameid, buffer);
    }

    if( !error_msg.empty() )
      this->send_frame_rejected( conn, frameid, SERVER_CORE_PATH, error_msg );

    init_read( info );
  }
  else if( error != boost::asio::error::eof )
    CFerror << "Could not read from [" << conn->socket().remote_endpoint()
            << "]: " << error.message() << CFendl;

  if( error )
  {
    std::string uuid = m_clients[conn].uuid;
    conn->disconnect();
    m_clients.erase( conn );

    if( error == boost::asio::error::eof )
      CFinfo << "Cliemt [" << uuid << "] has disconnected. ("
             << m_clients.size() << " left)." << CFendl;
  }

}
示例#7
0
文件: rstat.cpp 项目: ISP-SST/redux
void printJobList( TcpConnection::Ptr conn, int nj ) {

    uint8_t cmd = CMD_JSTAT;
    boost::asio::write(conn->socket(),boost::asio::buffer(&cmd,1));

    uint64_t blockSize;
    shared_ptr<char> buf = conn->receiveBlock( blockSize );

    if ( !blockSize ) return;

    const char* ptr = buf.get();
    uint64_t count(0);
    try {
        typedef std::shared_ptr<Job::Info> InfoPtr;
        vector<InfoPtr> infos;
        size_t maxLength[2] = {0,0};       // jobName & user
        //cout << Job::Info::printHeader() << endl;
        while( count < blockSize ) {
            InfoPtr info(new Job::Info);
            count += info->unpack(ptr+count,conn->getSwapEndian());
            maxLength[0] = max(maxLength[0],info->name.size());
            maxLength[1] = max(maxLength[1],info->user.size()+info->host.size());
            infos.push_back(info);
        }
        std::sort( infos.begin(), infos.end(), [](const InfoPtr& a, const InfoPtr& b) {
            if(a->step != b->step) return (a->step > b->step);
            if(a->priority != b->priority) return (a->priority > b->priority);
            return (a->id < b->id);
            
        } );
        int nJobs = infos.size();
        bool addComma(false);
        if ( nj != 0 ) {
            if ( nj < nJobs ) addComma = true;
            nJobs = min(nj,nJobs);
        }
        cout << alignRight("#", 4) << alignRight("ID", 5) << alignCenter("type", 10) << alignCenter("started", 20); // + alignCenter("started", 20);
        cout << alignCenter("name", maxLength[0]+4) + alignCenter("user", maxLength[1]+5) + alignCenter("priority", 8) + alignCenter("state", 8);
        cout << endl;
        ptime now = boost::posix_time::second_clock::local_time();
        for( int i=0; i<nJobs; ++i ) {
            if( i == nJobs-1 ) {
                i = infos.size()-1;
                if (addComma) cout << "   :" << endl;
            }
                
            string info = alignRight(std::to_string(infos[i]->id), 5) + alignCenter(infos[i]->typeString, 10);
            string startedString;
            if ( infos[i]->startedTime < now ) startedString = to_iso_extended_string(infos[i]->startedTime);
            else startedString = to_iso_extended_string(infos[i]->submitTime);
            info += alignCenter(startedString, 20);
            info += alignCenter(infos[i]->name, maxLength[0]+4) + alignLeft(infos[i]->user + "@" + infos[i]->host, maxLength[1]+5);
            info += alignCenter(std::to_string(infos[i]->priority), 8);
            info += alignCenter(Job::stateTag(infos[i]->state), 3) + alignLeft(infos[i]->progressString, 15);
            cout << alignRight(to_string(i+1),4) << info << endl;
        }
    } catch ( const exception& e) {
        cerr << "printJobList: Exception caught while parsing block: " << e.what() << endl;
    }
    if( count != blockSize ) {
        cerr << "printJobList: Parsing of datablock failed,  count = " << count << "   blockSize = " << blockSize << "  bytes." << endl;
    }

}