home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / mnl-rpc++-2.3.1 / part01 / rpc++ / stub.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-24  |  4.9 KB  |  158 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 _RPCSTUB_H_
  23. #define _RPCSTUB_H_
  24. static char _rpcpp_stub_h_[]
  25. = "stub.h,v 2.5 1992/12/06 18:58:15 mnl Exp";
  26.  
  27. // stub.h,v
  28. // Revision 2.5  1992/12/06  18:58:15  mnl
  29. // Fixed various bugs and added some Methods.
  30. //
  31. // Revision 2.4  1992/08/08  13:23:18  mnl
  32. // Space allocated for result turned out to be local to a CLIENT*, thus
  33. // "res", "resmax" and "resproc" have been made member variables of
  34. // RpcStub.
  35. //
  36. // Revision 2.3  1992/06/15  19:13:31  mnl
  37. // Fixed a few bugs, clarified interface.
  38. //
  39. // Revision 2.2  1992/06/13  14:27:43  mnl
  40. // Adapted to (patched) gcc-2.2. Fixed several bugs.
  41. //
  42. // Revision 2.1.1.1  1992/03/08  13:28:43  mnl
  43. // Initial mnl version.
  44. //
  45.  
  46. #ifdef __GNUG__
  47. #pragma interface
  48. #endif
  49.  
  50. #undef TRUE
  51. #undef FALSE
  52. #include <bool.h>
  53. #include <String.h>
  54. #include <sys/time.h>
  55. #include "rpc++/request.h"
  56.  
  57. class RpcStub 
  58. {
  59. protected:
  60.   static timeval defaultTimeout;
  61.  
  62. public:
  63.   typedef enum
  64.     { noError, notConnected, cantCreate, cantCall,
  65.     } errorCode;
  66.  
  67.   // Construct a new stub
  68.   RpcStub (u_long prognum, u_long versnum,
  69.        char* hostname = "localhost",
  70.        timeval timeout = defaultTimeout, bool connect = TRUE);
  71.   RpcStub (u_long prognum, u_long versnum,
  72.        char* hostname = "localhost",
  73.        bool connect = TRUE, timeval timeout = defaultTimeout);
  74.   virtual ~RpcStub ();
  75.  
  76.   // Reconnect (in case of failure or delayed connection)
  77.   void Reconnect (bool handle_errors = TRUE);
  78.  
  79.   // Various inquiries
  80.   virtual bool OK ();
  81.   CLIENT* Service ();
  82.  
  83.   // Get/set timeout
  84.   timeval GetTimeout () const;
  85.   void SetTimeout (timeval& timo);
  86.  
  87.   // Make a call, either with or without an argument. If handle_errors
  88.   // is true, "Call" will call the error handler in case of an error.
  89.   // Else, it returns 0 as result and it is up to the client to handle
  90.   // the error.
  91.   // Call with one arg:
  92.   void* Call (RpcRequest&, bool handle_errors = TRUE);
  93.   // Call with two args:
  94.   void* Call (RpcRequest&, void* in, bool handle_errors = TRUE);
  95.   // ...
  96.   void* Call (RpcRequest& req, void*, void*, bool handle_errors = TRUE);
  97.   void* Call (RpcRequest& req, void*, void*, void*, bool handle_errors = TRUE);
  98.   void* Call (RpcRequest& req, void*, void*, void*, void*,
  99.           bool handle_errors = TRUE);
  100.   void* Call (RpcRequest& req, void*, void*, void*, void*, void*,
  101.           bool handle_errors = TRUE);
  102.   void* Call (RpcRequest& req, void*, void*, void*, void*, void*, void*,
  103.           bool handle_errors = TRUE);
  104.   void* Call (RpcRequest& req, void*, void*, void*, void*, void*, void*, void*,
  105.           bool handle_errors = TRUE);
  106.   // Call with N args:
  107.   void* Call (RpcRequest& req, void**, bool handle_errors = TRUE);
  108.  
  109. protected:
  110.   void* HandleError (errorCode e);
  111.   errorCode errorState;
  112.   u_long program;
  113.   u_long version;
  114.   String server;
  115.   timeval timeout;
  116.   CLIENT* svc;
  117.   void* res;
  118.   size_t resmax;
  119.   xdrproc_t resproc;
  120.   RpcRequest* curReq;
  121.   void** curArgs;
  122.  
  123.   void init (u_long prognum, u_long versnum,
  124.          char* hostname, timeval timeout, bool connect);
  125.   // Default error handling prints a message and exit(2)s.
  126.   virtual void* HandleError ();
  127.   void* DoCall (RpcRequest& req, void** args, bool handle_errors);
  128.   void* ReCall (bool);
  129. };
  130.  
  131. inline RpcStub::RpcStub (u_long prognum, u_long versnum,
  132.              char* hostname, timeval timeout, bool connect)
  133. { init (prognum, versnum, hostname, timeout, connect); }
  134.  
  135. inline RpcStub::RpcStub (u_long prognum, u_long versnum,
  136.              char* hostname, bool connect, timeval timeout)
  137. { init (prognum, versnum, hostname, timeout, connect); }
  138.  
  139. inline virtual bool RpcStub::OK ()
  140. { return errorState == noError; }
  141.  
  142. inline CLIENT* RpcStub::Service ()
  143. { return svc; }
  144.  
  145. inline timeval RpcStub::GetTimeout () const
  146. { return timeout; }
  147.  
  148. inline void RpcStub::SetTimeout (timeval& tv)
  149. { clnt_control (svc, CLSET_TIMEOUT, &tv); timeout = tv; }
  150.  
  151. inline void* RpcStub::Call (RpcRequest& req, bool handle_errors = TRUE)
  152. { return Call (req, (void*)0, handle_errors); }
  153.  
  154. inline void* RpcStub::HandleError (errorCode e)
  155. { errorState = e; return HandleError (); }
  156.  
  157. #endif
  158.