⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 interop_test.cpp

📁 ESOAP一款专注于嵌入式的WEB SERVICE开发的软件。
💻 CPP
📖 第 1 页 / 共 3 页
字号:
      // return is an array....
      esoap::Array     *ret  = (esoap::Array  *)resp->getParameter( 0 );
      if( ret && ret->isHref() )
      {
        esoap::String href    = ret->getHref();
        printf( "Got HREF=%s\n" , href.c_str() );
        ret  = ( esoap::Array *)in->getIdParameter( href );
       
      }
      if( ret && ( ret->getParameterCount() > 0 ) )
      {
        int res = ret->getParameter( 0 )->getInteger();
        if( res == val )
        {
          printf( "%s: %s  => %d   ===> PASSED \n" ,
                  method_name, ret->getName().c_str(), res );
          rc = true;
        }
        else
        {
           printf( "%s: %s  => %d   ===> FAILED \n" ,
                  method_name, ret->getName().c_str(), res );
        }
      }
      else
      {
        printf( "%s: %s  =>  ===> FAILED \n" , 
                method_name, resp->getName().c_str() );
      }
    }
    else
    {
      error_status = printFault( method_name, in->getFault() );
    }
    printf( "\n\n" );
    delete in;
    delete ht;
    return rc;
}


bool echoFloatArray( const ToolKitInfo & tk, const esoap::String & value,
                 esoap::String & error_status )
{
    static const char *method_name = "e:echoFloatArray";

    printBegin( method_name, tk, value.c_str() );
    esoap::Transport *ht = esoap::TransportFactory::create( tk.endpoint.c_str(),  
                                                          esoap::TransportFactory::HTTP );
    ht->setTimeout( INTEROP_TIMEOUT );
    esoap::Envelope    env;
    esoap::Method *m = env.setMethod( method_name, tk.name_space.c_str() );

    esoap::Array *a = new esoap::Array( "inputFloatArray" );
    float val = atof( value.c_str() );
    a->addFloat( val );
    a->setType( esoap::xsd_float );
    m->addParameter( a );
    bool rc = false;
    esoap::Envelope *in = ht->call( env, getAction( tk, method_name ).c_str() );
    if( in->success() )
    {
      esoap::Method    *resp = in->getMethod();
      // return is an array....
      esoap::Array     *ret  = (esoap::Array  *)resp->getParameter( 0 );
      if( ret && ret->isHref() )
      {
        esoap::String href    = ret->getHref();
        printf( "Got HREF=%s\n" , href.c_str() );
        ret  = ( esoap::Array *)in->getIdParameter( href );
       
      }
      if( ret && ( ret->getParameterCount() > 0 ) )
      {
        float res = ret->getParameter( 0 )->getFloat();
        if( fabs( res - val ) < 0.0001 )
        {
          printf( "%s: %s  => %f   ===> PASSED \n" ,
                  method_name, ret->getName().c_str(), res );
          rc = true;
        }
        else
        {
           printf( "%s: %s  => %f   ===> FAILED \n" ,
                  method_name, ret->getName().c_str(), res );
        }
      }
      else
      {
        printf( "%s: %s  =>  ===> FAILED \n" , 
                method_name, resp->getName().c_str() );
      }
    }
    else
    {
      error_status = printFault( method_name, in->getFault() );
    }
    printf( "\n\n" );
    delete in;
    delete ht;
    return rc;
}



bool echoStruct( const ToolKitInfo & tk, const esoap::String & value,
                 esoap::String & error_status )
{
    static const char *method_name = "e:echoStruct";

    printBegin( method_name, tk, value.c_str() );
    esoap::Transport *ht = esoap::TransportFactory::create( tk.endpoint.c_str(),  
                                                          esoap::TransportFactory::HTTP );
    ht->setTimeout( INTEROP_TIMEOUT );
    esoap::Envelope    env;
    esoap::Method *m = env.setMethod( method_name, tk.name_space.c_str() );
    // add extra namespace to the envelope...
    env.addAttribute( "xmlns:ns1", "http://soapinterop.org/xsd" );

    esoap::Parameter *p = new esoap::Parameter( "inputStruct" );
    p->setType( "ns1:SOAPStruct" );
    float val_float = atof( value.c_str() );
    int   val_int = atoi( value.c_str() );

    p->addFloat( "varFloat",  val_float );
    p->addInteger("varInt",  val_int );
    p->addString( "varString", value.c_str() );
    m->addParameter( p );
    bool rc = false;
    esoap::Envelope *in = ht->call( env, getAction( tk, method_name ).c_str() );
    if( in->success() )
    {
      esoap::Method      *resp = in->getMethod();
      esoap::Parameter   *ret  = resp->getParameter( 0 );
      ret = in->getRealParameter( ret );
      if( ret && ( ret->getParameterCount() > 0 ) )
      {
        float res = ret->getParameter( 0 )->getFloat();
        if( res == val_float )
        {
          printf( "%s: %s  => %f   ===> PASSED \n" ,
                  method_name, ret->getName().c_str(), res );
          rc = true;
        }
        else
        {
           printf( "%s: %s  => %f   ===> FAILED \n" ,
                  method_name, ret->getName().c_str(), res );
        }
      }
      else
      {
        printf( "%s: %s  =>  ===> FAILED \n" , 
                method_name, resp->getName().c_str() );
      }
    }
    else
    {
      error_status = printFault( method_name, in->getFault() );
    }
    printf( "\n\n" );
    delete in;
    delete ht;
    return rc;
}



bool echoStructArray( const ToolKitInfo & tk, const esoap::String & value,
                 esoap::String & error_status )
{
    static const char *method_name = "e:echoStructArray";

    printBegin( method_name, tk, value.c_str() );
    esoap::Transport *ht = esoap::TransportFactory::create( tk.endpoint.c_str(),  
                                                          esoap::TransportFactory::HTTP );
    ht->setTimeout( INTEROP_TIMEOUT );
    esoap::Envelope    env;
    esoap::Method *m = env.setMethod( method_name, tk.name_space.c_str() );
    env.addAttribute( "xmlns:ns1", "http://soapinterop.org/xsd" );

    esoap::Array *a = new esoap::Array( "inputStructArray" );

    a->setType( "ns1:SOAPStruct" );

    float val_float = atof( value.c_str() );
    int   val_int = atoi( value.c_str() );

    esoap::Parameter *p = new esoap::Parameter( "item" );
    p->setType( "ns1:SOAPStruct" );

    p->addFloat( "varFloat",  val_float );
//    p->addFloat( "varFloat",  -HUGE_VAL );
    p->addInteger("varInt",  val_int );
    p->addString( "varString", value.c_str() );
    a->addParameter( p );

    m->addParameter( a );
    bool rc = false;
    esoap::Envelope *in = ht->call( env, getAction( tk, method_name ).c_str() );
    if( in->success() )
    {
      esoap::Method    *resp = in->getMethod();
      // return is an array....
      esoap::Array *ret = (esoap::Array *)in->getRealParameter( resp->getParameter( 0 ) );
      printf( "Got real Array: %s\n" , ret->getName().c_str() );
      if( ret && ( ret->isHref() || ( ret->getParameterCount() > 0 ) ) )
      {
        esoap::Parameter *p1 = in->getRealParameter( ret->getParameter( 0 ) );
        printf( "Got P1: %s\n" , p1->getName().c_str() );
        int res = p1->getParameter( "varInt" )->getInteger();
        if( res == val_int )
        {
          printf( "%s: %s  => %d   ===> PASSED \n" ,
                  method_name, ret->getName().c_str(), res );
          rc = true;
        }
        else
        {
           printf( "%s: %s  => %d   ===> FAILED \n" ,
                  method_name, ret->getName().c_str(), res );
        }
      }
      else
      {
        printf( "%s: %s  =>  ===> FAILED \n" , 
                method_name, resp->getName().c_str() );
      }
    }
    else
    {
      error_status = printFault( method_name, in->getFault() );
    }
    printf( "\n\n" );
    delete in;
    delete ht;
    return rc;
}

/** sends a string, and check if the same is received back */
bool echoDate( const ToolKitInfo & tk, const esoap::String & value,
                 esoap::String & error_status )
{
    static const char *method_name = "e:echoDate";

    printBegin( method_name, tk, value.c_str() );
    esoap::Transport *ht = esoap::TransportFactory::create( tk.endpoint.c_str(),  
                                                          esoap::TransportFactory::HTTP );
    ht->setTimeout( INTEROP_TIMEOUT );
    esoap::Envelope    env;
    esoap::Method *m = env.setMethod( method_name, tk.name_space.c_str() );
    time_t t_req = time( 0 );
    m->addTimeInstant( "inputDate", t_req );
    bool rc = false;

    esoap::Envelope *in = ht->call( env, getAction( tk, method_name ).c_str() );
    if( in->success() )
    {
      esoap::Method    *resp = in->getMethod();
      esoap::Parameter *ret  = resp->getParameter( 0 );
      time_t res             = ret->getTimeInstant();
      if( res == t_req )
      {
        printf( "%s: %s  => %s   ===> PASSED \n" ,
                method_name, ret->getName().c_str(), ret->timeInstantAsString().c_str() );
        rc = true;
      }
      else
      {
        printf( "%s: %s  => %s ( in_tm = %08X, out_tm=%08X )   ===> FAILED \n" , 
                method_name, ret->getName().c_str(), ret->timeInstantAsString().c_str(),
                t_req, res );

        error_status = "ERROR: wrong time back !!!. ";
        error_status += ret->getValueAsString();
      }
    }
    else
    {
      error_status = printFault( method_name, in->getFault() );
    }
    printf( "\n\n" );
    delete in;
    delete ht;
    return rc;
}



/** sends a string, and check if the same is received back */
bool echoBase64( const ToolKitInfo & tk, const esoap::String & value,
                 esoap::String & error_status )
{
    static const char *method_name = "e:echoBase64";

    printBegin( method_name, tk, value.c_str() );
    esoap::Transport *ht = esoap::TransportFactory::create( tk.endpoint.c_str(),  
                                                          esoap::TransportFactory::HTTP );
    ht->setTimeout( INTEROP_TIMEOUT );
    esoap::Envelope    env;
    esoap::Method *m = env.setMethod( method_name, tk.name_space.c_str() );
    const char    *s_req = value.c_str();
    unsigned long  s_len = value.size();
    printf( "Base64: msg = <%s>  ==> size=%d\n", s_req, s_len );
    m->addBinary( "inputBase64", s_req, s_len );
    bool rc = false;

    esoap::Envelope *in = ht->call( env, getAction( tk, method_name ).c_str() );
    if( in->success() )
    {
      esoap::Method    *resp = in->getMethod();
      esoap::Parameter *ret  = in->getRealParameter( resp->getParameter( 0 ) );
      const char   *b = 0;
      unsigned long len = 0;
      if( ret && ret->decode( &b, &len ) )
      {
        printf( "Base64: msg rcvd = <%s>  ==> size=%d\n", b, len );
        if( ( s_len == len ) && memcmp( s_req, b, len ) == 0 )
        {
          printf( "%s: %s  => %s   ===> PASSED \n" ,
                  method_name, ret->getName().c_str(), value.c_str() );
          rc = true;
        }
        else
        {
          printf( "%s: %s  => %s   ===> FAILED \n" , 
                  method_name, ret->getName().c_str(), value.c_str() );
        }
        delete [] ( char *)b;
      }
      else
      {
          printf( "%s:          => %s   ===> FAILED \n" , 
                  method_name, value.c_str() );
      }
    }
    else
    {
      error_status = printFault( method_name, in->getFault() );
    }
    printf( "\n\n" );
    delete in;
    delete ht;
    return rc;
}




void addMethod( FILE *fd, const char *method, int & fail_no, bool result )
{
    esoap::String temp;
    if( result )
       temp = "PASS";
    else
    {
        char buffer[ 100 ];
        sprintf( buffer, "FAIL(%d)",  fail_no++ );
        temp = buffer;
    }
    fprintf( fd, "<method name=\"%s\">%s</method>\n", method, temp.c_str() );
}


void addError( FILE *fd, const char *error, int & fail_no, bool result )
{
    if( !result )
        fprintf( fd, "<error id=\"%d\">%s</error>\n", fail_no++, error  );
}

esoap::String escape_xml( const esoap::String & src )
{
    esoap::String dst;
    const char *str = src.c_str();
    for( int i = 0; i < src.size(); i++) 
    {
       if (str[i] == '<')
       {
         dst += "&lt;";
       } 
       else if ( str[i] == '&')
       {
         dst += "&amp;";
       }
       else 
         dst += str[ i ];
    }
    return dst;
}


// int main(int argc, char **argv)
int main(int, char ** )
{

// printf( "timezone= %d, %d\n", _timezone );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -