home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / lan / chat / support.c < prev   
C/C++ Source or Header  |  1989-03-22  |  10KB  |  431 lines

  1. /******************************************************************************
  2.  *
  3.  *    Program Name:    TALK_TO.ME
  4.  *
  5.  *    Filename:    SUPPORT.C -- 10/25/88 4:00
  6.  *
  7.  *    Purpose:  Support routines for the TALK_TO.ME program
  8.  *
  9.  ******************************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <malloc.h>
  14. #include <nit.h>
  15. #include <ctype.h>
  16. #include "chat.h"
  17.  
  18.  
  19. ECB            sendECB[NUM_SEND_ECBS],    *ECBqueue[NUM_RECEIVE_ECBS];
  20. SPXHeader    sendHeader[NUM_SEND_ECBS];
  21. WORD        SPXConnectionNumber, Socket = SESSION_SOCKET, connectionID,
  22.             preferredConnectionID = 0, listeningConnection;
  23. BYTE        immediateAddress[6], connectionInfo[44], networkNumber[4];
  24. char        outChar[NUM_SEND_ECBS],    character,
  25.             hello[] = " wants to CHAT.  To answer, type HELLO ";
  26. int            RECEIVE_FLAG = FALSE, ACTIVE_CONNECTION = 1;
  27. extern int    sendRow, sendCol, receiveRow, receiveCol;
  28. NETWORK_NODE    destNode, callNode;
  29.  
  30. void       SetUpSendECB();
  31. void    SetUpReceiveECBs();
  32. void    SendPacket();
  33. void    TearDownConnection();
  34. ECB        *SetUpInitialECB();
  35. void    SendMessage();
  36. void    PrintCharacter();
  37. int        PollForMessage();
  38. void    ListenESR();
  39.  
  40. extern    ESRHandler();
  41. extern    void Update();
  42. extern    void ScrollUp();
  43. extern    void SetCursor();
  44. extern    DeleteChar();
  45.  
  46. ECB *SetUpInitialECB()
  47. {
  48.     static     ECB            initialECB;
  49.     static    SPXHeader    initialHeader;
  50.     unsigned    transportTime;
  51.     BYTE         immediateAddress[6];
  52.     
  53.     GetInternetAddress (listeningConnection, networkNumber, (BYTE *)&destNode);
  54.  
  55.  /* Put the destination header in the address */
  56.      memcpy(initialHeader.destinationNetwork, networkNumber, 4);
  57.     memcpy(initialHeader.destinationNode, &destNode, 6);
  58.     memcpy(&initialHeader.destinationSocket, &Socket, 2);
  59.     initialHeader.packetType = 5;
  60.     initialHeader.length = IntSwap(sizeof(SPXHeader));
  61.  
  62.  /* Now initialzie your initial ECB */
  63.     initialECB.socketNumber = Socket;
  64.     initialECB.inUseFlag = 0;
  65.     initialECB.ESRAddress = 0; /* no event service routine, just poll */
  66.     initialECB.fragmentCount = 1;
  67.     initialECB.fragmentDescriptor[0].address = &initialHeader;
  68.     initialECB.fragmentDescriptor[0].size = sizeof(initialHeader);
  69.  
  70.     return( (ECB *)&initialECB );
  71. }
  72.  
  73.  
  74. void SetUpReceiveECBs()
  75. {
  76.     int        i;
  77.     char    *inChar;
  78.     ECB            *receiveECB;
  79.     SPXHeader    *receiveHeader;
  80.  
  81.     for ( i = 0; i < NUM_RECEIVE_ECBS; i++ )
  82.     {
  83.         if( (receiveECB = (ECB *)calloc(1,sizeof(ECB)) ) == (ECB *)NULL )
  84.             Error("Out of memory during packet allocation",0);
  85.  
  86.         if( (receiveHeader = (SPXHeader *)calloc(1,sizeof(SPXHeader)) ) == (ECB *)NULL)
  87.             Error("Out of memory during packet allocation",0);
  88.  
  89.         if ( (inChar = (char *)calloc(1,sizeof(char)) ) == (ECB *)NULL)
  90.             Error("Out of memory during packet allocation",0);
  91.  
  92.         receiveHeader->packetType = (char)5;
  93.         receiveHeader->length = IntSwap(sizeof(SPXHeader));
  94.  
  95.         receiveECB->ESRAddress = (void far *)ESRHandler;
  96.         receiveECB->socketNumber = Socket;
  97.         memcpy(receiveECB->immediateAddress, &destNode, 6);
  98.         receiveECB->fragmentCount = 2;
  99.         receiveECB->fragmentDescriptor[0].address = receiveHeader;
  100.         receiveECB->fragmentDescriptor[0].size = sizeof(SPXHeader);
  101.         receiveECB->fragmentDescriptor[1].address = inChar;
  102.         receiveECB->fragmentDescriptor[1].size = sizeof(char);
  103.         SPXListenForSequencedPacket (receiveECB);
  104.     }
  105. }
  106.  
  107. void SetUpSendECB()
  108. {
  109.     int     i;
  110.  
  111.     for (i = 0; i < NUM_SEND_ECBS; i++)
  112.     {
  113.      /* Fill in your address */
  114.         IPXGetInternetworkAddress(sendHeader[i].sourceNetwork); /* includes the source node */
  115.         memcpy( &sendHeader[i].sourceSocket, &Socket, 2);
  116.  
  117.      /* Fill in the destination address */
  118.         memcpy(sendHeader[i].destinationNetwork, networkNumber, 4);
  119.         memcpy(sendHeader[i].destinationNode, &destNode, 6);
  120.         memcpy(&sendHeader[i].destinationSocket, &Socket, 2);
  121.         sendHeader[i].length = IntSwap(sizeof(SPXHeader));
  122.         sendHeader[i].packetType = 5;
  123.  
  124.      /* Now initialize the Send ECBs */
  125.         sendECB[i].ESRAddress = 0;     /* no routine, just polling */
  126.         sendECB[i].inUseFlag = 0;
  127.         sendECB[i].fragmentCount = 2;
  128.         sendECB[i].socketNumber = Socket;
  129.         memcpy(sendECB[i].immediateAddress, &destNode, 6);
  130.         sendECB[i].fragmentDescriptor[0].address = (sendHeader + i);
  131.         sendECB[i].fragmentDescriptor[0].size = sizeof(SPXHeader);
  132.         sendECB[i].fragmentDescriptor[1].address = (outChar + i);
  133.         sendECB[i].fragmentDescriptor[1].size = sizeof(char);
  134.     }
  135. }
  136.  
  137. int    PollForPacket()
  138. {
  139.     BYTE    dataType;
  140.     char    ch, *charPtr;
  141.     static int     out;
  142.  
  143.     if (RECEIVE_FLAG > 0)
  144.     {
  145.      /* Set the queue pointer */
  146.         if (++out >= 20)
  147.             out = 0;
  148.  
  149.      /* Get the data type */
  150.         dataType = (BYTE)( ((SPXHeader *)ECBqueue[out]->fragmentDescriptor[0].address)->dataStreamType);
  151.  
  152.         if (dataType == TERMINATING_PACKET ) 
  153.         {
  154.             Update("Connection has been terminated");    
  155.             return(0);
  156.         }    
  157.  
  158.      /* Get the character */
  159.          charPtr = (char *)(ECBqueue[out]->fragmentDescriptor[1].address);
  160.         memcpy (&character, charPtr, 1);
  161.     
  162.       /* Release the ECB and print the character */
  163.          SPXListenForSequencedPacket (ECBqueue[out]); 
  164.         --RECEIVE_FLAG;
  165.         PrintCharacter();
  166.     }
  167.     return(1);
  168. }
  169.  
  170.  
  171.  
  172. void PrintCharacter()
  173. {
  174.     int        i;
  175.     char    ch;
  176.  
  177.     CursorOff();
  178.      switch (character)
  179.     {
  180.         case TAB:
  181.             ClearReceiveBox();
  182.             break;
  183.           case ENTER:
  184.             receiveRow++;
  185.             if (receiveRow > LAST_RECEIVE_ROW) 
  186.             {
  187.                 receiveRow = LAST_RECEIVE_ROW;
  188.                 ScrollUp (1, TOP_RECEIVE_ROW, LAST_RECEIVE_ROW, FIRST_TEXT_COL, LAST_TEXT_COL);
  189.             }
  190.             receiveCol = FIRST_TEXT_COL;
  191.             break;
  192.         case BACKSPACE:
  193.             if (receiveCol > FIRST_TEXT_COL)
  194.                 DeleteChar (&receiveCol, &receiveRow);
  195.             break;
  196.         default:
  197.             if (receiveCol >= LAST_TEXT_COL)
  198.             {
  199.                 receiveCol = FIRST_TEXT_COL;
  200.                 receiveRow++;
  201.             }
  202.  
  203.             if (receiveRow > LAST_RECEIVE_ROW) 
  204.             {
  205.                 receiveRow = LAST_RECEIVE_ROW;
  206.                 ScrollUp(1,TOP_RECEIVE_ROW,LAST_RECEIVE_ROW,FIRST_TEXT_COL,LAST_TEXT_COL);
  207.             }
  208.             SetCursor(receiveRow, receiveCol++);
  209.             putchar(character);
  210.             break;
  211.     }
  212.     SetCursor(sendRow, sendCol);
  213.     CursorOn();
  214. }
  215.  
  216.  
  217. void ListenESR(receiveECBptr)
  218. ECB *receiveECBptr;
  219. {
  220.     SPXHeader    *header;
  221.     static int    in; 
  222.  
  223.     if (++in >= 20)
  224.         in = 0;
  225.  
  226.     ECBqueue[in] = receiveECBptr;        
  227.     ++RECEIVE_FLAG;
  228.     return;
  229. }
  230.  
  231.  
  232. void SendPacket(charBuffer)
  233. char *charBuffer;
  234. {
  235.     int     ccode = 0, i;
  236.  
  237.     i = FindECB();
  238.  
  239.  /* Check for a valid connection */
  240.     ccode = SPXGetConnectionStatus(SPXConnectionNumber, connectionInfo);
  241.      if (ccode)
  242.          Error("Invalid connection.");
  243.  
  244.  /* Send character */
  245.      memcpy( (outChar + i), charBuffer, 1);
  246.     SPXSendSequencedPacket (SPXConnectionNumber, (sendECB + i) );
  247.  
  248.      if(sendECB[i].completionCode != 0)
  249.         ACTIVE_CONNECTION = 0;
  250.     else
  251.         Update ("Connection is active.");
  252. }
  253.  
  254.  
  255.  
  256. void TearDownConnection()
  257. {
  258.     int     i;
  259.  
  260.     i = FindECB();
  261.  
  262.     sendECB[i].ESRAddress = 0;
  263.     sendECB[i].fragmentCount = 1;
  264.     sendECB[i].fragmentDescriptor[0].size = 42;
  265.  
  266.     SPXTerminateConnection( SPXConnectionNumber, (sendECB + i) );
  267.  
  268.  /* Wait until the Terminate has completed */
  269.     while ( sendECB[i].inUseFlag )
  270.         /* wait */;
  271.     
  272.     IPXCloseSocket(Socket);
  273.  
  274.     ClearScreen();
  275. }
  276.  
  277.  
  278. FindECB()
  279. {
  280.     int        ALL_ECBS_ARE_BUSY = 1, ECBnumber;
  281.  
  282.     while (ALL_ECBS_ARE_BUSY)
  283.     {
  284.         for (ECBnumber = 0; ECBnumber < NUM_SEND_ECBS; ECBnumber++)
  285.         {
  286.             if (!sendECB[ECBnumber].inUseFlag)
  287.             {
  288.                 ALL_ECBS_ARE_BUSY = FALSE;
  289.                 break;
  290.             }
  291.             else if (ECBnumber == (NUM_SEND_ECBS - 1) )
  292.                 Update ("Your partner has suspended the connection.  Please wait.");
  293.         }
  294.     }
  295.     return (ECBnumber);
  296. }
  297.  
  298. ParseDestination(server, user, command)
  299. char    *server, *user, *command;
  300. {
  301.     int        ch,    i = 0, j = 0;
  302.  
  303.     while ( (ch = *(command + i)) != '/' )
  304.     {
  305.         ch = toupper (ch);
  306.         *(server + i++) = (char)ch;
  307.         if (ch == '\0')
  308.         {
  309.             printf ("CHAT.EXE v1.01\n");
  310.             printf ("by Novell Technical Documentation\n\n");
  311.             printf ("Format: Chat fileserver/username\n");
  312.             printf ("Example: Chat Server_1/Rasputin\n");
  313.             exit (); 
  314.         }
  315.     }
  316.     *(server + i++) = '\0';
  317.  
  318.     while( (ch = *(command + i++)) != '\0' )
  319.     {
  320.         ch = toupper(ch);
  321.         *(user + j++) = (char)ch;
  322.     }
  323.     *(user + j) = '\0';
  324.  
  325. }
  326.  
  327.  
  328. CheckServer(originalServer, preferredServer)
  329. char    *originalServer, *preferredServer;
  330. {
  331.     char    key;
  332.     int        ccode;
  333.  
  334.     connectionID = GetPreferredConnectionID();
  335.     GetFileServerName(connectionID, originalServer);
  336.  
  337.     if( (strcmp(originalServer, preferredServer)) != 0 )
  338.     {
  339.         ccode = GetConnectionID(preferredServer, &preferredConnectionID);
  340.         if (ccode)
  341.         {
  342.             Error("You're not attached to that file server.");
  343.         }
  344.  
  345.         SetPreferredConnectionID(preferredConnectionID);
  346.     }
  347. }
  348.  
  349.  
  350. GetConnectionAndNode(preferredServer, userName, connection)
  351. char    *preferredServer, *userName;
  352. WORD    *connection;
  353.  
  354. {
  355.     WORD    *count, max = 100;
  356.     int        ccode;
  357.     char    networkNumber[4];
  358.     BYTE    address[6];
  359.  
  360.  /* Get the connection number of listening side */
  361.     ccode = GetObjectConnectionNumbers(userName, OT_USER, count,
  362.                 connection, max);
  363.  
  364.  /* Get the node address of the listening side */
  365.     GetInternetAddress( *(connection), networkNumber, (char *)&destNode);
  366.  
  367. /*     destNode.hiNode = IntSwap(destNode.hiNode);
  368.     destNode.loNode = LongSwap(destNode.loNode); */
  369. }
  370.  
  371.  
  372. SayHello(listeningConnection)
  373. WORD    *listeningConnection;
  374. {
  375.     BYTE    result[2], time[7], address[6];
  376.     WORD    callingConnection;
  377.     char    callerName[48], networkNumber[4], connString[3];
  378.     int        type, ccode;
  379.     long    IDNumber;
  380.  
  381.  /* Get connection number of calling side */
  382.     callingConnection = GetConnectionNumber();
  383.  
  384.  /* Use the connection number to get the name of the caller */
  385.     GetConnectionInformation(callingConnection, callerName, &type,
  386.             &IDNumber, time);
  387.  
  388.  /* Create broadcast message */
  389.       ConvertToString(&callingConnection, connString, 1); 
  390.     strncat(callerName, hello, sizeof(hello) );
  391.     strncat(callerName, connString, sizeof(connString));
  392.     ccode = SendBroadcastMessage(callerName, listeningConnection, result, 1);
  393.     return( *(result) );
  394. }
  395.  
  396. ConvertToString(number, string, size)
  397. char    *number,*string,size;
  398. {
  399.     char        i, j, k;
  400.     int            left = 1;
  401.  
  402.     for (i = j = k = 0; i < size; i++)
  403.     {
  404.         if (left)
  405.         {
  406.             j = *(number + i--);
  407.             j = j & 0x00F0;
  408.             j = j >> 4;
  409.             left--;
  410.         }
  411.  
  412.         else
  413.         {
  414.             j = *(number + i);
  415.             j = j & 0x000F;
  416.             left++;
  417.         }
  418.  
  419.         if (j < 10)
  420.              *(string + k++) = j + 0x30;
  421.         else
  422.              *(string + k++) = j + 0x37;
  423.  
  424.     }
  425.    *(string + k) = '\0';
  426.  
  427. }
  428.  
  429.  
  430.  
  431.