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

📄 callback_server.cpp

📁 ESOAP是一款专注于嵌入式web service开发的工具 压缩包里提供了基本的例子
💻 CPP
字号:
/////////////////////////////////////////////////////////////////////////////
// $Id: callback_server.cpp,v 1.1 2001/09/11 16:37:22 rosimildo Exp $
//
// Copyright (c) 2001 Exor International Inc. All rights reserved.
//
// MODULE DESCRIPTION:
//
// MODIFICATION/HISTORY:
//
// $Log: callback_server.cpp,v $
// Revision 1.1  2001/09/11 16:37:22  rosimildo
// added callback example
//
//
// Created 2001/09/10 Rosimildo da Silva, ConnectTel Inc.
// [rdasilva@connecttel.com]
//
/////////////////////////////////////////////////////////////////////////////

#include "soap_server.h"
#include "Echo_impl.h"
#include "EchoCallback_proxy.h"
#include "uthread.h"
#include "passivetimer.h"

/* must be the last include */
#ifdef MEMWATCH
#include <memwatch.h>
#endif

struct ClientData
{
   echo::EchoCallback_Proxy proxy;
   PassiveTimer       timer;
   int count;
   ClientData( const esoap::String & clientUrl,
               const int rate,
               const int initCount )
   : proxy( clientUrl.c_str(), 60 ),
     timer( rate ),
	 count( initCount ) {}
};


class ServerEcho: public echo::Echo_Impl, public utils::Thread
{

  typedef std::vector< ClientData * >  ClientList;
  typedef ClientList::iterator         ClientListIterator;

  public:

   ServerEcho() : running( false ) {}

   virtual esoap::String registerCallback(
                  const esoap::String & clientUrl,
                  const int rate,
                  const int initCount );
   virtual bool deRegisterCallback(
                  const esoap::String & handle );

   virtual void run();

private:

   bool running;
   utils::Mutex mutex;
   std::vector< ClientData *> clients;   
};


esoap::String ServerEcho::registerCallback(
                  const esoap::String & clientUrl,
                  const int rate,
                  const int initCount )
{

  ClientData *c = new ClientData( clientUrl, rate, initCount );
  mutex.lock();
  clients.push_back( c );
  mutex.unlock();
  char buffer[ 512 ];
  sprintf( buffer, "%d", c );

  printf( "Server registered client: %d\n", c );

  return esoap::String( buffer );
}


bool ServerEcho::deRegisterCallback(
                  const esoap::String & handle )
{

  bool rc = false;
  ClientData *c = ( ClientData *)atoi( handle.c_str() );
  mutex.lock();
  printf( "Server deRegister size 1: %d\n", clients.size() );
  for( ClientListIterator i = clients.begin(); i != clients.end(); ++i )
  {
     if( c == *i )
     {
        printf( "Server deRegistered client: %d\n", c );
        clients.erase( i );
        delete c;
        rc = true;
		break;
     }
  }
  mutex.unlock();
  printf( "Server deRegister size 2: %d\n", clients.size() );
  return rc;

}


void ServerEcho::run()
{

   printf( "Server worker thread is starting\n" );
   running = true;
   while( running )
   {
      mutex.lock();
      for( ClientListIterator i = clients.begin(); i != clients.end(); ++i )
      {
         if( (*i)->timer.timed_out() )
         {
            printf( "Server calling client: %d, size=%d\n", *i, clients.size() );
            (*i)->proxy.callback( (*i)->count++ );
            (*i)->timer.reset();
         }
      }
      mutex.unlock();
      utils::Thread::sleep( 5 );
   }
}



int main(int, char ** )
{
  // create the server instance, and initialize it.
  esoap::ServerFactory::create( esoap::ServerFactory::ABYSS );
  esoap::Server::instance()->init( "eSOAPServer", 8080, ".", 0, true );

  // Create "Echo" instance, and register it with the server
  ServerEcho echo;
  echo.activate();
  echo.start();

  // start the server...
  esoap::Server::instance()->run();

  // shutdown server.
  echo.deactivate();
  esoap::Server::setInstance( 0 );
  return 0;
}

⌨️ 快捷键说明

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