Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

SRPC_impl.H

Go to the documentation of this file.
00001 // Copyright (C) 2001, Compaq Computer Corporation
00002 // 
00003 // This file is part of Vesta.
00004 // 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 // 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // Last modified on Sun May 22 22:53:52 EDT 2005 by ken@xorian.net         
00020 //      modified on Fri Aug 14 14:24:27 PDT 1998 by mann  
00021 //      modified on Tue Mar 31 10:34:24 PST 1998 by heydon
00022 //      modified on Fri Aug 29 14:27:55 PDT 1997 by yuanyu
00023 //      modified on Thu Aug  1 14:39:57 PDT 1996 by levin
00024 
00025 #include <Basics.H>  // for threads
00026 
00027 //  **********************************
00028 //  *  Implementation of class SRPC  *
00029 //  **********************************
00030 
00031 class SRPC_listener;    // forward declaration
00032 
00033 class SRPC_impl {
00034   friend class SRPC;
00035 public:
00036   //  ***************************
00037   //  *  Data type definitions  *
00038   //  ***************************
00039 
00040   //  SRPC wire protocol:
00041   //  -------------------
00042   //
00043   //  Each direction on the wire is treated as a reliable stream of
00044   //  self-describing entities ("items").  Each item begins with a
00045   //  single byte code and may be followed by zero or more integers or
00046   //  byte strings.  The item_code specifies what integers/strings, if
00047   //  any, follow it in the stream.
00048 
00049   //  An integer is represented as a binary, 4-byte value in network
00050   //  byte order.  A byte string is represented by a binary,
00051   //  non-negative, 4-byte value (call it L) specifying the length of
00052   //  the string, followed by a sequence of L bytes that forms the
00053   //  body of the string.  Thus, a byte string may contain any byte values,
00054   //  including the null character.
00055 
00056   //  The following table defines the item codes used by SRPC.  The
00057   //  comments indicate what numbers or byte sequences, if any, are
00058   //  sent.
00059 
00060   enum item_code {
00061     ic_null =           0,        // internal use only
00062     ic_hello =         10,        // (proto_version: bs)
00063     ic_start_call =    11,        // (proc: int32, intf: int32)
00064     ic_end_call =      12,        // (and_close: int)
00065     ic_end_ack =       13,        // (and_close: int)
00066     ic_seq_start =     20,        // (n: int32, bytes: int32)
00067     ic_seq_end =       21,        // ()
00068     ic_int16 =         28,        // (i: int16)
00069     ic_int16_array =   29,        // (n: int32, vals[0..n): int16)
00070     ic_int32 =         30,        // (i: int32)
00071     ic_int32_seq =     31,        // (n: int, values[0..n): int)
00072     ic_chars =         32,        // (body: bs)
00073     ic_chars_seq =     33,        // (n: int32, bodies: bs, offs[0..n): int32)
00074     ic_bytes =         34,        // (body: bs)
00075     ic_bytes_seq =     35,        // (n: int32, bodies: bs, lens[0..n): int32)
00076     ic_text =          36,        // (body: bs)
00077     ic_socket =        40,        // (ip_addr_and_port: bs)
00078 
00079     // Added later:
00080     ic_int64 =         41,        // (i: int64)
00081     ic_int32_array =   42,        // (n: int32, vals[0..n): int32)
00082     ic_int64_array =   43,        // (n: int32, vals[0..n): int64)
00083 
00084     ic_bool_true     = 44,        // Boolean piggy-backed in the item code
00085     ic_bool_false    = 45,
00086 
00087     ic_failure =      255         // (code: int32, msg: bs)
00088   };
00089 
00090   // Definitions for state machine
00091 
00092   enum rpc_state {
00093     initial = 0,                    // start of call (includes reused conn.)
00094     ready = 1,                      // start of directional transmission
00095     data_out = 2,                   // sending top-level args or results
00096     seq_out = 3,                    // sending a sequence, arg or result
00097     data_in = 4,                    // recving top-level args or results
00098     seq_in = 5,                     // recving a sequence, arg or result
00099     failed = 6
00100   };
00101 
00102   enum ops {
00103     op_start_call       =  0,
00104     op_await_call       =  1,
00105     op_send_int32       =  2,
00106     op_recv_int32       =  3,
00107     op_send_chars       =  4,
00108     op_recv_chars       =  5,
00109     op_recv_chars_here  =  6,
00110     op_send_bytes       =  7,
00111     op_recv_bytes       =  8,
00112     op_recv_bytes_here  =  9,
00113     op_send_text        = 10,
00114     op_recv_text        = 11,
00115     op_send_socket      = 12,
00116     op_recv_socket      = 13,
00117     op_send_int32_seq   = 14,
00118     op_recv_int32_seq   = 15,
00119     op_send_chars_seq   = 16,
00120     op_recv_chars_seq   = 17,
00121     op_send_bytes_seq   = 18,
00122     op_recv_bytes_seq   = 19,
00123     op_send_seq_start   = 20,
00124     op_recv_seq_start   = 21,
00125     op_send_seq_end     = 22,
00126     op_recv_seq_end     = 23,
00127     op_send_end         = 24,
00128     op_recv_end         = 25,
00129     op_send_failure     = 26,
00130 
00131     // Added later:
00132     op_send_int16       = 27,
00133     op_recv_int16       = 28,
00134     op_send_int16_array = 29,
00135     op_recv_int16_array = 30,
00136 
00137     // Operations for specific bit sizes of integers
00138     op_send_int64       = 31,
00139     op_recv_int64       = 32,
00140 
00141     // Arrays of larger integers
00142     op_send_int32_array = 33,
00143     op_recv_int32_array = 34,
00144     op_send_int64_array = 35,
00145     op_recv_int64_array = 36,
00146 
00147     // Booleans
00148     op_send_bool        = 37,
00149     op_recv_bool        = 38,
00150 
00151   };
00152 
00153 
00154   //  **********************
00155   //  *  Member functions  *
00156   //  **********************
00157 
00158   // Constructors and Destructors
00159 
00160   SRPC_impl(bool i_am_caller);
00161   ~SRPC_impl() throw (SRPC::failure);
00162 
00163   // Item code transmission
00164 
00165   void send_item_code(item_code ic, bool flush = false)
00166     throw (TCP_sock::failure);
00167   item_code read_item_code()
00168     throw (TCP_sock::failure, SRPC::failure);
00169     // Reads item code from stream, but doesn't check it against
00170     // any expectation of the caller.
00171   void recv_item_code(item_code expected, bool *got_end = NULL)
00172     throw (TCP_sock::failure, SRPC::failure);
00173     // If the item_code read from the stream matches "expected", the
00174     // procedure returns normally.  Otherwise, if the received
00175     // item_code is "ic_seq_end" and "got_end" is non-NULL, set
00176     // "got_end" to true.  Otherwise, throw failure.
00177   item_code recv_item_code(item_code expected1, item_code expected2,
00178                            bool *got_end)
00179     throw (TCP_sock::failure, SRPC::failure);
00180     // Like recv_item_code, but either expected1 or expected2 is
00181     // allowed.  If either of these is read, it is returned.  If the
00182     // received item_code is "ic_seq_end" and "got_end" is non-NULL,
00183     // set "got_end" to true and return "ic_seq_end".
00184 
00185   // Integer transmission: send and recieve integers of different
00186   // sizes, dealing with network byte ordering.
00187 
00188   void send_int16(Basics::int16 i, bool flush = false)
00189     throw (TCP_sock::failure);
00190   Basics::int16 recv_int16() throw (TCP_sock::failure);
00191 
00192   void send_int32(Basics::int32 i, bool flush = false)
00193     throw (TCP_sock::failure);
00194   Basics::int32 recv_int32() throw (TCP_sock::failure);
00195 
00196   void send_int64(Basics::int64 i, bool flush = false)
00197     throw (TCP_sock::failure);
00198   Basics::int64 recv_int64() throw (TCP_sock::failure);
00199 
00200   // Byte-sequence transmission
00201 
00202   void send_bytes(const char *bs, int len, bool flush = false)
00203     throw (TCP_sock::failure);
00204   void recv_bytes(char *bs, int len)
00205     throw (TCP_sock::failure, SRPC::failure);
00206     // Reads exactly "len" bytes from the underlying stream, waiting as
00207     // necessary.
00208 
00209   // State machine
00210 
00211   void check_state(int op) throw (SRPC::failure);
00212   void ensure_sending_state() throw ();
00213   void ensure_recving_state() throw ();
00214 
00215   // Failure reporting
00216 
00217   void throw_failure(int reason, const Text &msg, bool local_only = false)
00218     throw (SRPC::failure);
00219   void report_TCP_failure(TCP_sock::failure &tf)
00220     throw (SRPC::failure);
00221 
00222   //  *****************
00223   //  *  Member data  *
00224   //  *****************
00225 
00226   rpc_state state;
00227   SRPC::failure f;                  // set if state == failed
00228 
00229   bool is_caller;                   // true <=> this is the caller
00230   bool is_sender;                   // true <=> this end is allowed to send
00231   bool version_sent;                // true <=> "hello" sent
00232   bool version_checked;             // true <=> "hello" matches
00233 
00234   item_code buffered_ic;
00235 
00236   TCP_sock *s;                       // local socket
00237   SRPC_listener *listener;           // (for callee only)
00238 
00239   bool use_read_timeout;
00240   bool use_read_timeout_between_calls;
00241   unsigned int read_timeout_secs;
00242 };
00243 
00244 //  *************************
00245 //  *  SRPC_listener class  *
00246 //  *************************
00247 
00248 class SRPC_listener {
00249 public:
00250   TCP_sock *sock;                 // TCP_sock listener
00251   bool my_sock;                   // true <=> we created sock
00252   u_short port;                   // port we're listening on
00253 
00254   // Static member functions
00255   static SRPC_listener *create(u_short port, TCP_sock *ls = NULL)
00256     throw (TCP_sock::failure);
00257 
00258   static bool destroy(SRPC_listener *listener)
00259     throw (TCP_sock::failure);
00260 
00261 private:
00262   SRPC_listener *next;
00263   int n;                          // reference count
00264 
00265   // static member data
00266   static Basics::mutex m;          // mutex for:
00267   static SRPC_listener *listeners; // list of known listeners
00268 };
00269 
00270 //  Convenience declarations
00271 
00272 typedef SRPC_impl::item_code item_code;
00273 typedef char *chars;

Generated on Mon May 8 00:48:57 2006 for Vesta by  doxygen 1.4.2