home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / sys / amiga / programm / 18333 < prev    next >
Encoding:
Text File  |  1993-01-08  |  5.5 KB  |  184 lines

  1. Newsgroups: comp.sys.amiga.programmer
  2. Path: sparky!uunet!gatech!usenet.ins.cwru.edu!magnus.acs.ohio-state.edu!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!usenet.ucs.indiana.edu!navajo!shulick
  3. From: shulick@navajo.ucs.indiana.edu (Sam Hulick)
  4. Subject: Enough talk.  FILE: server.c
  5. Message-ID: <C0IyF5.9Gv@usenet.ucs.indiana.edu>
  6. Originator: shulick@navajo
  7. Sender: news@usenet.ucs.indiana.edu (USENET News System)
  8. Nntp-Posting-Host: navajo.ucs.indiana.edu
  9. Reply-To: shulick@navajo.ucs.indiana.edu
  10. Organization: Vallen Software
  11. Date: Fri, 8 Jan 1993 07:35:28 GMT
  12. Lines: 170
  13.  
  14.  
  15. Well, I've complained enough about memory leaks.  =u)  Here's the server
  16. I wrote that is faulty.  About 8k bytes are not freed, and I can't
  17. figure out why.
  18.  
  19. Compiling instructions (SAS 5.10b)
  20. lc1 server
  21. lc2 server
  22. blink lib:c.o server.o lib lib:lc.lib lib:amiga.lib DEFINE __main=__tinymain
  23. (6.0/6.1 users, I think NOSTDOUT is what you do to make it detach)
  24.  
  25. Note, there's a sleep() function in here.  sorry.  =u)  It's in one of
  26. my custom libraries.  sleep(x) = Delay(50*x), that's all.  PLEASE
  27. contact me if you find out why this program leaks.  Thank you.
  28.  
  29. (two files below: server.c foomsg.h)
  30. -----cut here-------------------------------------------
  31. /*** BEGIN SERVER.C ***/
  32. /* This code is free.  Sell it, change it, erase it, distribute it, puke on
  33. it.  I don't care!! :)  I'll try to comment this as best I can.. */
  34. /* oh, keep in mind, this was done w/ SAS/C 5.10b. */
  35.  
  36. #include <stdio.h>
  37. #include <exec/exec.h>
  38. #include <exec/ports.h>
  39. #include <exec/types.h>
  40. #include <exec/alerts.h>
  41. #include <clib/exec_protos.h>
  42. #include <intuition/intuition.h>
  43. #include "foomsg.h"
  44.  
  45. #define SERVER_RUNNING  0
  46. #define SERVER_OK       1
  47. #define SERVER_FULL     2
  48.  
  49. void FlushPort(struct MsgPort *);
  50. struct Library *IntuitionBase = NULL, *GfxBase = NULL;
  51. struct Window *win;
  52. struct MsgPort *myport;
  53. struct NewWindow newwin = {
  54.    0, 0, 320, 50, 0, 1, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  55.    0, 0, 0, 0, WBENCHSCREEN
  56. };
  57. struct EasyStruct ereq[3] = {
  58.    {
  59.       sizeof(struct EasyStruct), 0, "Problem",
  60.       "Server already running!\nShut down?",
  61.       "Yes|No"
  62.    },
  63.    {
  64.       sizeof(struct EasyStruct), 0, "Confirmation",
  65.       "Server is now running!",
  66.       "Groovy"
  67.    },
  68.    {
  69.       sizeof(struct EasyStruct), 0, "Problem",
  70.       "*grunt* This string is getting too big!\nShall I reset it?",
  71.       "Sure|No, just quit"
  72.    }
  73. };
  74.  
  75. void shutdown(struct MsgPort *);
  76.  
  77. void main(int ac, char **av)
  78. {
  79.    char catmsg[30+1] = {0};
  80.    struct FooMessage *msg;
  81.    int done = FALSE;
  82.  
  83.    if (!(IntuitionBase = (struct Library *)
  84.      OpenLibrary("intuition.library", 37)))
  85.    {
  86.       Alert(AG_OpenLib | AO_Intuition);
  87.       exit(0);
  88.    }
  89.    if (!(GfxBase = (struct Library *)OpenLibrary("graphics.library", 37)))
  90.    {
  91.       Alert(AG_OpenLib | AO_GraphicsLib);
  92.       exit(0);
  93.    }
  94.    Forbid();
  95.    myport = FindPort("Foobar!");  /*Is the server already running..? */
  96.    Permit();
  97.    if (myport)  /* Yup */
  98.    {
  99.       if (EasyRequest(NULL, &ereq[SERVER_RUNNING], NULL))
  100.          shutdown(myport);  /* Either shut down */
  101.       else  /* Or remain running */
  102.       {
  103.          CloseLibrary(GfxBase);
  104.          CloseLibrary(IntuitionBase);
  105.          exit(0);
  106.       }
  107.    }                                   /* If we don't exist, */
  108.    if (!(myport = CreateMsgPort()))  /* Create the port.. */
  109.    {
  110.       Alert(AN_CreatePort);
  111.       exit(0);
  112.    }
  113.    myport->mp_Node.ln_Name = "Foobar!";
  114.    myport->mp_Node.ln_Pri = 0;
  115.    AddPort(myport);  /* Now we're a public port, anyone can access us */
  116.  
  117.    EasyRequest(NULL, &ereq[SERVER_OK], NULL);  /* Tell user we're up! */
  118.  
  119.    while (!done) {
  120.       WaitPort(myport); /* Wait for an incoming message. */
  121.       while ((msg = (struct FooMsg *)GetMsg(myport)))  /* Got one...! */
  122.       {
  123.   /* Ignore the catmsg stuff.  I have a 'send' program where you */
  124.   /* can send strings to the server, which keeps concatenating them. */
  125.   /* In this case, the 'send' client is irrelevant. */
  126.          if (strlen(catmsg) + strlen(msg->message) > 30)
  127.          {
  128.             if (EasyRequest(NULL, &ereq[SERVER_FULL], NULL))
  129.                strcpy(catmsg, "\0");
  130.             else
  131.                done = TRUE;
  132.          }
  133.          else
  134.          {
  135.             if (!(win = (struct Window *)OpenWindow(&newwin)))
  136.             {
  137.                Alert(AN_OpenWindow);
  138.                exit(0);
  139.             }
  140.             strcat(catmsg, msg->message);
  141.             Move(win->RPort, 10, 20);
  142.             Text(win->RPort, catmsg, strlen(catmsg));
  143.             sleep(3);
  144.             CloseWindow(win);
  145.             if (!strcmp(msg->message, "QUIT"))
  146.                done = TRUE;
  147.          }
  148.       }
  149.   }
  150.   shutdown(myport);
  151. }
  152.  
  153. void shutdown(struct MsgPort *victim)
  154. {
  155.   FlushPort(victim);  /* Empty the queue (see func below) */
  156.   RemPort(victim);  /* We're no longer public.. */
  157.   DeleteMsgPort(victim); /* And now we don't exist. :) */
  158.   CloseLibrary(IntuitionBase);
  159.   CloseLibrary(GfxBase);
  160.   exit(0);
  161. }
  162.  
  163. void FlushPort(struct MsgPort *thePort)
  164. {
  165.    struct Message *mesg;
  166.  
  167.    while ((mesg = GetMsg(thePort)))  /* There's a message here.. */
  168.       ReplyMsg(mesg);              /* Kiss it goodbye */
  169. }
  170. /*** END OF SERVER.C ***/
  171.  
  172. /*** BEGIN FOOMSG.H ***/
  173. struct FooMessage
  174. {
  175.    struct Message msg;
  176.    char message[50];   /* so we can pass strings to the server */
  177. };                  /* and so the server can interpret them. */
  178. /* end */
  179. -- 
  180.     // Amiga 3000   ___ \ Sam Hulick: shulick@indiana.edu (NeXTmail OK!)
  181.    // 68030 25 MHz /__/\ \ My opinions wear combat boots.  Or whatever.
  182. \\// OS 2.04       \__\/ / "Walk!  Not bloody likely.  I am going in a
  183.  \/                     / taxi."  --George Bernard Shaw
  184.