engine/dlib/src/test/test_ssdp.cpp
/*
 * TODO:
 * select() on file descriptors is not available on windows
 * Port this test to use WaitForSingleObject or other windows appropriate stuff
 */
select() on file descriptors is not available on windows
Port this test to use WaitForSingleObject or other windows appropriate stuff
select () on file descriptors is not available on windows Port this test to use WaitForSingleObject or other windows appropriate stuff
TEST_F(dmSSDPTest, JavaClient)
{
    dmSSDP::HSSDP server1, server2;
    dmSSDP::DeviceDesc device_desc1, device_desc2;
    char device1_udn[43] = { 0 };
    char device1_usn[60] = { 0 };
    CreateRandomUDN(device1_udn, 43);
    dmStrlCat(device1_usn, device1_udn, 60);
    dmStrlCat(device1_usn, "::upnp:rootdevice", 60);
    char device2_udn[43] = { 0 };
    char device2_usn[60] = { 0 };
    CreateRandomUDN(device2_udn, 43);
    dmStrlCat(device2_usn, device2_udn, 60);
    dmStrlCat(device2_usn, "::upnp:rootdevice", 60);
    dmSSDP::Result r;
    dmSSDP::NewParams server_params1;
    server_params1.m_MaxAge = 1;
    server_params1.m_AnnounceInterval = 1;
    server_params1.m_Announce = 1;
    r = dmSSDP::New(&server_params1, &server1);
    ASSERT_EQ(dmSSDP::RESULT_OK, r);
    device_desc1.m_Id = "my_root_device1";
    device_desc1.m_DeviceDescription = DEVICE_DESC_STATIC;
    device_desc1.m_DeviceType = "upnp:rootdevice";
    dmStrlCpy(device_desc1.m_UDN, device1_udn, sizeof(device_desc1.m_UDN));
    r = dmSSDP::RegisterDevice(server1, &device_desc1);
    ASSERT_EQ(dmSSDP::RESULT_OK, r);
    dmSSDP::NewParams server_params2;
    server_params2.m_MaxAge = 2;
    server_params2.m_AnnounceInterval = 2;
    server_params2.m_Announce = 0;
    r = dmSSDP::New(&server_params2, &server2);
    ASSERT_EQ(dmSSDP::RESULT_OK, r);
    device_desc2.m_Id = "my_root_device2";
    device_desc2.m_DeviceDescription = DEVICE_DESC_STATIC;
    device_desc2.m_DeviceType = "upnp:rootdevice";
    dmStrlCpy(device_desc2.m_UDN, device2_udn, sizeof(device_desc2.m_UDN));
    r = dmSSDP::RegisterDevice(server2, &device_desc2);
    ASSERT_EQ(dmSSDP::RESULT_OK, r);
    dmSSDP::Update(server1, false);
    dmSSDP::Update(server2, false);
    WaitPackage();
    printf("(C++) Sending USN1 = \"%s\"\n", device1_usn);
    printf("(C++) Sending USN2 = \"%s\"\n", device2_usn);
    char* dynamo_home = getenv("DYNAMO_HOME");
    char command[2048];
    dmSnPrintf(command, sizeof(command),
            "java -cp build/default/src/java:build/default/src/java_test:%s/ext/share/java/junit-4.6.jar -DUSN1=%s -DUSN2=%s org.junit.runner.JUnitCore com.dynamo.upnp.SSDPTest", dynamo_home, device1_usn, device2_usn);
#if !defined(__EMSCRIPTEN__) 
    const char* mode = "r";
    FILE* f = popen(command, mode);
    fd_set set;
    timeval timeout;
    timeout.tv_sec = 0;
    timeout.tv_usec = 5 * 1000;
    int fd = fileno(f);
    while (true) {
        FD_ZERO(&set);
        FD_SET(fd, &set);
        int n = select(fd + 1, &set, 0, 0, &timeout);
        if (n > 0) {
            int c = fgetc(f);
            if (c == EOF)
                break;
            fputc(c, stderr);
        }
        dmSSDP::Update(server1, false);
        dmSSDP::Update(server2, false);
    }
    int ret = pclose(f);
    ASSERT_EQ(0, ret);
#endif
    r = dmSSDP::DeregisterDevice(server1, "my_root_device1");
    ASSERT_EQ(dmSSDP::RESULT_OK, r);
    r = dmSSDP::DeregisterDevice(server2, "my_root_device2");
    ASSERT_EQ(dmSSDP::RESULT_OK, r);
    r = dmSSDP::Delete(server1);
    ASSERT_EQ(dmSSDP::RESULT_OK, r);
    r = dmSSDP::Delete(server2);
    ASSERT_EQ(dmSSDP::RESULT_OK, r);
}
TEST_F ( dmSSDPTest , JavaClient ) { dmSSDP :: HSSDP server1 , server2 ; dmSSDP :: DeviceDesc device_desc1 , device_desc2 ; char device1_udn [ 43 ] = { 0 } ; char device1_usn [ 60 ] = { 0 } ; CreateRandomUDN ( device1_udn , 43 ) ; dmStrlCat ( device1_usn , device1_udn , 60 ) ; dmStrlCat ( device1_usn , " " , 60 ) ; char device2_udn [ 43 ] = { 0 } ; char device2_usn [ 60 ] = { 0 } ; CreateRandomUDN ( device2_udn , 43 ) ; dmStrlCat ( device2_usn , device2_udn , 60 ) ; dmStrlCat ( device2_usn , " " , 60 ) ; dmSSDP :: Result r ; dmSSDP :: NewParams server_params1 ; server_params1 . m_MaxAge = 1 ; server_params1 . m_AnnounceInterval = 1 ; server_params1 . m_Announce = 1 ; r = dmSSDP :: New ( & server_params1 , & server1 ) ; ASSERT_EQ ( dmSSDP :: RESULT_OK , r ) ; device_desc1 . m_Id = " " ; device_desc1 . m_DeviceDescription = DEVICE_DESC_STATIC ; device_desc1 . m_DeviceType = " " ; dmStrlCpy ( device_desc1 . m_UDN , device1_udn , sizeof ( device_desc1 . m_UDN ) ) ; r = dmSSDP :: RegisterDevice ( server1 , & device_desc1 ) ; ASSERT_EQ ( dmSSDP :: RESULT_OK , r ) ; dmSSDP :: NewParams server_params2 ; server_params2 . m_MaxAge = 2 ; server_params2 . m_AnnounceInterval = 2 ; server_params2 . m_Announce = 0 ; r = dmSSDP :: New ( & server_params2 , & server2 ) ; ASSERT_EQ ( dmSSDP :: RESULT_OK , r ) ; device_desc2 . m_Id = " " ; device_desc2 . m_DeviceDescription = DEVICE_DESC_STATIC ; device_desc2 . m_DeviceType = " " ; dmStrlCpy ( device_desc2 . m_UDN , device2_udn , sizeof ( device_desc2 . m_UDN ) ) ; r = dmSSDP :: RegisterDevice ( server2 , & device_desc2 ) ; ASSERT_EQ ( dmSSDP :: RESULT_OK , r ) ; dmSSDP :: Update ( server1 , false ) ; dmSSDP :: Update ( server2 , false ) ; WaitPackage ( ) ; printf ( " \" \" \n " , device1_usn ) ; printf ( " \" \" \n " , device2_usn ) ; char * dynamo_home = getenv ( " " ) ; char command [ 2048 ] ; dmSnPrintf ( command , sizeof ( command ) , " " , dynamo_home , device1_usn , device2_usn ) ; #if ! defined ( __EMSCRIPTEN__ ) const char * mode = " " ; FILE * f = popen ( command , mode ) ; fd_set set ; timeval timeout ; timeout . tv_sec = 0 ; timeout . tv_usec = 5 * 1000 ; int fd = fileno ( f ) ; while ( true ) { FD_ZERO ( & set ) ; FD_SET ( fd , & set ) ; int n = select ( fd + 1 , & set , 0 , 0 , & timeout ) ; if ( n > 0 ) { int c = fgetc ( f ) ; if ( c == EOF ) break ; fputc ( c , stderr ) ; } dmSSDP :: Update ( server1 , false ) ; dmSSDP :: Update ( server2 , false ) ; } int ret = pclose ( f ) ; ASSERT_EQ ( 0 , ret ) ; #endif r = dmSSDP :: DeregisterDevice ( server1 , " " ) ; ASSERT_EQ ( dmSSDP :: RESULT_OK , r ) ; r = dmSSDP :: DeregisterDevice ( server2 , " " ) ; ASSERT_EQ ( dmSSDP :: RESULT_OK , r ) ; r = dmSSDP :: Delete ( server1 ) ; ASSERT_EQ ( dmSSDP :: RESULT_OK , r ) ; r = dmSSDP :: Delete ( server2 ) ; ASSERT_EQ ( dmSSDP :: RESULT_OK , r ) ; }
TODO:
 select() on file descriptors is not available on windows
 Port this test to use WaitForSingleObject or other windows appropriate stuff
TODO : select () on file descriptors is not available on windows Port this test to use WaitForSingleObject or other windows appropriate stuff
/*
     * About the servers:
     * Server1: max-age=1, announce=true
     * Server2: max-age=2, announce=false
     *
     * Server1 is used to test announce messages
     * Server2 is used to test expiration (client-side)
     * Server2 is also used for search as automatic announcement is disabled
     *
     */ // no support for popen