00001 // network.h 00002 // Data structures to emulate a physical network connection. 00003 // The network provides the abstraction of ordered, unreliable, 00004 // fixed-size packet delivery to other machines on the network. 00005 // 00006 // You may note that the interface to the network is similar to 00007 // the console device -- both are full duplex channels. 00008 // 00009 // DO NOT CHANGE -- part of the machine emulation 00010 // 00011 // Copyright (c) 1992-1996 The Regents of the University of California. 00012 // All rights reserved. See copyright.h for copyright notice and limitation 00013 // of liability and disclaimer of warranty provisions. 00014 00015 #ifndef NETWORK_H 00016 #define NETWORK_H 00017 00018 #include "copyright.h" 00019 #include "utility.h" 00020 #include "callback.h" 00021 00022 // Network address -- uniquely identifies a machine. This machine's ID 00023 // is given on the command line. 00024 typedef int NetworkAddress; 00025 00026 // The following class defines the network packet header. 00027 // The packet header is prepended to the data payload by the Network driver, 00028 // before the packet is sent over the wire. The format on the wire is: 00029 // packet header (PacketHeader) 00030 // data (containing MailHeader from the PostOffice!) 00031 00032 class PacketHeader { 00033 public: 00034 NetworkAddress to; // Destination machine ID 00035 NetworkAddress from; // source machine ID 00036 unsigned length; // bytes of packet data, excluding the 00037 // packet header (but including the 00038 // MailHeader prepended by the post office) 00039 }; 00040 00041 #define MaxWireSize 64 // largest packet that can go out on the wire 00042 #define MaxPacketSize (MaxWireSize - sizeof(struct PacketHeader)) 00043 // data "payload" of the largest packet 00044 00045 00046 // The following two classes defines a physical network device. The network 00047 // is capable of delivering fixed sized packets, in order but unreliably, 00048 // to other machines connected to the network. 00049 // 00050 // The "reliability" of the network can be specified to the constructor. 00051 // This number, between 0 and 1, is the chance that the network will lose 00052 // a packet. Note that you can change the seed for the random number 00053 // generator, by changing the arguments to RandomInit() in Initialize(). 00054 // The random number generator is used to choose which packets to drop. 00055 00056 class NetworkInput : public CallBackObj{ 00057 public: 00058 NetworkInput(CallBackObj *toCall); 00059 // Allocate and initialize network input driver 00060 ~NetworkInput(); // De-allocate the network input driver data 00061 00062 PacketHeader Receive(char* data); 00063 // Poll the network for incoming messages. 00064 // If there is a packet waiting, copy the 00065 // packet into "data" and return the header. 00066 // If no packet is waiting, return a header 00067 // with length 0. 00068 00069 void CallBack(); // A packet may have arrived. 00070 00071 private: 00072 int sock; // UNIX socket number for incoming packets 00073 char sockName[32]; // File name corresponding to UNIX socket 00074 00075 CallBackObj *callWhenAvail; // Interrupt handler, signalling packet has 00076 // arrived. 00077 bool packetAvail; // Packet has arrived, can be pulled off of 00078 // network 00079 PacketHeader inHdr; // Information about arrived packet 00080 char inbox[MaxPacketSize]; // Data for arrived packet 00081 }; 00082 00083 class NetworkOutput : public CallBackObj { 00084 public: 00085 NetworkOutput(double reliability, CallBackObj *toCall); 00086 // Allocate and initialize network output driver 00087 ~NetworkOutput(); // De-allocate the network input driver data 00088 00089 void Send(PacketHeader hdr, char* data); 00090 // Send the packet data to a remote machine, 00091 // specified by "hdr". Returns immediately. 00092 // "callWhenDone" is invoked once the next 00093 // packet can be sent. Note that callWhenDone 00094 // is called whether or not the packet is 00095 // dropped, and note that the "from" field of 00096 // the PacketHeader is filled in automatically 00097 // by Send(). 00098 00099 void CallBack(); // Interrupt handler, called when message is 00100 // sent 00101 00102 private: 00103 int sock; // UNIX socket number for outgoing packets 00104 double chanceToWork; // Likelihood packet will be dropped 00105 CallBackObj *callWhenDone; // Interrupt handler, signalling next packet 00106 // can be sent. 00107 bool sendBusy; // Packet is being sent. 00108 }; 00109 00110 #endif // NETWORK_H