tac0S
Template Affectional Command Operating System
network.h
Go to the documentation of this file.
1 
18 #ifndef NETWORK_H
19 #define NETWORK_H
20 
21 #include "copyright.h"
22 #include "utility.h"
23 
26 typedef int NetworkAddress;
27 
33 
34 class PacketHeader {
35  public:
36  NetworkAddress to; // Destination machine ID
37  NetworkAddress from; // source machine ID
38  unsigned length; // bytes of packet data, excluding the
39  // packet header (but including the
40  // MailHeader prepended by the post office)
41 };
42 
43 #define MaxWireSize 64 // largest packet that can go out on the wire
44 #define MaxPacketSize (MaxWireSize - sizeof(struct PacketHeader))
45  // data "payload" of the largest packet
46 
47 
48 // The following class defines a physical network device. The network
49 // is capable of delivering fixed sized packets, in order but unreliably,
50 // to other machines connected to the network.
51 //
52 // The "reliability" of the network can be specified to the constructor.
53 // This number, between 0 and 1, is the chance that the network will lose
54 // a packet. Note that you can change the seed for the random number
55 // generator, by changing the arguments to RandomInit() in Initialize().
56 // The random number generator is used to choose which packets to drop.
57 
58 class Network {
59  public:
60  Network(NetworkAddress addr, double reliability,
61  VoidFunctionPtr readAvail, VoidFunctionPtr writeDone, int callArg);
62  // Allocate and initialize network driver
63  ~Network(); // De-allocate the network driver data
64 
65  void Send(PacketHeader hdr, char* data);
66  // Send the packet data to a remote machine,
67  // specified by "hdr". Returns immediately.
68  // "writeHandler" is invoked once the next
69  // packet can be sent. Note that writeHandler
70  // is called whether or not the packet is
71  // dropped, and note that the "from" field of
72  // the PacketHeader is filled in automatically
73  // by Send().
74 
75  PacketHeader Receive(char* data);
76  // Poll the network for incoming messages.
77  // If there is a packet waiting, copy the
78  // packet into "data" and return the header.
79  // If no packet is waiting, return a header
80  // with length 0.
81 
82  void SendDone(); // Interrupt handler, called when message is
83  // sent
84  void CheckPktAvail(); // Check if there is an incoming packet
85 
86  private:
87  NetworkAddress ident; // This machine's network address
88  double chanceToWork; // Likelihood packet will be dropped
89  int sock; // UNIX socket number for incoming packets
90  char sockName[32]; // File name corresponding to UNIX socket
91  VoidFunctionPtr writeHandler; // Interrupt handler, signalling next packet
92  // can be sent.
93  VoidFunctionPtr readHandler; // Interrupt handler, signalling packet has
94  // arrived.
95  int handlerArg; // Argument to be passed to interrupt handler
96  // (pointer to post office)
97  bool sendBusy; // Packet is being sent.
98  bool packetAvail; // Packet has arrived, can be pulled off of
99  // network
100  PacketHeader inHdr; // Information about arrived packet
101  char inbox[MaxPacketSize]; // Data for arrived packet
102 };
103 
104 #endif // NETWORK_H
int NetworkAddress
Network address – uniquely identifies a machine. This machine's ID is given on the command line...
Definition: network.h:26
The following class defines the network packet header. The packet header is prepended to the data pay...
Definition: network.h:34
Miscellaneous useful definitions, including debugging routines.
Definition: network.h:58
Definition: out.c:36