home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format 56 / af056sub.adf / parnfs.lha / netio.c < prev    next >
C/C++ Source or Header  |  1993-12-02  |  5KB  |  243 lines

  1. /*
  2.  * $Id: netio.c,v 1.1 1993/12/02 20:45:46 Rhialto Exp $
  3.  * $Log: netio.c,v $
  4.  * Revision 1.1  1993/12/02  20:45:46  Rhialto
  5.  * Initial revision
  6.  *
  7.  *
  8.  * Network file system: network I/O. This version for PARNET.DEVICE.
  9.  */
  10.  
  11. #include "netfs.h"
  12. #include "devices/parnet.h"
  13.  
  14. #ifdef DEBUG
  15. #   include "syslog.h"
  16. #else
  17. #   define    debug(x)
  18. #endif
  19.  
  20. Prototype struct MsgPort *Port;
  21. Prototype struct MsgPort *RdPort;
  22. Prototype struct IOParReq *ParReq;
  23. Prototype struct IOParReq *RdParReq[PENDREADS];
  24. Prototype Packet     *RdPkt[PENDREADS];
  25. Prototype int          MyAddr;
  26. Prototype int          PeerAddr;
  27. Prototype int          NetFSPort;
  28.  
  29. struct MsgPort *Port;
  30. struct MsgPort *RdPort;
  31. struct IOParReq *ParReq;
  32. struct IOParReq *RdParReq[PENDREADS];
  33. Packet           *RdPkt[PENDREADS];
  34. int        MyAddr;
  35. int        PeerAddr;
  36. int        NetFSPort;
  37.  
  38. Prototype void SendRead(struct IOParReq *io, Packet *pkt, ULONG size);
  39.  
  40. void
  41. SendRead(struct IOParReq *io, Packet *pkt, ULONG size)
  42. {
  43.     io->io_Command = CMD_READ;
  44.     io->io_Data    = pkt;
  45.     io->io_Length  = size;
  46.     io->io_Data2   = NULL;
  47.     io->io_Length2 = 0;
  48.  
  49.     SendIO((struct IORequest *)io);
  50. }
  51.  
  52. Prototype LONG DoReset(struct IOParReq *io);
  53.  
  54. LONG
  55. DoReset(struct IOParReq *io)
  56. {
  57.     char        pkt[STDREPLY];
  58.     Packet       *p = (Packet *)pkt;
  59.     struct        DateStamp ds;
  60.  
  61.     p->p_Type = pt_Reset;
  62.     p->p_Origin = MyAddr;
  63.  
  64.     DateStamp(&ds);
  65.  
  66.     if (MyValidation == 0)
  67.     MyValidation = (ds.ds_Days * 24*60 +
  68.             ds.ds_Minute) * 60 +
  69.                ds.ds_Tick / TICKS_PER_SECOND;
  70.     p->p_Validation = MyValidation;
  71.  
  72.     debug(("DoReset %d\n", MyValidation));
  73.  
  74.     io->io_Command = CMD_WRITE;
  75.     io->io_Data    = pkt;
  76.     io->io_Length  = sizeof(pkt);
  77.     io->io_Data2   = NULL;
  78.     io->io_Length2 = 0;
  79.  
  80.     return DoIO((struct IORequest *)io);
  81. }
  82.  
  83. Prototype LONG DoResetReply(struct IOParReq *io);
  84.  
  85. LONG
  86. DoResetReply(struct IOParReq *io)
  87. {
  88.     char        pkt[STDREPLY];
  89.     Packet       *p = (Packet *)pkt;
  90.  
  91.     p->p_Type = pt_ResetReply;
  92.     p->p_Origin = MyAddr;
  93.  
  94.     p->p_Validation = MyValidation;
  95.  
  96.     debug(("DoResetReply %d\n", MyValidation));
  97.  
  98.     io->io_Command = CMD_WRITE;
  99.     io->io_Data    = pkt;
  100.     io->io_Length  = sizeof(pkt);
  101.     io->io_Data2   = NULL;
  102.     io->io_Length2 = 0;
  103.  
  104.     return DoIO((struct IORequest *)io);
  105. }
  106.  
  107. Prototype LONG DoProtocol(struct IOParReq *io, Packet *pkt);
  108.  
  109. LONG
  110. DoProtocol(struct IOParReq *io, Packet *pkt)
  111. {
  112.     switch (pkt->p_Type) {
  113.     case pt_Reply:
  114.     return pt_Reply;
  115.     case pt_Request:
  116.     return pt_Request;
  117.     case pt_AsyncReply:
  118.     DoAsyncReply(pkt);      /* Different in client and server */
  119.     return pt_Retry;
  120.     break;
  121.     case pt_Reset:
  122.     /* Oops! Server reset! */
  123.     Validation = pkt->p_Validation;
  124.     DoResetReply(io);
  125.     return pt_Reset;    /* No use in retrying */
  126.     case pt_ResetReply:
  127.     Validation = pkt->p_Validation;
  128.     debug(("got ResetReply %d\n", Validation));
  129.     /* fall through */
  130.     default:
  131.     /* Ignore bad packets, including mistaken pt_Requests */
  132.     debug(("Must re-read a packet\n"));
  133.     return pt_Retry;
  134.     }
  135. }
  136.  
  137. Prototype LONG InitReads(void);
  138.  
  139. LONG
  140. InitReads(void)
  141. {
  142.     int         i;
  143.  
  144.     for (i = 0; i < PENDREADS; i++) {
  145.     if ((RdPkt[i] = AllocMem(PKTSIZE, MEMF_PUBLIC)) == NULL)
  146.         return 4;
  147.     }
  148.  
  149.     for (i = 0; i < PENDREADS; i++) {
  150.     SendRead(RdParReq[i], RdPkt[i], PKTSIZE);
  151.     }
  152.  
  153.     return 0;
  154. }
  155.  
  156. Prototype ULONG OpenNetwork(void);
  157.  
  158. ULONG
  159. OpenNetwork(void)
  160. {
  161.     ULONG        error;
  162.     int         i;
  163.  
  164.     Port = CreatePort("Net:NetIO", -1);
  165.     if (Port == NULL)
  166.     return 1;
  167.     RdPort = CreatePort("Net:RdNetIO", -1);
  168.     if (RdPort == NULL)
  169.     return 1;
  170.     ParReq = CreateExtIO(Port, sizeof(*ParReq));
  171.     if (ParReq == 0)
  172.     return 2;
  173.  
  174.     ParReq->io_Port = NetFSPort;
  175.     ParReq->io_Flags = PRO_DGRAM;
  176.     ParReq->io_Addr = PeerAddr;
  177.  
  178.     debug(("MyAddr %d PeerAddr %d Port %d\n", MyAddr, PeerAddr, NetFSPort));
  179.  
  180.     error = OpenDevice(DevName, 0, (struct IORequest *)ParReq, 0);
  181.     if (error)
  182.     return error;
  183.  
  184.     ParReq->io_Command = PPD_SETADDR;
  185.     ParReq->io_Addr = MyAddr;
  186.     DoIO((struct IORequest *)ParReq);
  187.  
  188.     ParReq->io_Addr = PeerAddr;
  189.  
  190.     if ((RdParReq[0] = CreateExtIO(RdPort, sizeof(*ParReq))) == NULL)
  191.     return 3;
  192.  
  193.     RdParReq[0]->io_Device = ParReq->io_Device;
  194.     RdParReq[0]->io_Unit   = ParReq->io_Unit;
  195.     RdParReq[0]->io_Port   = NetFSPort;     /* not needed, just tidy */
  196.     RdParReq[0]->io_Flags  = IOF_QUICK;
  197.  
  198.     for (i = 1; i < PENDREADS; i++) {
  199.     if ((RdParReq[i] = CreateExtIO(RdPort, sizeof(*ParReq))) == NULL)
  200.         return 4;
  201.     *RdParReq[i] = *RdParReq[0];
  202.     }
  203.  
  204.     InitReads();
  205.     DoReset(ParReq);
  206.  
  207.     return 0;
  208. }
  209.  
  210. Prototype ULONG CloseNetwork(void);
  211.  
  212. ULONG
  213. CloseNetwork(void)
  214. {
  215.     int         i;
  216.  
  217.     if (ParReq) {
  218.     if (ParReq->io_Device) {
  219.         CloseDevice((struct IORequest *)ParReq);
  220.     }
  221.     DeleteExtIO((struct IORequest *)ParReq);
  222.     ParReq = NULL;
  223.     }
  224.     for (i = 0; i < PENDREADS; i++) {
  225.     if (RdParReq[i]) {
  226.         AbortIO((struct IORequest *)RdParReq[i]);
  227.         WaitIO((struct IORequest *)RdParReq[i]);
  228.         DeleteExtIO((struct IORequest *)RdParReq[i]);
  229.     }
  230.     RdParReq[i] = NULL;
  231.     }
  232.     if (RdPort) {
  233.     DeletePort(RdPort);
  234.     RdPort = NULL;
  235.     }
  236.     if (Port) {
  237.     DeletePort(Port);
  238.     Port = NULL;
  239.     }
  240.     return 0;
  241. }
  242.  
  243.