• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Nach os network
 

Nach os network

on

  • 192 views

 

Statistics

Views

Total Views
192
Views on SlideShare
192
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 Nach os network Presentation Transcript

    • 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.
      • 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.
      • NachOs involved in the folders are:
        • #include <fcntl.h>
        • #include <sys/types.h>
        • #include <sys/socket.h>
        • #include <netinet/in.h>
        • #include <arpa/inet.h>
        • #include <netdb.h>
    • 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)
    • 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;
    • 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); }
    • 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 ;
    • 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); }
    • 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; }
    • 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; }