/*
   RPCPktIOb.c
 L. Stewart March 31, 1983  2:56 PM, split from rpcpktio.c.
 L. Stewart May 4, 1983  3:38 PM, BindingRopes.
 L. Stewart June 6, 1983  11:06 AM, 256 byte Pups.
 */
#include <Context.h>
#include <Queue.h>
#include <Env.h>
#include <Signal.h>
#include <rpc.h>
#include <rpcinternal.h>
#include <rpclupine.h>
#include <rpcpkt.h>

extern EnqueueAgain();
extern EnqueueBcst();
extern ServerMain();
extern struct Queue idlerQ;
extern struct Queue *myCtxQ;
extern struct Machine myHost;
extern BindingInitialize();
extern SecurityInitialize();

struct Port {
  union Machine m;
  struct Socket s;
  };

RPCInitializeCtx(hearBroadcasts)
  int hearBroadcasts;
  {
  int i, tmr;
  struct Port port;
  struct Machine m;
  InitPupLevel1 (myCtxQ, 28, 230); /* for 256 byte ether packets */
  for (i = 0; i < 4; i += 1) {
    RequestRoute(0);	/* Initialize Routing tables */
    Dismiss(100); /* Wait for multiple response packets to arrive */
    /* Based on a superstition that late arrivals mess up the time stuff */
    SetTmr(2000, &tmr);
    while (!TmrExp(&tmr)) {
      m.w = GetPupHost();
      if (m.b.net != 0) goto Break2;
      Block();
      };
    };
Break2:
  InitCalendar(4);	/* Get time of day set up */
  port.m.w = myHost.w = GetPupHost();
  port.s.ms = port.s.midS = 0;
  port.s.LS = rpcSocket;
  OpenLevel1Socket(&port, &EnqueueAgain, 0);
  port.s.LS = rpcBcstSocket;
  if (hearBroadcasts) OpenLevel1Socket(&port, &EnqueueBcst, 0);
  BindingRopes();
  AgentRopes();
  BindingInitialize();
  SecurityInitialize();
  StreamInitialize();
  InitQueue(&idlerQ);
  for (i = 1; i < 4; i += 1)
    StartNProcess("RPCServer", myCtxQ, &ServerMain, 300, 90);
  };

RPCRestart()
  {
  StreamRestart();   /* forget connection counts */
  SecurityRestart(); /* invalidate local conversations */
  BindingRestart();  /* nothing? */
  };