home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool.zip / OOL / samples / sample9 / server.cpp < prev    next >
Text File  |  1997-04-08  |  3KB  |  144 lines

  1. #include "xheaders.h"
  2. #include XNamedPipeServer_i
  3. #include XThread_i
  4. #include XEventSemaphore_i
  5. #include XString_i
  6. #include XSemaphoreException_i
  7.  
  8. #include <process.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11.  
  12. XString semName;
  13.  
  14. //we derive a class
  15. class MyServer: public XNamedPipeServer
  16. {
  17.    public:
  18.       MyServer();
  19.       void Start( int );
  20. };
  21.  
  22. static int c = 0;
  23.  
  24. //the thread-starting function
  25. // TBO
  26. #ifndef __WATCOMC__
  27. void _Optlink StartFunction( void * arg)
  28. #else
  29. void StartFunction( void * arg)
  30. #endif
  31. {
  32.    c += 10;
  33.    MyServer * s = (MyServer*) arg;
  34.    s->Start( c );
  35. }
  36.  
  37.  
  38. //create the server and open a pipe
  39. MyServer :: MyServer(): XNamedPipeServer() 
  40. {
  41.    LONG res;
  42.     BOOL created = FALSE;
  43.  
  44.     XEventSemaphore * sem = NULL;
  45.     if( semName.GetLength() == 0)         //no thread earlier started
  46.     {                                                //we set a semaphore so the next thread waits with 
  47.         semName = "sample9";    //initalisation until this thread has started
  48.         created = TRUE;
  49.         sem = new XEventSemaphore( "sample9", TRUE, FALSE);
  50.     }
  51.     else        //a semaphor is set, another thread was started earlier than this.
  52.     {    
  53.                 //this thread must wait until the earlier thread has finished intialisation
  54.         try    //try if the semaphore is working
  55.         {
  56.             sem = new XEventSemaphore( "sample9" );    //try to open the semaphore 
  57.             sem->Wait(-1);                                    //wait until the semaphore gets post
  58.         }
  59.         catch( XSemaphoreException& e)    //if an exception is thrown the semaphore is closed
  60.         {                                            //start to init this thread (no error-handling needed)
  61.         }
  62.     }
  63.    if( (res = Open( "Sample9Pipe", XNPIPE_ACCESS_DUPLEX, XNPIPE_WAIT|XNPIPE_TYPE_BYTE|XNPIPE_READMODE_BYTE, 2 )) != 0)
  64.    {
  65.       printf( "cannot open server: %i", res);
  66.       exit(0);
  67.    }
  68.  
  69.    //every server is a single thread, because we use simple threads
  70.    //and not the window-api, this simple method is recommended
  71.    _beginthread( StartFunction, NULL, 20000, (void*) this);
  72.  
  73.     if( sem )
  74.     {
  75.         if(created)    //if this thread created the semaphore, post so other threads dont wait any longer
  76.             sem->Post();
  77.         delete sem; //close the semaphore
  78.     }
  79. }
  80.  
  81.  
  82. //start this server
  83. void MyServer :: Start( int id )
  84. {   
  85.    printf("server %i started, id = %i\n", id/10, id);
  86.    LONG res;
  87.  
  88.    //wait for a connect
  89.    Connect();
  90.  
  91.    while(1)
  92.    {
  93.         //client is killed?
  94.       if( GetState() == XNPIPE_STATE_CLOSING)
  95.         {
  96.             //clear connection
  97.             DisConnect();     
  98.             //wait for the next client          
  99.             if( (res = Connect()) != 0)
  100.             {
  101.                 printf( "cannot connect: %i id=%i", res, id);
  102.                 exit(0);
  103.             } 
  104.         }
  105.  
  106.         //we have a client
  107.         if( GetState() == XNPIPE_STATE_CONNECTED)         
  108.         {
  109.             //write some data
  110.             LONG i;
  111.             for(i=0; i < 10000; i++)
  112.                 Write( i + id);
  113.             ResetBuffer();
  114.         }
  115.     }
  116. }
  117.  
  118.  
  119. void main ( void)
  120. {
  121.    LONG res;
  122.    printf( "press Ctrl+C to stop the server\n");
  123.  
  124.    //in this case we start two servers 
  125.     try
  126.     {
  127.        MyServer server1;
  128.        MyServer server2;
  129.  
  130.        //let the servers work
  131.        XThread :: RunThreads();
  132.     }
  133.     catch( XSemaphoreException& e)
  134.     {
  135.        printf("\nA semaphore-exception was thrown!\n");
  136.         e.PrintError();
  137.     }
  138.     catch( XException& e)
  139.     {
  140.         printf("\nA standard-exception was thrown!\n");
  141.         e.PrintError();
  142.     }
  143. }
  144.