void TCPConnectionManagerTest::test_send()
{
    // Use Case:
    // client requests a connection
    // expect client to be registered for any data
    StreamDataRequest req;
    DataSpec require;
    require.addStreamData("testData");
    req.addDataOption(require);

    QTcpSocket* client = _createClient();
    _sendRequest( client, req );
    QCoreApplication::processEvents();
    CPPUNIT_ASSERT_EQUAL( 1, _server->clientsForStream("testData") );
    TestDataBlob blob;
    blob.setData("sometestData");
    _server->send("testData",&blob);
    sleep(1);

    CPPUNIT_ASSERT( client->state() == QAbstractSocket::ConnectedState );
    boost::shared_ptr<ServerResponse> r = _clientProtocol->receive(*client);
    CPPUNIT_ASSERT( r->type() == ServerResponse::Blob );
    DataBlobResponse* res = static_cast<DataBlobResponse*>(r.get());
    CPPUNIT_ASSERT( res->dataName() == "testData" );
    CPPUNIT_ASSERT( res->blobClass() == "TestDataBlob" );
    TestDataBlob recvBlob;
    recvBlob.deserialise(*client, res->byteOrder());
    CPPUNIT_ASSERT(recvBlob == blob);

}
// Connect to Pelican Lofar and register requested data type
void PelicanBlobClient::connectToLofarPelican()
{
    while (_tcpSocket->state() == QAbstractSocket::UnconnectedState) {

        _tcpSocket->connectToHost(_server, _port);

        if (!_tcpSocket->waitForConnected(5000) ||
                _tcpSocket->state() == QAbstractSocket::UnconnectedState)
        {
            cerr << "PelicanBlobClient: Unable to connect to server ("
                 << _server.toStdString() << ":" << _port << ")" << endl;
            sleep(2);
            continue;
        }

        // Define the data type which the client will except and send request
        StreamDataRequest req;
        DataSpec require;
        require.setStreamData(_blobType);
        req.addDataOption(require);

        QByteArray data = _protocol->serialise(req);
        _tcpSocket -> write(data);
        _tcpSocket -> waitForBytesWritten(data.size());
        _tcpSocket -> flush();
    }
}
void TCPConnectionManagerTest::test_brokenConnection()
{
    // Use Case:
    // client requests a connection
    // expect client to be registered for any data
    StreamDataRequest req;
    DataSpec require;
    require.addStreamData("testData");
    req.addDataOption(require);

    QTcpSocket* client = _createClient();
    _sendRequest( client, req );
    QCoreApplication::processEvents();

    CPPUNIT_ASSERT_EQUAL( 1, _server->clientsForStream("testData") );

    // Use Case:
    // client dies after connection
    // expect to be removed from the system
    delete client;
    QCoreApplication::processEvents();
    CPPUNIT_ASSERT_EQUAL( 0, _server->clientsForStream("testData") );
}
예제 #4
0
/**
 * @details
 */
void PelicanTCPBlobServerTest::test_connection()
{
    // Use Case:
    // ?????? TODO
    // Expect: TODO
    //
    // Create and configure TCP server
    QString xml = "<PelicanTCPBlobServer>"
                  "   <connection port=\"0\"/>"  // 0 = find unused system port
                  "</PelicanTCPBlobServer>";
    ConfigNode config(xml);
    PelicanTCPBlobServer server(config);
    sleep(1);

    // Create a client and connect it to the server
    QTcpSocket tcpSocket;
    tcpSocket.connectToHost( QHostAddress::LocalHost, server.serverPort() );
    if (!tcpSocket.waitForConnected(5000) || tcpSocket.state() == QAbstractSocket::UnconnectedState)
        CPPUNIT_FAIL("Client could not connect to server");

    // Define the data type which the client will except and send request
    StreamDataRequest req;
    DataRequirements require;
    require.setStreamData("testData");
    req.addDataOption(require);

    PelicanClientProtocol clientProtocol;
    QByteArray data = clientProtocol.serialise(req);
    tcpSocket.write(data);
    tcpSocket.waitForBytesWritten(data.size());
    tcpSocket.flush();

    /// ensure we are registered before continuing
    while( server.clientsForStream("testData") == 0 )
    {
        sleep(1);
    }

    {
        // Test Server send
        TestDataBlob blob;
        blob.setData("Testing TCPServer");
        server.send("testData", &blob);

        // Evaluate the response from the server
        tcpSocket.waitForReadyRead();
        boost::shared_ptr<ServerResponse> r = clientProtocol.receive(tcpSocket);
        CPPUNIT_ASSERT( r->type() == ServerResponse::Blob );
        TestDataBlob blobResult;
        blobResult.deserialise(tcpSocket, ((DataBlobResponse*)r.get())->byteOrder());
        CPPUNIT_ASSERT(blobResult == blob);
    }

    {
        // Test Server send
        TestDataBlob blob;
        blob.setData("Testing TCPServer again");
        server.send("testData", &blob);

        // Evaluate the response from the server
        tcpSocket.waitForReadyRead();
        boost::shared_ptr<ServerResponse> r = clientProtocol.receive(tcpSocket);
        CPPUNIT_ASSERT( r->type() == ServerResponse::Blob );
        TestDataBlob blobResult;
        blobResult.deserialise(tcpSocket, ((DataBlobResponse*)r.get())->byteOrder());
        CPPUNIT_ASSERT(blobResult == blob);
    }
}