home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 10 / ioProg_10.iso / soft / optima / samples.z / ClientWorker.wxc < prev    next >
Encoding:
Text File  |  1996-07-26  |  6.2 KB  |  250 lines

  1. Save Format v1.3
  2. @begin ClassFile "ClientWorker"
  3.  Exported 0;
  4.  
  5. @begin-code BaseClassList
  6.  
  7. public WObject
  8.  
  9. @end-code;
  10.  
  11.  @begin UserFunction "ClientWorker()"
  12.   GencodeSrcLine 20;
  13.   FunctionName "ClientWorker::ClientWorker()";
  14.  @end;
  15.  
  16.  @begin UserFunction "Prototype for ClientWorker()"
  17.   Private 1;
  18.   GencodeSrcLine 27;
  19.   FunctionName "ClientWorker::Prototype for ClientWorker()";
  20.  @end;
  21.  
  22.  @begin UserFunction "~ClientWorker()"
  23.   GencodeSrcLine 26;
  24.   FunctionName "ClientWorker::~ClientWorker()";
  25.  @end;
  26.  
  27.  @begin UserFunction "Prototype for ~ClientWorker()"
  28.   Private 1;
  29.   GencodeSrcLine 29;
  30.   FunctionName "ClientWorker::Prototype for ~ClientWorker()";
  31.  @end;
  32.  
  33.  @begin UserFunction "Create( Form1 * displayWindow, WInt i )"
  34.   GencodeSrcLine 30;
  35.   FunctionName "ClientWorker::Create( Form1 * displayWindow, WInt i )";
  36.  @end;
  37.  
  38.  @begin UserFunction "Prototype for Create( Form1 * displayWindow, WInt i )"
  39.   Private 1;
  40.   GencodeSrcLine 31;
  41.   FunctionName "ClientWorker::Prototype for Create( Form1 * displayWindow, WInt i )";
  42.  @end;
  43.  
  44.  @begin UserFunction "Client( void * )"
  45.   Private 1;
  46.   GencodeSrcLine 42;
  47.   FunctionName "ClientWorker::Client( void * )";
  48.  @end;
  49.  
  50.  @begin UserFunction "Prototype for Client( void * )"
  51.   Private 1;
  52.   GencodeSrcLine 33;
  53.   FunctionName "ClientWorker::Prototype for Client( void * )";
  54.  @end;
  55.  
  56.  @begin HPPPrefixBlock
  57. @begin-code HPPPrefix
  58.  
  59. // Declarations added here will be included at the top of the .HPP file
  60.  
  61. class Form1;
  62.  
  63. @end-code;
  64.   GencodeSrcLine 11;
  65.  @end;
  66.  
  67.  @begin CPPPrefixBlock
  68. @begin-code CPPPrefix
  69.  
  70. // Code added here will be included at the top of the .CPP file
  71. #include "Form1.hpp"
  72. #include "ServerWorker.hpp"
  73. #define MAX_BUFFER_LENGTH 500
  74. extern template WVector< WSocket >;
  75. extern template WVector< ClientWorker >;
  76.  
  77. //  Include definitions for resources.
  78. #include "WRes.h"
  79.  
  80. @end-code;
  81.   GencodeSrcLine 11;
  82.  @end;
  83.  
  84.  @begin ClassContentsBlock
  85. @begin-code ClassContents
  86.  
  87.     public:
  88.         // add your public instance data here
  89.     private:
  90.         // add your private instance data here
  91.         WThread   _thread;
  92.         Form1   * _displayWindow;
  93.         WInt      _id;
  94.     protected:
  95.         // add your protected instance data here
  96.  
  97. @end-code;
  98.   GencodeSrcLine 18;
  99.  @end;
  100.  
  101. @begin-code GeneratedClassContents
  102.  
  103.  
  104. @end-code;
  105.  
  106. @begin-code Code "ClientWorker::ClientWorker()"
  107.  
  108. @@CLASSNAME@::@CLASSNAME@()
  109.     :    _displayWindow( NULL ),
  110.          _id( 0 )
  111. {
  112.     
  113. }
  114.  
  115. @end-code;
  116.  
  117. @begin-code Code "ClientWorker::Prototype for ClientWorker()"
  118.  
  119.     public:
  120.         @@CLASSNAME@();
  121.  
  122. @end-code;
  123.  
  124. @begin-code Code "ClientWorker::~ClientWorker()"
  125.  
  126. @@CLASSNAME@::~@CLASSNAME@()
  127. {
  128.     
  129. }
  130.  
  131. @end-code;
  132.  
  133. @begin-code Code "ClientWorker::Prototype for ~ClientWorker()"
  134.  
  135.     public:
  136.         ~@CLASSNAME@();
  137.  
  138. @end-code;
  139.  
  140. @begin-code Code "ClientWorker::Create( Form1 * displayWindow, WInt i )"
  141.  
  142. WBool @CLASSNAME@::Create( Form1 * displayWindow, WInt i )
  143. {
  144.     WBool success;
  145.     
  146.     _displayWindow = displayWindow;
  147.     _id = i;
  148.     success =
  149.         _thread.Create( this,
  150.                         (WThreadRoutine)&@CLASSNAME@::Client,
  151.                         NULL );
  152.     return success;    
  153. }
  154.  
  155. @end-code;
  156.  
  157. @begin-code Code "ClientWorker::Prototype for Create( Form1 * displayWindow, WInt i )"
  158.  
  159.     public:
  160.         WBool Create( Form1 * displayWindow, WInt i );
  161.  
  162. @end-code;
  163.  
  164. @begin-code Code "ClientWorker::Client( void * )"
  165.  
  166. WDWord @CLASSNAME@::Client( void * )
  167. {
  168.     WInt bytes_received;
  169.     WBuffer buffer;
  170.     WBuffer buffer_2;
  171.     WString welcome;
  172.     WString msg;
  173.     WSocket * my_sock;
  174.     WInt num_clients;    
  175.     WString my_name;
  176.     
  177.     ServerWorker::_client_sockets_cs.Enter();
  178.     my_sock = ServerWorker::_client_sockets[_id];
  179.     ServerWorker::_client_sockets_cs.Exit();
  180.     my_name = my_sock->GetRemoteHostName();
  181.  
  182.     // Send welcome message to all clients.
  183.     welcome.SetText( WTEXT( "Welcome " ) );
  184.     welcome.Concat( my_name );
  185.     buffer.Create( welcome.GetSize(), (void *)welcome.GetText() );
  186.     ServerWorker::_client_sockets_cs.Enter();
  187.     num_clients = ServerWorker::_client_sockets.GetCount();
  188.     for ( WInt i = 0; i < num_clients; i += 1 ) {
  189.         ServerWorker::_client_sockets[i]->
  190.             Send( buffer );
  191.     }
  192.     ServerWorker::_client_sockets_cs.Exit();        
  193.  
  194.     buffer.Clear();
  195.     buffer.Create( (WULong)MAX_BUFFER_LENGTH );
  196.     // Read messages from client, and send to all clients.
  197.     while ( TRUE ) {
  198.         bytes_received =
  199.             my_sock->Receive( buffer );
  200.         if ( bytes_received < 0 ) {
  201.             msg.Sprintf( WTEXT( "Client %d receive error!" ),
  202.                          _id );
  203.             _displayWindow->AddLog( msg.GetText() );
  204.             break;
  205.         }
  206.         if ( bytes_received == 0 ) {
  207.             msg.Sprintf( WTEXT( "Client %d closed." ),
  208.                          _id );
  209.             _displayWindow->AddLog( msg.GetText() );
  210.             break;
  211.         }
  212.         //msg.Sprintf( WTEXT( "Client %d received %d bytes." ),
  213.         //             _id, bytes_received );
  214.         //_displayWindow->AddLog( msg.GetText() );
  215.         msg.SetText( my_name.GetText() );
  216.         msg.Concat( WTEXT( ": " ) );
  217.         msg.Concat(
  218.             WString( (WChar *)buffer.GetBuffer(),
  219.                      bytes_received ) );
  220.         buffer_2.Create(
  221.             bytes_received + my_name.GetSize() + 2,
  222.             (void *)msg.GetText() );
  223.         // Send buffer received to all clients.
  224.         ServerWorker::_client_sockets_cs.Enter();
  225.         num_clients = ServerWorker::_client_sockets.GetCount();
  226.         for ( WInt i = 0; i < num_clients; i += 1 ) {
  227.             ServerWorker::_client_sockets[i]->
  228.                 Send( buffer_2 );
  229.         }
  230.         buffer_2.Clear();
  231.         ServerWorker::_client_sockets_cs.Exit();        
  232.     }
  233.     my_sock->Close( TRUE );
  234.     ServerWorker::_client_sockets_cs.Enter();
  235.     ServerWorker::_client_sockets.Remove( my_sock );
  236.     ServerWorker::_client_workers.Remove( this );
  237.     ServerWorker::_client_sockets_cs.Exit();    
  238.     return 0;
  239. }
  240.  
  241. @end-code;
  242.  
  243. @begin-code Code "ClientWorker::Prototype for Client( void * )"
  244.  
  245.     private:
  246.         WDWord Client( void * );
  247.  
  248. @end-code;
  249. @end;
  250.