Nach os network
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Nach os network

on

  • 223 views

 

Statistics

Views

Total Views
223
Views on SlideShare
223
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Nach os network Presentation Transcript

  • 1. NETWORK IN NACHOS The operating system can use a simulated network to send and receive small, fixed – length messages. Message delivery is unreliable – messages may be lost.
  • 2.
    • When data has been sent or when data is available to be received, interrupts are generated. The network simulation is implemented using UNIX, domain sockets.
    • 3. NachOs involved in the folders are:
      • #include <fcntl.h>
      • 4. #include <sys/types.h>
      • 5. #include <sys/socket.h>
      • 6. #include <netinet/in.h>
      • 7. #include <arpa/inet.h>
      • 8. #include <netdb.h>
  • 9. Network.cc
      #include &quot;copyright.h&quot; #include &quot;system.h&quot; #include <strings.h> static void NetworkReadPoll(int arg) { Network *net = (Network *)arg; net->CheckPktAvail(); } static void NetworkSendDone(int arg) { // Initialize the network emulation Network *net = (Network *)arg; net->SendDone(); } Network::Network(NetworkAddress addr, double reliability, VoidFunctionPtr readAvail, VoidFunctionPtr writeDone, int callArg)
  • 10. Network.cc
      { ident = addr; if (reliability < 0) chanceToWork = 0; else if (reliability > 1) chanceToWork = 1; else chanceToWork = reliability; // set up the stuff to emulate asynchronous interrupts writeHandler = writeDone; readHandler = readAvail; handlerArg = callArg; sendBusy = FALSE; inHdr.length = 0;
  • 11. Network.cc
      sock = OpenSocket(); sprintf(sockName, &quot;SOCKET_%d&quot;, (int)addr); AssignNameToSocket(sockName, sock); // Bind socket to a filename in the current directory. interrupt->Schedule(NetworkReadPoll, (int)this, NetworkTime, NetworkRecvInt); } Network::~Network() { CloseSocket(sock); DeAssignNameToSocket(sockName); }
  • 12. Network.cc
      void Network::CheckPktAvail() { interrupt->Schedule(NetworkReadPoll, (int)this, NetworkTime, NetworkRecvInt); if (inHdr.length != 0) // do nothing if packet is already buffered return; if (!PollSocket(sock)) // do nothing if no packet to be read return; char *buffer = new char[MaxWireSize]; ReadFromSocket(sock, buffer, MaxWireSize); inHdr = *(PacketHeader *)buffer; ASSERT((inHdr.to == ident) && (inHdr.length <= MaxPacketSize)); bcopy(buffer + sizeof(PacketHeader), inbox, inHdr.length); delete []buffer ;
  • 13. Network.cc
      DEBUG('n', &quot;Network received packet from %d, length %d...n&quot;, (int) inHdr.from, inHdr.length); stats->numPacketsRecvd++; (*readHandler)(handlerArg); } void Network::SendDone() { sendBusy = FALSE; stats->numPacketsSent++; (*writeHandler)(handlerArg); }
  • 14. Network.cc
      void Network::Send(PacketHeader hdr, char* data) { char toName[32]; sprintf(toName, &quot;SOCKET_%d&quot;, (int)hdr.to); ASSERT((sendBusy == FALSE) && (hdr.length > 0) && (hdr.length <= MaxPacketSize) && (hdr.from == ident)); DEBUG('n', &quot;Sending to addr %d, %d bytes... &quot;, hdr.to, hdr.length); interrupt->Schedule(NetworkSendDone, (int)this, NetworkTime, NetworkSendInt); if (Random() % 100 >= chanceToWork * 100) { // emulate a lost packet DEBUG('n', &quot;oops, lost it!n&quot;); return; }
  • 15. Network.cc
      char *buffer = new char[MaxWireSize]; *(PacketHeader *)buffer = hdr; bcopy(data, buffer + sizeof(PacketHeader), hdr.length); SendToSocket(sock, buffer, MaxWireSize, toName); delete []buffer; } PacketHeader Network::Receive(char* data) { PacketHeader hdr = inHdr; inHdr.length = 0; if (hdr.length != 0) bcopy(inbox, data, hdr.length); return hdr; }