/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* |_o_o|\\ Copyright (c) 1988 The Software Distillery.  All Rights Reserved *
* |. o.| ||          Written by Doug Walker                                 *
* | .  | ||          The Software Distillery                                *
* | o  | ||          235 Trillingham Lane                                   *
* |  . |//           Cary, NC 27513                                         *
* ======             BBS:(919)-471-6436                                     *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "netdnet.h"
#include "handler.h"

#ifdef CPR
char *dbgwind = "CON:0/0/640/160/NETDNET-HANDLER/a";
#endif

#if 1

int ReSync(global, ioptr)
GLOBAL global;
APTR ioptr;
{
   TermRDevice(global, 1);
   return(InitRDevice(global));
}

#else

int ReSync(global, ioptr)
GLOBAL global;
APTR ioptr;
{
   return(1);
}

#endif

int InitRDevice(global)
GLOBAL global;
{
   struct MsgPort *chan;
   struct NetNode *tmpnode;
   struct RPacket RP;
   static doinit = 1;

   if(!doinit) 
   {
      BUG(("InitRDevice: Init loop, exiting\n"))
      return(1);
   }

   doinit = 0;

   BUG(("InitRDevice: Initializing for DNET operation on unit %d\n",
        global->unitnum));

   BUGP("InitRDevice: Entry")

   if(!(chan=DOpen(NULL, (uword)(PORT_FHANDLER), 20, 15)))
   {
      BUG(("********ERROR: Can't DOpen!!\n"));
      BUGR("Can't open channel!")
   }
   if(chan) DQueue((struct DChannel *)chan, 32);/* Is this really necessary? */
   BUG(("DNET Channel %lx open\n", chan));

   if(!global->netchain.next)
   {
      if(tmpnode=AddNode(global, "\3Foo", (APTR)chan))
         strcpy(tmpnode->devname, "ROOT:");
   }
   else if(chan)
   {
      /* Preserve state of global->RP */
       memcpy((char *)&RP, (char *)&global->RP, sizeof(struct RPacket));

      for(tmpnode=global->netchain.next; tmpnode; tmpnode=tmpnode->next)
      {
         BUG(("ReSync: Trying to resync '%s'\n", tmpnode->devname))
   
         global->RP.Type = ACTION_NETWORK_KLUDGE;
         strcpy(global->RP.Data, tmpnode->devname);
         global->RP.DLen = strlen(global->RP.Data);
         tmpnode->RootLock.RDevice = (RPTR)1L;
         tmpnode->ioptr = (APTR)chan;
         tmpnode->status = NODE_UP;
         if(RemotePacket(global, &tmpnode->RootLock))
         {
            tmpnode->status = NODE_CRASHED;
            BUG(("ReSync: Failed\n"))
         }
      }
      memcpy((char *)&global->RP, (char *)&RP, sizeof(struct RPacket));
   }


   doinit = 1;

   BUGP("InitRDevice: Exit")

   return(chan != NULL);
}

int TermRDevice(global, status)
GLOBAL global;
int status;
{
   struct NetNode *netnode;

   for(netnode=global->netchain.next; netnode; netnode=netnode->next)
   {
      if(netnode->ioptr)
      {
         netnode->status = NODE_DEAD;
         if(!status) DClose((struct DChannel *)netnode->ioptr);
         netnode->ioptr = NULL;
      }
   }

   return(0);
}

void ActNetHello(global, pkt)
GLOBAL global;
struct DosPacket *pkt;
{
}
