home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / mnl-rpc++-2.3.1 / part01 / rpc++ / service.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-24  |  4.4 KB  |  156 lines

  1. // -*- c++ -*-
  2. /* 
  3. Copyright (C) 1991 Peter Bersen
  4.  
  5. This file is part of the rpc++ Library.  This library is free
  6. software; you can redistribute it and/or modify it under the terms of
  7. the GNU Library General Public License as published by the Free
  8. Software Foundation; either version 2 of the License, or (at your
  9. option) any later version.  This library is distributed in the hope
  10. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  11. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  12. PURPOSE.  See the GNU Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  
  17. Modified and partially rewritten March 1992 by Michael N. Lipp,
  18. mnl@dtro.e-technik.th-darmstadt.de. The original copyright terms and
  19. conditions apply without change to any modified or new parts.
  20. */
  21.  
  22. #ifndef _RPCSERVICE_H_
  23. #define _RPCSERVICE_H_
  24. static char _rpcpp_service_h_[]
  25. = "service.h,v 2.5 1993/01/20 15:00:51 mnl Exp";
  26.  
  27. // service.h,v
  28. // Revision 2.5  1993/01/20  15:00:51  mnl
  29. // Updated for gcc-2.3.3.
  30. //
  31. // Revision 2.4  1992/12/06  18:58:14  mnl
  32. // Fixed various bugs and added some Methods.
  33. //
  34. // Revision 2.3  1992/06/15  19:13:30  mnl
  35. // Fixed a few bugs, clarified interface.
  36. //
  37. // Revision 2.2  1992/06/13  14:27:41  mnl
  38. // Adapted to (patched) gcc-2.2. Fixed several bugs.
  39. //
  40. // Revision 2.1.1.1  1992/03/08  13:28:43  mnl
  41. // Initial mnl version.
  42. //
  43.  
  44. #ifdef __GNUG__
  45. #pragma interface
  46. #endif
  47.  
  48. #undef TRUE
  49. #undef FALSE
  50. #include <bool.h>
  51. #include "rpc++/request.h"
  52. #include "rpc++/callback.h"
  53.  
  54. class RpcRegistered;
  55.  
  56. //
  57. // RpcService
  58. //
  59.  
  60. class RpcService
  61. {
  62. public:
  63.   typedef enum
  64.     { noError, reconstructionAttempt, cantCreateTCPService,
  65.       cantRegisterService, notRegistered, cantGetArgs,
  66.       invalidResult, cantSendReply, cantFreeArgs, selectError,
  67.     } errorCode;
  68.  
  69.   // Get state
  70.   inline virtual bool OK ()
  71.     { return errorState == noError; }
  72.   // Construct a service object for service prog, version vers
  73.   RpcService (u_long prog, u_long vers);
  74.   // Construct a transient service object for version vers
  75.   RpcService (u_long vers);
  76.   // Destruct the service
  77.   virtual ~RpcService ();
  78.  
  79.   // Get the program number (normally used after construction of transient)
  80.   u_long Program ();
  81.   virtual u_long ReserveService ();
  82.   
  83.   // Register an object and its method to be called on request
  84.   virtual void Register (const RpcRequest&, const AnyRpcCallback&);
  85.   virtual void Unregister (const RpcRequest&);
  86.  
  87.   // The link to RPC
  88.   virtual void Dispatch (svc_req* req, SVCXPRT* transp);
  89.   // Provide the service. Never returns.
  90.   void Provide ();
  91.   // Provide the service. Returns after timeout or call.
  92.   void Provide (timeval&);
  93.  
  94.   // Get caller. May be called during execution of a service routine.
  95.   inline struct sockaddr_in* Caller ()
  96.     { return svc_getcaller (xprt); }
  97.   const char* CallerName ();
  98.   // Reply before return
  99.   void Reply (void* res);
  100.   void Reply ();
  101.   // Quit provide loop
  102.   void Interrupt ();
  103.  
  104. private:
  105.   // Save the address of the one and only RpcService in the process.
  106.   // There may be only one RpcService, because we can register a program
  107.   // with svc_register (method RpcServiceCallback) but we can't make
  108.   // the svc function give an argument to this function when doing a
  109.   // callback, which means that we can't have it distinguish between
  110.   // various instances of RpcService.
  111.   static RpcService* me;
  112.   static inline void RpcServiceCallback (svc_req* req, SVCXPRT* transp)
  113.     { RpcService::me->Dispatch (req, transp); }
  114.  
  115. protected:
  116.   void init ();
  117.   void HandleError (errorCode e);
  118.   virtual void DoProvide (timeval*);
  119.   bool standalone;
  120.   errorCode errorState;
  121.   u_long prog;
  122.   u_long vers;
  123.   RpcRegistered** handlers;
  124.   int maxHandlerIndex;
  125.   static RpcRegistered* nopHandler;
  126.   SVCXPRT* xprt;
  127.   RpcRequest* rpcreq;
  128.   bool quitLoop;
  129.   char* inbuf;
  130.   int inmax;
  131.   bool haveReplied;
  132.  
  133.   // Default error handling prints a message and exit(2)s.
  134.   virtual void HandleError ();
  135. };
  136.  
  137. inline void RpcService::HandleError (errorCode e)
  138. { errorState = e; HandleError (); }
  139.  
  140. inline u_long RpcService::Program ()
  141. { return prog; }
  142.  
  143. inline void RpcService::Reply ()
  144. { Reply (0); }
  145.  
  146. inline void RpcService::Interrupt ()
  147. { quitLoop = TRUE; }
  148.  
  149. inline void RpcService::Provide ()
  150. { DoProvide (0); }
  151.  
  152. inline void RpcService::Provide (timeval& t)
  153. { DoProvide (&t); }
  154.  
  155. #endif
  156.