示例#1
0
int main( int argc, char **argv )
{
    TEST( co::init( argc, argv ) );

    co::ConnectionDescriptionPtr connDesc = new co::ConnectionDescription;

    lunchbox::RNG rng;
    connDesc->type = co::CONNECTIONTYPE_TCPIP;
    connDesc->port = (rng.get<uint16_t>() % 60000) + 1024;
    connDesc->setHostname( "localhost" );

    MyLocalNodePtr server = new MyLocalNode;
    server->addConnectionDescription( connDesc );
    TEST( server->listen( ));

    co::NodePtr serverProxy = new co::Node;
    serverProxy->addConnectionDescription( connDesc );

    connDesc = new co::ConnectionDescription;
    connDesc->type = co::CONNECTIONTYPE_TCPIP;
    connDesc->setHostname( "localhost" );

    co::LocalNodePtr client = new co::LocalNode;
    client->addConnectionDescription( connDesc );
    TEST( client->listen( ));
    TEST( client->connect( serverProxy ));

    server->registerCommandHandler( cmdID1,
                                    boost::bind( &MyLocalNode::cmdCustom1,
                                                 server.get(), _1 ),
                                    server->getCommandThreadQueue( ));
    server->registerCommandHandler( cmdID2,
                                    boost::bind( &MyLocalNode::cmdCustom2,
                                                 server.get(), _1 ), 0 );

    serverProxy->send( cmdID1 );
    serverProxy->send( cmdID2 ) << std::string( "hello" );

    TEST( gotCmd1.timedWaitEQ( true, 1000 ));
    TEST( gotCmd2.timedWaitEQ( true, 1000 ));

    TEST( client->disconnect( serverProxy ));
    TEST( client->close( ));
    TEST( server->close( ));

    serverProxy->printHolders( std::cerr );
    TESTINFO( serverProxy->getRefCount() == 1, serverProxy->getRefCount( ));
    TESTINFO( client->getRefCount() == 1, client->getRefCount( ));
    TESTINFO( server->getRefCount() == 1, server->getRefCount( ));

    serverProxy = 0;
    client      = 0;
    server      = 0;

    TEST( co::exit( ));

    return EXIT_SUCCESS;
}