Main Page   Compound List   File List   Compound Members   File Members  

PostOffice Class Reference

#include <post.h>

List of all members.

Public Methods

 PostOffice (NetworkAddress addr, double reliability, int nBoxes)
 ~PostOffice ()
void Send (PacketHeader pktHdr, MailHeader mailHdr, char *data)
void Receive (int box, PacketHeader *pktHdr, MailHeader *mailHdr, char *data)
void PostalDelivery ()
void PacketSent ()
void IncomingPacket ()

Private Attributes

Networknetwork
NetworkAddress netAddr
MailBoxboxes
int numBoxes
SemaphoremessageAvailable
SemaphoremessageSent
LocksendLock


Constructor & Destructor Documentation

PostOffice::PostOffice NetworkAddress    addr,
double    reliability,
int    nBoxes
 

Definition at line 178 of file post.cc.

References boxes, Thread::Fork, messageAvailable, messageSent, netAddr, network, NetworkAddress, numBoxes, PostalHelper, ReadAvail, sendLock, and WriteDone.

00179 {
00180 // First, initialize the synchronization with the interrupt handlers
00181     messageAvailable = new Semaphore("message available", 0);
00182     messageSent = new Semaphore("message sent", 0);
00183     sendLock = new Lock("message send lock");
00184 
00185 // Second, initialize the mailboxes
00186     netAddr = addr; 
00187     numBoxes = nBoxes;
00188     boxes = new MailBox[nBoxes];
00189 
00190 // Third, initialize the network; tell it which interrupt handlers to call
00191     network = new Network(addr, reliability, ReadAvail, WriteDone, (int) this);
00192 
00193 
00194 // Finally, create a thread whose sole job is to wait for incoming messages,
00195 //   and put them in the right mailbox. 
00196     Thread *t = new Thread("postal worker");
00197 
00198     t->Fork(PostalHelper, (int) this);
00199 }

PostOffice::~PostOffice  
 

Definition at line 206 of file post.cc.

References boxes, messageAvailable, messageSent, network, and sendLock.

00207 {
00208     delete network;
00209     delete [] boxes;
00210     delete messageAvailable;
00211     delete messageSent;
00212     delete sendLock;
00213 }


Member Function Documentation

void PostOffice::IncomingPacket  
 

Definition at line 328 of file post.cc.

References messageAvailable, and Semaphore::V.

Referenced by ReadAvail.

00329 { 
00330     messageAvailable->V(); 
00331 }

void PostOffice::PacketSent  
 

Definition at line 344 of file post.cc.

References messageSent, and Semaphore::V.

Referenced by WriteDone.

00345 { 
00346     messageSent->V();
00347 }

void PostOffice::PostalDelivery  
 

Definition at line 224 of file post.cc.

References ASSERT, boxes, DebugIsEnabled, MailHeader::length, MaxMailSize, MaxPacketSize, messageAvailable, network, numBoxes, Semaphore::P, PrintHeader, Network::Receive, and MailHeader::to.

Referenced by PostalHelper.

00225 {
00226     PacketHeader pktHdr;
00227     MailHeader mailHdr;
00228     char *buffer = new char[MaxPacketSize];
00229 
00230     for (;;) {
00231         // first, wait for a message
00232         messageAvailable->P();  
00233         pktHdr = network->Receive(buffer);
00234 
00235         mailHdr = *(MailHeader *)buffer;
00236         if (DebugIsEnabled('n')) {
00237             printf("Putting mail into mailbox: ");
00238             PrintHeader(pktHdr, mailHdr);
00239         }
00240 
00241         // check that arriving message is legal!
00242         ASSERT(0 <= mailHdr.to && mailHdr.to < numBoxes);
00243         ASSERT(mailHdr.length <= MaxMailSize);
00244 
00245         // put into mailbox
00246         boxes[mailHdr.to].Put(pktHdr, mailHdr, buffer + sizeof(MailHeader));
00247     }
00248 }

void PostOffice::Receive int    box,
PacketHeader   pktHdr,
MailHeader   mailHdr,
char *    data
 

Definition at line 311 of file post.cc.

References ASSERT, boxes, MailBox::Get, MailHeader::length, MaxMailSize, and numBoxes.

00313 {
00314     ASSERT((box >= 0) && (box < numBoxes));
00315 
00316     boxes[box].Get(pktHdr, mailHdr, data);
00317     ASSERT(mailHdr->length <= MaxMailSize);
00318 }

void PostOffice::Send PacketHeader    pktHdr,
MailHeader    mailHdr,
char *    data
 

Definition at line 264 of file post.cc.

References Lock::Acquire, ASSERT, bcopy, DebugIsEnabled, PacketHeader::from, PacketHeader::length, MailHeader::length, MaxMailSize, MaxPacketSize, messageSent, netAddr, network, numBoxes, Semaphore::P, PrintHeader, Lock::Release, Network::Send, sendLock, and MailHeader::to.

00265 {
00266     char* buffer = new char[MaxPacketSize];     // space to hold concatenated
00267                                                 // mailHdr + data
00268 
00269     if (DebugIsEnabled('n')) {
00270         printf("Post send: ");
00271         PrintHeader(pktHdr, mailHdr);
00272     }
00273     ASSERT(mailHdr.length <= MaxMailSize);
00274     ASSERT(0 <= mailHdr.to && mailHdr.to < numBoxes);
00275     
00276     // fill in pktHdr, for the Network layer
00277     pktHdr.from = netAddr;
00278     pktHdr.length = mailHdr.length + sizeof(MailHeader);
00279 
00280     // concatenate MailHeader and data
00281     bcopy((char *) &mailHdr, buffer, sizeof(MailHeader));
00282     bcopy(data, buffer + sizeof(MailHeader), mailHdr.length);
00283 
00284     sendLock->Acquire();                // only one message can be sent
00285                                         // to the network at any one time
00286     network->Send(pktHdr, buffer);
00287     messageSent->P();                   // wait for interrupt to tell us
00288                                         // ok to send the next message
00289     sendLock->Release();
00290 
00291     delete [] buffer;                   // we've sent the message, so
00292                                         // we can delete our buffer
00293 }


Member Data Documentation

MailBox* PostOffice::boxes [private]
 

Definition at line 134 of file post.h.

Referenced by PostalDelivery, PostOffice, Receive, and ~PostOffice.

Semaphore* PostOffice::messageAvailable [private]
 

Definition at line 136 of file post.h.

Referenced by IncomingPacket, PostalDelivery, PostOffice, and ~PostOffice.

Semaphore* PostOffice::messageSent [private]
 

Definition at line 137 of file post.h.

Referenced by PacketSent, PostOffice, Send, and ~PostOffice.

NetworkAddress PostOffice::netAddr [private]
 

Definition at line 133 of file post.h.

Referenced by PostOffice, and Send.

Network* PostOffice::network [private]
 

Definition at line 132 of file post.h.

Referenced by PostalDelivery, PostOffice, Send, and ~PostOffice.

int PostOffice::numBoxes [private]
 

Definition at line 135 of file post.h.

Referenced by PostalDelivery, PostOffice, Receive, and Send.

Lock* PostOffice::sendLock [private]
 

Definition at line 138 of file post.h.

Referenced by PostOffice, Send, and ~PostOffice.


The documentation for this class was generated from the following files:
Generated on Mon Feb 10 09:54:58 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