Main Page | Alphabetical List | Class List | File List | Class Members | File Members

Network Class Reference

#include <network.h>

List of all members.

Public Member Functions

 Network (NetworkAddress addr, double reliability, VoidFunctionPtr readAvail, VoidFunctionPtr writeDone, int callArg)
 ~Network ()
void Send (PacketHeader hdr, char *data)
PacketHeader Receive (char *data)
void SendDone ()
void CheckPktAvail ()


Constructor & Destructor Documentation

Network::Network NetworkAddress  addr,
double  reliability,
VoidFunctionPtr  readAvail,
VoidFunctionPtr  writeDone,
int  callArg
 

Definition at line 25 of file network.cc.

References AssignNameToSocket(), FALSE, interrupt, PacketHeader::length, NetworkAddress, NetworkRecvInt, NetworkTime, OpenSocket(), Interrupt::Schedule(), and VoidFunctionPtr.

00027 { 00028 ident = addr; 00029 if (reliability < 0) chanceToWork = 0; 00030 else if (reliability > 1) chanceToWork = 1; 00031 else chanceToWork = reliability; 00032 00033 // set up the stuff to emulate asynchronous interrupts 00034 writeHandler = writeDone; 00035 readHandler = readAvail; 00036 handlerArg = callArg; 00037 sendBusy = FALSE; 00038 inHdr.length = 0; 00039 00040 sock = OpenSocket(); 00041 sprintf(sockName, "SOCKET_%d", (int)addr); 00042 AssignNameToSocket(sockName, sock); // Bind socket to a filename 00043 // in the current directory. 00044 00045 // start polling for incoming packets 00046 interrupt->Schedule(NetworkReadPoll, (int)this, NetworkTime, NetworkRecvInt); 00047 }

Network::~Network  ) 
 

Definition at line 49 of file network.cc.

References CloseSocket(), and DeAssignNameToSocket().

00050 { 00051 CloseSocket(sock); 00052 DeAssignNameToSocket(sockName); 00053 }


Member Function Documentation

void Network::CheckPktAvail  ) 
 

Definition at line 59 of file network.cc.

References ASSERT, DEBUG(), PacketHeader::from, interrupt, PacketHeader::length, MaxPacketSize, MaxWireSize, NetworkRecvInt, NetworkTime, Statistics::numPacketsRecvd, PollSocket(), ReadFromSocket(), Interrupt::Schedule(), stats, and PacketHeader::to.

00060 { 00061 // schedule the next time to poll for a packet 00062 interrupt->Schedule(NetworkReadPoll, (int)this, NetworkTime, NetworkRecvInt); 00063 00064 if (inHdr.length != 0) // do nothing if packet is already buffered 00065 return; 00066 if (!PollSocket(sock)) // do nothing if no packet to be read 00067 return; 00068 00069 // otherwise, read packet in 00070 char *buffer = new char[MaxWireSize]; 00071 ReadFromSocket(sock, buffer, MaxWireSize); 00072 00073 // divide packet into header and data 00074 inHdr = *(PacketHeader *)buffer; 00075 ASSERT((inHdr.to == ident) && (inHdr.length <= MaxPacketSize)); 00076 bcopy(buffer + sizeof(PacketHeader), inbox, inHdr.length); 00077 delete []buffer ; 00078 00079 DEBUG('n', "Network received packet from %d, length %d...\n", 00080 (int) inHdr.from, inHdr.length); 00081 stats->numPacketsRecvd++; 00082 00083 // tell post office that the packet has arrived 00084 (*readHandler)(handlerArg); 00085 }

PacketHeader Network::Receive char *  data  ) 
 

Definition at line 129 of file network.cc.

References PacketHeader::length.

00130 { 00131 PacketHeader hdr = inHdr; 00132 00133 inHdr.length = 0; 00134 if (hdr.length != 0) 00135 bcopy(inbox, data, hdr.length); 00136 return hdr; 00137 }

void Network::Send PacketHeader  hdr,
char *  data
 

Definition at line 102 of file network.cc.

References ASSERT, DEBUG(), FALSE, PacketHeader::from, interrupt, PacketHeader::length, MaxPacketSize, MaxWireSize, NetworkSendInt, NetworkTime, Random(), Interrupt::Schedule(), SendToSocket(), and PacketHeader::to.

00103 { 00104 char toName[32]; 00105 00106 sprintf(toName, "SOCKET_%d", (int)hdr.to); 00107 00108 ASSERT((sendBusy == FALSE) && (hdr.length > 0) 00109 && (hdr.length <= MaxPacketSize) && (hdr.from == ident)); 00110 DEBUG('n', "Sending to addr %d, %d bytes... ", hdr.to, hdr.length); 00111 00112 interrupt->Schedule(NetworkSendDone, (int)this, NetworkTime, NetworkSendInt); 00113 00114 if (Random() % 100 >= chanceToWork * 100) { // emulate a lost packet 00115 DEBUG('n', "oops, lost it!\n"); 00116 return; 00117 } 00118 00119 // concatenate hdr and data into a single buffer, and send it out 00120 char *buffer = new char[MaxWireSize]; 00121 *(PacketHeader *)buffer = hdr; 00122 bcopy(data, buffer + sizeof(PacketHeader), hdr.length); 00123 SendToSocket(sock, buffer, MaxWireSize, toName); 00124 delete []buffer; 00125 }

void Network::SendDone  ) 
 

Definition at line 89 of file network.cc.

References FALSE, Statistics::numPacketsSent, and stats.

00090 { 00091 sendBusy = FALSE; 00092 stats->numPacketsSent++; 00093 (*writeHandler)(handlerArg); 00094 }


The documentation for this class was generated from the following files:
Generated on Thu Sep 16 12:33:48 2004 for NachOS by doxygen 1.3.8