home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / emerald / emrldsys.lha / Kernel / KmdOps / unixkmdMM.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-30  |  9.6 KB  |  298 lines

  1.  
  2. /*  C O P Y R I G H T   N O T I C E :                                     */
  3. /* Copyright 1986 Eric Jul.  May not be used for any               */
  4. /* purpose without written permission from the author.              */
  5.  
  6. /* File: /usr/em/Kernel/KmdOps/kmdMM.c  Date: 1986-05-01  */
  7.  
  8. /*
  9.  * $Header$
  10.  * INTERFACE:    
  11.  *
  12.  * FUNCTION:    Takes care of sending messages for KMD.
  13.  *              Messages are sent using reliable byte streams using the
  14.  *              TCP/IP protocol.
  15.  *
  16.  * IMPORTS:    malloc, free, sys/types.h, sys/cmuipc.h, errno.h,
  17.  *              QueueTask, PutEdenMsg, HOTSSearchPtr,
  18.  *              /u1/Eden/ErrCodes/MMcodes.h,
  19.  *              /u1/Eden/ErrCodes/SYScodes.h,
  20.  *              /u1/Eden/Kernel/MsgOps/Types.h,
  21.  *              /u1/Eden/Kernel/MsgOps/BufferTypes.h,
  22.  *              /u1/Eden/Kernel/MsgOps/FifoTypes.h,
  23.  *              /u1/Eden/Kernel/MsgOps/mmMsgTypes.h,
  24.  *              /u1/Eden/Kernel/ProcessA/Events.h,
  25.  *
  26.  * EXPORTS:    KMDReceive, KMDSend, KMDBuildMsg, KMDSendBlock
  27.  *
  28.  * DESIGN:    A lot of code copied from the Message Module, (msgCode.c).
  29.  *
  30.  * $Log$
  31.  * 1986-05-01   Rewritten to use TCP/IP for ULTRIX 32m (and Berkeley 4.2bsd).
  32.  * 1985-02-21   Changed pack and unpack to allow for a string of bytes
  33.  *              instead of just a string of char.
  34.  *              Added KMDSendBlock.
  35.  * 1983-06-08   Initial Implementation, Eric Jul.
  36.  */
  37. #include <sys/types.h>
  38.  
  39. #undef integer
  40. #include <errno.h>
  41.  
  42. extern errno;  /* IPC status if call returns -1; NB: not set upon success!*/
  43.  
  44. extern char *malloc();
  45. extern int  free();
  46. extern void perror();
  47.  
  48. extern int  DisplayEdenMsg(),
  49.             GetEdenMsg(),
  50.             PutEdenMsg();
  51.  
  52. #include "Kernel/h/mmCodes.h"
  53. #include "Kernel/h/unixCodes.h"
  54.  
  55. #include "Kernel/h/mmTypes.h"
  56. #include "Kernel/h/mmMsgDefs.h"
  57. #include "Kernel/h/mmMsgTypes.h"
  58. #include "Kernel/h/kmdDefs.h"
  59.  
  60. /* Define KMD test output flag. */
  61. int KMDTest = 0;
  62.  
  63. /* InvByteOrder is a macro that given a byte index produces a corresponding
  64.    index for grouping into 4-byte integers. */
  65. #define InvByteOrder(i) (i+3-2*(i%4))
  66.  
  67.  
  68.  
  69. /************************************************************/
  70. /*                                                          */
  71. /*                     KMDPack                              */
  72. /*                                                          */
  73. /*  KMDPack takes three data items and packs them into a    */
  74. /*  KMDData packet.                                         */
  75. /*                                                          */
  76. /************************************************************/
  77.  
  78. void KMDPack(fType, fInt, fString, fMsgPtr)
  79.  
  80.     int         fType;
  81.     int         fInt;
  82.     char       *fString;
  83.     KMDMsgPtr   fMsgPtr;
  84.  
  85. {
  86.     register char *src, *dest;
  87.     if (KMDTest) printf("KMDPack (%d, %d, %s)\n", fType, fInt, fString);
  88.     fMsgPtr->data.MsgType       = fType;
  89.     fMsgPtr->data.MsgInt        = fInt;
  90.     src                         = fString;
  91.     dest                        = &fMsgPtr->data.MsgString[0];
  92.     while (*dest++ = *src++);
  93.     fMsgPtr->totalSize          = dest - (char *) (& fMsgPtr->totalSize);
  94.  
  95.     if ( KMDTest )
  96.         printf("  Packed total size %d\n", fMsgPtr->totalSize);
  97. }
  98.  
  99. #ifdef NOTIMPLEMENTED
  100.  
  101. /************************************************************/
  102. /*                                                          */
  103. /*                     KMDPackBlock                         */
  104. /*                                                          */
  105. /*  KMDPack packs a block into a KMDData packet.            */
  106. /*                                                          */
  107. /************************************************************/
  108.  
  109. void KMDPackBlock(fType, fSize, fBlock, fMsgPtr)
  110.  
  111.     int         fType;
  112.     int         fSize;
  113.     char        *fBlock;
  114.     KMDDataPtr  fMsgPtr;
  115. {
  116.     if (KMDTest) { register int i;
  117.         printf("KMDPackBlock (%d, %d, %d)", fType, fSize, fMsgPtr);
  118.     for (i=0; i < fSize; i++){
  119.         printf("%s%02x", (i % (8*4) ? (i % 4 ? "" : " ") : "\n"),
  120.             (unsigned char) fBlock[InvByteOrder(i)]);
  121.     };
  122.     printf("\n");
  123.     }
  124.     fMsgPtr -> MsgType     = fType;
  125.     fMsgPtr -> MsgInt      = fSize;
  126.     bcopy(fBlock, (char *) fMsgPtr->MsgString, fSize);
  127. }
  128. #endif NOTIMPLEMENTED
  129.  
  130. /************************************************************/
  131. /*                                                          */
  132. /*                      KMDSend                             */
  133. /*                                                          */
  134. /*  KMDSend packages and sends the three data via IPC to    */
  135. /*  specified port.                                         */
  136. /*                                                          */
  137. /************************************************************/
  138.  
  139.  
  140. KKStatus KMDSend(fDestSock, fType, fInt, fString)
  141. int fDestSock, fType, fInt;
  142. char *fString;
  143. {
  144.     KMDMsg          msg;
  145.     int             err;
  146.     if(KMDSTDOUTSOCK == fDestSock) {
  147.     printf("%s", fString);
  148.     (void) fflush(stdout);
  149.     return(MMSS_Success);
  150.     }
  151.     KMDPack(fType, fInt, fString, &msg);
  152.     do {
  153.     err = send(fDestSock, (char *) &msg, msg.totalSize, 0);
  154.     } while ((err < 0) && (errno == EINTR));
  155.     if (err < 0){
  156.     if (KMDTest || 1) perror("KMDShip: send");
  157.     return ( mSystemError( errno ) );
  158.     };
  159.     return MMSS_Success;
  160. }
  161.  
  162. #ifdef NOTIMPLEMENTED
  163. /************************************************************/
  164. /*                                                          */
  165. /*                      KMDSendBlock                        */
  166. /*                                                          */
  167. /*  KMDSend packages and sends a block of bytes via IPC to  */
  168. /*  the specified port.                                     */
  169. /*                                                          */
  170. /************************************************************/
  171.  
  172.  
  173. KKStatus KMDSendBlock(fSrcPort, fDestPort, fType, fSize, fBlock, fMsgPrio)
  174.  
  175.   localport     fSrcPort;
  176.   localport     fDestPort;
  177.   int           fType;
  178.   int           fSize;
  179.   char          *fBlock;
  180.   int           fMsgPrio;
  181.  
  182. {
  183.   int Size;
  184.   Message(MAXMESSAGESIZE)           Msg;
  185.   MessageHeaderPtr  MsgHdrPtr;
  186.   KMDDataType       Data1;
  187.   KMDDataPtr        Data1Ptr;
  188.  
  189.  
  190.     Data1Ptr    = &Data1;
  191.     
  192.     if (KMDTest)
  193.         printf("KMDSendBlock\n");
  194.     
  195.     KMDPackBlock(fType, fSize, fBlock, Data1Ptr);
  196. /*
  197.     KMDBuildMsg(fSrcPort, fDestPort, Data1Ptr, fSize+2*(sizeof (int)), &Msg);
  198. */
  199.     return ());
  200. }
  201.  
  202. #endif NOTIMPLEMENTED
  203.  
  204. /************************************************************/
  205. /*                                                          */
  206. /*                      KMDUnpack                           */
  207. /*                                                          */
  208. /*  Unpack a KMD data packet into the components.           */
  209. /*                                                          */
  210. /************************************************************/
  211.  
  212. void KMDUnpack(fMsgPtr, fType, fInt, fString)
  213.  
  214.   KMDMsgPtr          fMsgPtr;
  215.   int               *fType;
  216.   int               *fInt;
  217.   char               fString[];
  218.  
  219. {
  220.     register char *src, *dest;
  221.     if(KMDTest) printf("Unpack total length %d\n", fMsgPtr->totalSize);
  222.     *fType                      = fMsgPtr->data.MsgType;
  223.     *fInt                       = fMsgPtr->data.MsgInt;
  224.     src                         = &fMsgPtr->data.MsgString[0];
  225.     dest                        = fString;
  226.     while (*dest++ = *src++);
  227.     if ( KMDTest) {
  228.         printf("KMDUnpack: type=%d, Int=%d\n", *fType, *fInt);
  229. #ifdef NOTIMPLEMENTED
  230.     if (fMsgPtr->MsgType >= KMDCBYTES) {
  231.         /* Contains raw bytes instead of a string. */
  232.         register int i;
  233.         printf("Block:");
  234.             for (i=0; i < *fInt; i++) {
  235.             printf("%s%02x", i % (8*4) ? i % 4 ? "" : " " : "\n",
  236.                 (unsigned char) (fMsgPtr -> MsgString[InvByteOrder(i)]));
  237.         };
  238.         printf("\n");
  239.  
  240.     } else
  241. #endif NOTIMPLEMENTED
  242.         printf(" String=#%s#\n", fString);
  243.     }
  244. }
  245.  
  246. /************************************************************/
  247. /*                                                          */
  248. /*                      KMDReceive                          */
  249. /*                                                          */
  250. /*  Synchronously receive a KMD message on the DATAPORT     */
  251. /*  of the process.  Unpack it and return the components.   */
  252. /*                                                          */
  253. /************************************************************/
  254.  
  255. KKStatus KMDReceive(fRemotePort, fType, fInt, fString)
  256.  
  257.   int               fRemotePort;
  258.   int               *fType;
  259.   int               *fInt;
  260.   char              fString[];
  261. {
  262.   KMDMsg            msg;
  263.   int               noerror;
  264.   KKStatus          status;
  265.  
  266.     do {
  267.     noerror = recv(fRemotePort, (char *) &msg.totalSize, 4, 0);
  268.     } while ( (noerror < 0 ) && (errno == EINTR) );
  269.     if (noerror < 0) {
  270.         if (KMDTest) perror("KMDReceive: recv");
  271.     return (mSystemError(errno));
  272.     };
  273.     if (KMDTest) printf("KMDReceive msgSize = %d\n", msg.totalSize);
  274.     if ((msg.totalSize >= 5000) || (msg.totalSize < 5) ) return(MMSF_MsgOvfl);
  275.     do {
  276.     noerror = recv( fRemotePort, (char *) &msg.data, msg.totalSize-4, 0);
  277.         if ( KMDTest ) printf("Receive result: %d %d\n", noerror,
  278.             noerror < 0 ? errno : 0);
  279.     if ( noerror < 0  ) {
  280.         if (KMDTest) perror("KMDReceive: recv");
  281.                 status = mSystemError(errno);
  282.     } else     if ( noerror < 8 ) {
  283.         /* Too few bytes read */
  284.         status = MMSF_MsgOvfl;
  285.     } else status = MMSS_Success;
  286.  
  287.         if ( mSUCCESS(status) ) {
  288.                 KMDUnpack(&msg, fType, fInt, fString);
  289.         }
  290.     } while (status == SYSK_EINTR);
  291.     return (status);
  292. }
  293.  
  294. /*  C O P Y R I G H T   N O T I C E :                                     */
  295. /* Copyright 1986 Eric Jul.  May not be used for any               */
  296. /* purpose without written permission from the author.              */
  297.  
  298.