Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

rpc_srv.cc

Go to the documentation of this file.
00001 
00002 
00003 
00004 extern "C"
00005 {
00006 
00007 #include <unistd.h>             // getpid()
00008 #ifdef sunos5
00009 #include <sys/select.h>         /* make sure NBBY defined before in.h */
00010   /* (needed for Solaris 2.6) */
00011 #endif
00012 #include <rpc/rpc.h>            /* SVCXPRT, svc_req,  xprt_, */
00013   /* & xdr_ functions */
00014 #include <rpc/svc.h>            /* svc_ functions */
00015 #ifdef sunos5
00016 #include <rpc/svc_soc.h>
00017 #endif
00018 
00019 #if 0
00020   /* This is why I hate CenterLine and sparcworks */
00021   /* There should be a directory in
00022      /depot/Centerline called include which
00023      should contain all of the correct
00024      prototypes. */
00025   void svc_unregister (ulong prognum, ulong versnum);
00026   SVCXPRT *svctcp_create (int sock, u_int sendsz, u_int recvsz);
00027   bool_t svc_register (SVCXPRT * xprt, ulong prognum, ulong versnum,
00028                        void (*dispatch) (svc_req *, SVCXPRT *),
00029                        ulong protocol);
00030 #endif
00031 
00032 #ifdef VXWORKS
00033 #include <rpcLib.h>             /* rpcTaskInit() */
00034 #include <taskLib.h>            /* taskIdSelf() */
00035 #endif
00036 #if defined(VXWORKS) || defined(linuxppc) || defined(linuxPPC)
00037 #ifdef svc_destroy
00038 #undef svc_destroy
00039 #define svc_destroy(xprt) \
00040   ((*(void (*)(SVCXPRT *))((xprt)->xp_ops->xp_destroy))(xprt))
00041 #endif
00042 #ifdef svc_getargs
00043 #undef svc_getargs
00044 #define svc_getargs(xprt,inproc,in) \
00045   ((*(bool_t (*)(SVCXPRT *,xdrproc_t, char *))((xprt)->xp_ops->xp_getargs))((xprt),(inproc), (in)))
00046 #endif
00047 #endif
00048 }
00049 #include "cms_srv.hh"           /* class CMS_SERVER */
00050 #include "rpc_srv.hh"           /* class CMS_SERVER_REMOTE_RPC_PORT  */
00051 #include "xdr_msg.hh"           /* xdr_REMOTE_READ_REQUEST(), */
00052                                 /* xdr_REMOTE_WRITE_REQUEST */
00053 #include "rcs_prnt.hh"          /* rcs_print_error() */
00054 
00055 CMS_SERVER_REMOTE_RPC_PORT::CMS_SERVER_REMOTE_RPC_PORT (CMS_SERVER *
00056                                                         _cms_server_parent):CMS_SERVER_REMOTE_PORT
00057   (_cms_server_parent)
00058 {
00059   prog_num = 0;
00060   xprt = NULL;
00061 }
00062 
00063 CMS_SERVER_REMOTE_RPC_PORT::~CMS_SERVER_REMOTE_RPC_PORT ()
00064 {
00065   unregister_port ();
00066 }
00067 
00068 int
00069 CMS_SERVER_REMOTE_RPC_PORT::accept_local_port_cms (CMS * _cms)
00070 {
00071   if (NULL == _cms)
00072     {
00073       return 0;
00074     }
00075 
00076   if (_cms->remote_port_type != CMS_RPC_REMOTE_PORT_TYPE)
00077     {
00078       return 0;
00079     }
00080 
00081   if (prog_num == 0)
00082     {
00083       prog_num = _cms->rpc_program_number;
00084       return 1;
00085     }
00086 
00087   if (prog_num == _cms->rpc_program_number)
00088     {
00089       return 1;
00090     }
00091   return 0;
00092 }
00093 
00094 void
00095 CMS_SERVER_REMOTE_RPC_PORT::run ()
00096 {
00097   cms_server_count++;
00098   svc_run ();
00099 }
00100 
00101 void
00102 CMS_SERVER_REMOTE_RPC_PORT::unregister_port ()
00103 {
00104   if (0 != prog_num)
00105     {
00106       svc_unregister (prog_num, 1);
00107       prog_num = 0;
00108     }
00109   if (NULL != xprt)
00110     {
00111       svc_destroy (xprt);
00112       xprt = NULL;
00113     }
00114   port_registered = 0;
00115 }
00116 
00117 void
00118 CMS_SERVER_REMOTE_RPC_PORT::register_port ()
00119 {
00120   port_registered = 0;
00121 
00122 #ifdef VXWORKS
00123   /* In VxWorks, always call rpcTaskInit() before using any other RPC call. */
00124   if (rpcTaskInit () < 0)
00125     {
00126       rcs_print_error ("CMS_SERVER_REMOTE_RPC_PORT: rpcTaskInit failed.\n");
00127       return;
00128     }
00129 #endif
00130 
00131   /* Open a TCP/IP port to listen on. */
00132   xprt = (SVCXPRT *) svctcp_create (RPC_ANYSOCK, 0, 0);
00133   if (xprt == NULL)
00134     {
00135       rcs_print_error ("server: Can't create TCP socket.\n");
00136       cms_server_parent->clean (2);
00137       return;
00138     }
00139 
00140   /* Register with the RPC port_map facility. */
00141 #if defined(sunos5) || defined(SGI) || defined(sparcworks) || defined(linux)
00142   if (svc_register (xprt, prog_num, 1, dispatch, IPPROTO_TCP) != TRUE)
00143 #else
00144   if (svc_register (xprt, prog_num, 1, (void (*)()) dispatch,
00145                     IPPROTO_TCP) != TRUE)
00146 #endif
00147     {
00148       rcs_print_error
00149         ("server: Can't register with RPC portmapper with program number %ld(0x%lX)\n",
00150          prog_num, prog_num);
00151       cms_server_parent->clean (2);
00152       return;
00153     }
00154   port_registered = 1;
00155 }
00156 
00157 /* Receive RPC calls and call the appropriate  CMS_SERVER_LOCAL_PORT function. */
00158 void
00159 CMS_SERVER_REMOTE_RPC_PORT::dispatch (svc_req * request,
00160                                       SVCXPRT * dispatch_xprt)
00161 {
00162 
00163   CMS_SERVER *cms_server;
00164 
00165 #ifdef VXWORKS
00166   int pid = taskIdSelf ();
00167 #else
00168   pid_t pid = getpid ();
00169 #endif
00170 
00171   rcs_print_debug (PRINT_RPC_SERVER_CALL, "RPC Server called.\n");
00172   rcs_print_debug (PRINT_RPC_SERVER_CALL, "(pid = %d, rq_proc = %d)\n",
00173                    pid, request->rq_proc);
00174 
00175   cms_server = find_server (pid);
00176   if (NULL == cms_server)
00177     {
00178       rcs_print_error
00179         ("Process Identifier(pid=%ld) does not match any on list of registered CMS servers.\n",
00180          pid);
00181       print_servers ();
00182       return;
00183     }
00184 #if 0
00185 #if defined(sunos5) || defined(sunos5CC)
00186   cms_server->remote_port->current_user_info =
00187     cms_server->remote_port->get_connected_user (dispatch_xprt->xp_fd);
00188 #endif
00189 #endif
00190 
00191   switch (request->rq_proc)
00192     {
00193     case 1:                     /* cms_reader */
00194       /* Get the request data  sent by remote reader. */
00195       if (svc_getargs (dispatch_xprt, (xdrproc_t) xdr_REMOTE_READ_REQUEST,
00196                        (char *) &cms_server->read_req) != TRUE)
00197         {
00198           rcs_print_error ("CMSserver: Couldn't getargs.\n");
00199           svcerr_decode (dispatch_xprt);
00200           return;
00201         }
00202 
00203 #if 0
00204       /* Read from local CMS buffer. */
00205       cms_local_port =
00206         cms_server->find_local_port (cms_server->read_req.buffer_number);
00207       if (NULL == cms_local_port)
00208         {
00209           rcs_print_error ("CMSserver: Invalid buffer number (%d).\n",
00210                            cms_server->read_req.buffer_number);
00211           svcerr_systemerr (dispatch_xprt);
00212           return;
00213         }
00214       read_reply = cms_local_port->reader (&cms_server->read_req);
00215 #endif
00216       cms_server->read_reply =
00217         (REMOTE_READ_REPLY
00218          *) (cms_server->process_request (&cms_server->read_req));
00219 
00220       if (NULL == cms_server->read_reply)
00221         {
00222           rcs_print_error ("CMSserver: Reader returned NULL.\n");
00223           svcerr_systemerr (dispatch_xprt);
00224           return;
00225         }
00226 
00227       /* Send buffer back to remote reader. */
00228       if (svc_sendreply (dispatch_xprt, (xdrproc_t) xdr_REMOTE_READ_REPLY,
00229                          (char *) cms_server->read_reply) != TRUE)
00230         {
00231           rcs_print_error ("CMSserver: Couldn't sendreply.\n");
00232           return;
00233         }
00234       break;
00235 
00236     case 2:                     /* cms_writer */
00237       /* Get the buffer that the remote writer is writing. */
00238       if (svc_getargs (dispatch_xprt, (xdrproc_t) xdr_REMOTE_WRITE_REQUEST,
00239                        (char *) &cms_server->write_req) != TRUE)
00240         {
00241           rcs_print_error ("CMSserver: Couldn't getargs.\n");
00242           svcerr_decode (dispatch_xprt);
00243           return;
00244         }
00245 
00246       /* Write to local CMS buffer. */
00247 #if 0
00248       cms_local_port =
00249         cms_server->find_local_port (cms_server->write_req.buffer_number);
00250       if (NULL == cms_local_port)
00251         {
00252           rcs_print_error ("CMSserver: Invalid buffer number (%d).\n",
00253                            cms_server->write_req.buffer_number);
00254           svcerr_systemerr (dispatch_xprt);
00255           return;
00256         }
00257       write_reply = cms_local_port->writer (&cms_server->write_req);
00258 #endif
00259       cms_server->write_reply =
00260         (REMOTE_WRITE_REPLY
00261          *) (cms_server->process_request (&cms_server->write_req));
00262 
00263 
00264       if (NULL == cms_server->write_reply)
00265         {
00266           rcs_print_error ("CMSserver: Writer returned NULL.\n");
00267           svcerr_systemerr (dispatch_xprt);
00268           return;
00269         }
00270 
00271       /* Send reply  back to the remote writer. */
00272       if (svc_sendreply (dispatch_xprt, (xdrproc_t) xdr_REMOTE_WRITE_REPLY,
00273                          (char *) cms_server->write_reply) != TRUE)
00274         {
00275           rcs_print_error ("CMSserver: Couldn't sendreply.\n");
00276           return;
00277         }
00278       break;
00279     case 3:                     /* check_if_read */
00280       /* Get the buffer that the remote writer is writing. */
00281       if (svc_getargs (dispatch_xprt, (xdrproc_t) xdr_long,
00282                        (char *) &cms_server->check_if_read_req.
00283                        buffer_number) != TRUE)
00284         {
00285           rcs_print_error ("CMSserver: Couldn't getargs.\n");
00286           svcerr_decode (dispatch_xprt);
00287           return;
00288         }
00289 
00290 #if 0
00291       cms_local_port = cms_server->find_local_port (long_arg);
00292       if (NULL == cms_local_port)
00293         {
00294           rcs_print_error ("CMSserver: Invalid buffer number (%d).\n",
00295                            long_arg);
00296           svcerr_systemerr (dispatch_xprt);
00297           return;
00298         }
00299       int_ret = cms_local_port->cms->check_if_read ();
00300 #endif
00301       cms_server->check_if_read_reply =
00302         (REMOTE_CHECK_IF_READ_REPLY
00303          *) (cms_server->process_request (&cms_server->check_if_read_req));
00304 
00305       /* Send an integer back to the remote writer. */
00306       if (svc_sendreply (dispatch_xprt, (xdrproc_t) xdr_int,
00307                          (char *) &cms_server->check_if_read_reply->
00308                          was_read) != TRUE)
00309         {
00310           rcs_print_error ("CMSserver: Couldn't sendreply.\n");
00311           return;
00312         }
00313       break;
00314     case 4:                     /* clean */
00315       cms_server->spawner_pid = cms_server->server_pid;
00316       cms_server->kill_server ();
00317       break;
00318     case 5:                     /* clear */
00319       /* Get the buffer that the remote writer is writing. */
00320       if (svc_getargs (dispatch_xprt, (xdrproc_t) xdr_long,
00321                        (char *) &cms_server->clear_req.buffer_number) != TRUE)
00322         {
00323           rcs_print_error ("CMSserver: Couldn't getargs.\n");
00324           svcerr_decode (dispatch_xprt);
00325           return;
00326         }
00327 
00328 #if 0
00329       cms_local_port = cms_server->find_local_port (long_arg);
00330       if (NULL == cms_local_port)
00331         {
00332           rcs_print_error ("CMSserver: Invalid buffer number (%d).\n",
00333                            long_arg);
00334           svcerr_systemerr (dispatch_xprt);
00335           return;
00336         }
00337       int_ret = cms_local_port->cms->clear ();
00338 #endif
00339       cms_server->clear_reply =
00340         (REMOTE_CLEAR_REPLY
00341          *) (cms_server->process_request (&cms_server->clear_req));
00342 
00343 
00344       /* Send an integer back to the remote writer. */
00345       if (svc_sendreply (dispatch_xprt, (xdrproc_t) xdr_int,
00346                          (char *) &cms_server->clear_reply->status) != TRUE)
00347         {
00348           rcs_print_error ("CMSserver: Couldn't sendreply.\n");
00349           return;
00350         }
00351       break;
00352     case 6:                     /* Get Keys */
00353       if (svc_getargs (dispatch_xprt, (xdrproc_t) xdr_REMOTE_GET_KEYS_REQUEST,
00354                        (char *) &cms_server->get_keys_req) != TRUE)
00355         {
00356           rcs_print_error ("CMSserver: Couldn't getargs.\n");
00357           svcerr_decode (dispatch_xprt);
00358           return;
00359         }
00360 
00361       /* Write to local CMS buffer. */
00362       cms_server->get_keys_reply =
00363         (REMOTE_GET_KEYS_REPLY
00364          *) (cms_server->process_request (&cms_server->get_keys_req));
00365 
00366 
00367       if (NULL == cms_server->get_keys_reply)
00368         {
00369           rcs_print_error ("CMSserver: Get_Keys returned NULL.\n");
00370           svcerr_systemerr (dispatch_xprt);
00371           return;
00372         }
00373 
00374       /* Send reply  back to the remote get_keysr. */
00375       if (svc_sendreply (dispatch_xprt, (xdrproc_t) xdr_REMOTE_GET_KEYS_REPLY,
00376                          (char *) cms_server->get_keys_reply) != TRUE)
00377         {
00378           rcs_print_error ("CMSserver: Couldn't sendreply.\n");
00379           return;
00380         }
00381       break;
00382     case 7:                     /* Login */
00383       if (svc_getargs (dispatch_xprt, (xdrproc_t) xdr_REMOTE_LOGIN_REQUEST,
00384                        (char *) &cms_server->login_req) != TRUE)
00385         {
00386           rcs_print_error ("CMSserver: Couldn't getargs.\n");
00387           svcerr_decode (dispatch_xprt);
00388           return;
00389         }
00390 
00391       /* Write to local CMS buffer. */
00392       cms_server->login_reply =
00393         (REMOTE_LOGIN_REPLY
00394          *) (cms_server->process_request (&cms_server->login_req));
00395 
00396 
00397       if (NULL == cms_server->login_reply)
00398         {
00399           rcs_print_error ("CMSserver: Login returned NULL.\n");
00400           svcerr_systemerr (dispatch_xprt);
00401           return;
00402         }
00403 
00404       /* Send reply  back to the remote loginr. */
00405       if (svc_sendreply (dispatch_xprt, (xdrproc_t) xdr_REMOTE_LOGIN_REPLY,
00406                          (char *) cms_server->login_reply) != TRUE)
00407         {
00408           rcs_print_error ("CMSserver: Couldn't sendreply.\n");
00409           return;
00410         }
00411       break;
00412 
00413     default:
00414       rcs_print_error ("CMS_server: Invalid RPC procedure number. (%d)\n",
00415                        request->rq_proc);
00416       svcerr_noproc (dispatch_xprt);
00417       break;
00418     }
00419 }

Generated on Sun Dec 2 15:56:52 2001 for rcslib by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001