Main Page   Compound List   File List   Compound Members   File Members  

Network Class Reference

#include <network.h>

List of all members.

Public Methods

 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 ()

Private Attributes

NetworkAddress ident
double chanceToWork
int sock
char sockName [32]
VoidFunctionPtr writeHandler
VoidFunctionPtr readHandler
int handlerArg
bool sendBusy
bool packetAvail
PacketHeader inHdr
char inbox [MaxPacketSize]


Constructor & Destructor Documentation

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

Definition at line 28 of file network.cc.

References AssignNameToSocket, chanceToWork, FALSE, handlerArg, ident, inHdr, PacketHeader::length, NetworkAddress, NetworkReadPoll, NetworkRecvInt, NetworkTime, OpenSocket, readHandler, sendBusy, sock, sockName, VoidFunctionPtr, and writeHandler.

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

Network::~Network  
 

Definition at line 52 of file network.cc.

References CloseSocket, DeAssignNameToSocket, sock, and sockName.

00053 {
00054     CloseSocket(sock);
00055     DeAssignNameToSocket(sockName);
00056 }


Member Function Documentation

void Network::CheckPktAvail  
 

Definition at line 62 of file network.cc.

References ASSERT, bcopy, DEBUG, PacketHeader::from, ident, inbox, inHdr, PacketHeader::length, MaxPacketSize, MaxWireSize, NetworkReadPoll, NetworkRecvInt, NetworkTime, PollSocket, ReadFromSocket, sock, and PacketHeader::to.

Referenced by NetworkReadPoll.

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

PacketHeader Network::Receive char *    data
 

Definition at line 132 of file network.cc.

References bcopy, inbox, inHdr, and PacketHeader::length.

Referenced by PostOffice::PostalDelivery.

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

void Network::Send PacketHeader    hdr,
char *    data
 

Definition at line 105 of file network.cc.

References ASSERT, bcopy, chanceToWork, DEBUG, FALSE, PacketHeader::from, ident, PacketHeader::length, MaxPacketSize, MaxWireSize, NetworkSendDone, NetworkSendInt, NetworkTime, Random, sendBusy, SendToSocket, sock, and PacketHeader::to.

Referenced by PostOffice::Send.

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

void Network::SendDone  
 

Definition at line 92 of file network.cc.

References FALSE, and sendBusy.

Referenced by NetworkSendDone.

00093 {
00094     sendBusy = FALSE;
00095     stats->numPacketsSent++;
00096     (*writeHandler)(handlerArg);
00097 }


Member Data Documentation

double Network::chanceToWork [private]
 

Definition at line 85 of file network.h.

Referenced by Network, and Send.

int Network::handlerArg [private]
 

Definition at line 92 of file network.h.

Referenced by Network.

NetworkAddress Network::ident [private]
 

Definition at line 84 of file network.h.

Referenced by CheckPktAvail, Network, and Send.

char Network::inbox[MaxPacketSize] [private]
 

Definition at line 98 of file network.h.

Referenced by CheckPktAvail, and Receive.

PacketHeader Network::inHdr [private]
 

Definition at line 97 of file network.h.

Referenced by CheckPktAvail, Network, and Receive.

bool Network::packetAvail [private]
 

Definition at line 95 of file network.h.

VoidFunctionPtr Network::readHandler [private]
 

Definition at line 90 of file network.h.

Referenced by Network.

bool Network::sendBusy [private]
 

Definition at line 94 of file network.h.

Referenced by Network, Send, and SendDone.

int Network::sock [private]
 

Definition at line 86 of file network.h.

Referenced by CheckPktAvail, Network, Send, and ~Network.

char Network::sockName[32] [private]
 

Definition at line 87 of file network.h.

Referenced by Network, and ~Network.

VoidFunctionPtr Network::writeHandler [private]
 

Definition at line 88 of file network.h.

Referenced by Network.


The documentation for this class was generated from the following files:
Generated on Mon Feb 10 09:54:57 2003 for nachos by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002
The University of Southern California does not screen or control the content on this website and thus does not guarantee the accuracy, integrity, or quality of such content. All content on this website is provided by and is the sole responsibility of the person from which such content originated, and such content does not necessarily reflect the opinions of the University administration or the Board of Trustees