Docstoc

Networking - Distributed Objects

Document Sample
Networking - Distributed Objects Powered By Docstoc
					Networking - Distributed Objects




1
                   Objectives
• Highest level abstraction view of
  networking:
    – Objects independently of their location (host &
      process) can exchange messages.




2
              Basic Concepts
•   Distributed objects
•   Distributed object
•   Proxy
•   Serialization/Deserialization
•   Object reference



3
        First RMI Example
• Objective: a LinePrinter server object.
public interface LinePrinter {
    public void type(String line)
  }




4
          RMI Server & Client
• The server creates remote objects, makes
  references to them accessible, & waits for clients
  to invoke methods on these objects.
• A typical client application gets a remote reference
  to remote objects in the server & then invokes
  methods on them.
• RMI provides the mechanism by which the server
  & the client communicate & pass information
  back & forth.
5
    Distributed Object Applications
• Locate remote objects:
     – remote objects are registered with rmiregistry, or
       passed & returned as parameters.
• Communicate with remote objects:
     – to the programmer, remote communication looks like a
       standard Java method invocation.
• Load class bytecodes for passed objects:
     – RMI provides the necessary mechanisms for loading an
       object's code, as well as for transmitting its data.

6
• The server calls the registry to associate (or bind) a
  name with a remote object.
• The client looks up the remote object by its name in the
  server's registry & then invokes a method on it.
• RMI uses an existing web server to load class
  bytecodes, from server to client & from client to server,
  for objects when needed.




   7
    Adv of Dynamic Code Loading
• The types & the behavior of an object, previously
  available only in a single machine, can be
  transmitted to another remote machine.
• RMI passes objects by their true type, so the
  behavior of those objects is not changed when
  they are sent to another machine.
• This allows new types to be introduced into a
  remote virtual machine, thus extending the
  behavior of an application dynamically.
8
    Interfaces, Objects, & Methods
• An object becomes remote by implementing
  a remote interface:
    – A remote interface extends the interface
      java.rmi.Remote.

    – Each method of the interface declares
      java.rmi.RemoteException in its throws
      clause, in addition to any application-specific
      exceptions.

9
                   The Stub
• RMI treats a remote object differently from a non-
  remote object when the object is passed from one
  virtual machine to another.
• Rather than making a copy of the implementation
  object in the receiving virtual machine, RMI
  passes a remote stub for a remote object.
• The stub acts as the local representative for the
  object & is the remote reference.

10
             The Stub (Cont.)
• The caller invokes a method on the local stub,
   which is responsible for carrying out the method
   call on the remote object.
• A stub for a remote object implements the same
   set of remote interfaces that the remote object
   implements.
• This allows a stub to be cast to any of the
   interfaces that the remote object implements.
• However, this also means that only those methods
   defined in a remote interface are available to be
11 called in the receiving virtual machine.
Creating Distributed Applications
1. Design & implement the components of
   your distributed application.
2. Compile sources & generate stubs.
3. Make classes network accessible.
4. Start the application.



12
             Design & Implement
• Decide which components are local & which ones
  remotely accessible.
• Defining the remote interfaces:
     – A remote interface specifies the methods that can be
       invoked remotely by a client.
     – Clients program to remote interfaces, not to the
       implementation classes of those interfaces.
     – Part of the design of such interfaces is the
       determination of any local objects that will be used as
       parameters & return values for these methods; if any of
       these interfaces or classes do not yet exist, you need to
13
       define them as well.
           RMI Requirements
public interface LinePrinter
    extends java.rmi.Remote {
    public void type(String line)
      throws java.rmi.RemoteException;
  }
• RMI uses the extends java.rmi.Remote as an
  indication that an object is distributed.
• All operations on remote objects can potentially
  fail on the network.
• The exception RemoteException indicates this.
14
     How Parameters are Passed
• If a parameter is an instance of a distributed
  object, an object reference is passed.
• All operations to that object are invoked
  remotely.
• If a parameter is not a distributed object, it
  is serialized using Java's standard Object
  Serialization. (It is passed by value.)

15
 How Parameters are Pass (Cont.)
try {
  printer.type("Hello!");
}
catch (java.rmi.RemoteException e){
     System.err.println("Cannot connect.");
     e.printStackTrace();
}


16
      Objects Implementation
public class LinePrinterImpl extends
  java.rmi.server.UnicastRemoteObject
  implements LinePrinter {
  public LinePrinterImpl()
    throws java.rmi.RemoteException{}
  public void type(String s)
    throws java.rmi.RemoteException
  {System.out.println(s);}
}

17
     Distributed Object Compilation
• Compile implementation as usual (javac
  LinePrinterImpl).
• Generate stub & skeleton automatically:
  (rmic LinePrinterImpl)




18
            Writing a Server
import java.rmi.*;
public class PrintServer{
 public static void main(String[] args){
  System.setSecurityManager(new
 RMISecurityManager());
  try {
   LinePrinter printer=new LinePrinterImpl();
   Naming.rebind( "LinePrinter",printer);
   System.out.println("PrintServer is ready.");}
  catch (Exception e){
   System.out.println("Error: " + e);
   e.printStackTrace();}}}

19
import java.io.*;
import java.rmi.*;   Writing a Client
public class PrintClient {
 public static void main(String[] args) {
  try {
    LinePrinter printer =
  (LinePrinter)Naming.lookup("//localhost/LinePrinter");
    BufferedReader r = new
    BufferedReader(new InputStreamReader(System.in));
    String nextLine = r.readLine();
    while (nextLine != null) {
     printer.type( nextLine );
     nextLine = r.readLine();}
    } catch (Exception e) {
     System.err.println("Error: " + e);
     e.printStackTrace();
  20
    }}}
     Naming Service & rmiregistry
• Must run rmiregistry for client to locate
  server.
• Client uses URLs of the form:
     – rmi://hostname:port/name
• The naming object connects to
  rmiregistry to resolve these names.


21
             Running the Example
• The files needed are:
     – LinePrinter.java   & LinePrinter.class
     –   LinePrinterImpl.java & LinePrinterImpl.class

     –   PrintClient.java & PrintClient.class

     –   PrintServer.java & PrintServer.class




22
     Running the Example (Cont.)
• Open a window on lead
• Make sure you define in your .cshrc (or
  .tcshrc):
     setenv JAVA_HOME /usr/local/java
     alias java /usr/local/java/bin/java
     alias javac /usr/local/java/bin/javac
     alias rmiregistry /usr/local/java/bin/rmiregistry




23
     Running the Example (Cont.)
• Start the registry:
  % rmiregistry &
• Start the server:
  % java PrintServer &
• Open another window on lead
• In the second window, run the PrintClient:
  % java PrintClient
• Everything you type in the client window will be
  echoed on the server window.
24
 Distributed & Non-distributed Programs

• Object definition:
     – A local object is defined by a java class.
     – A remote object's behavior is defined by an
       interface that must extend the remote interface.
• Object implementation:
     – A local object is implemented by its java class.
     – A remote object's behavior is executed by a
       java class that implements the remote interface.
25
        Object Creation & Access
• Object creation:
     – An instance of a local object is created by new.
     – An instance of a remote object is created on the host
       computer with the new operator.
     – A client cannot directly create a new remote object.
• Object access:
     – A local object is accessed directly via an object
       reference variable.
     – A remote object is accessed via an object reference
       variable which points to a proxy stub implementation of
       the remote interface.
26
               References
• In a single JVM, an object reference points
  directly at an object in the heap.
• A "remote reference" is a pointer to a proxy
  object (a "stub") in the local heap.
• That stub contains information that allows it
  to connect to a remote object, which
  contains the implementation of the methods.

27
            Active References
• In a single JVM, an object is considered "alive" if
   there is at least one reference to it.
• In a distributed environment, remote JVMs may
   crash, & network connections may be lost.
• A remote object is considered to have an active
   remote reference to it if it has been accessed
   within a certain time period (the lease period).
• If all remote references have been explicitly
   dropped, or if all remote references have expired
   leases, then a remote object is available for
28 distributed garbage collection.
                 Finalization
• If an object implements the finalize()
  method, it is called before an object is reclaimed
  by the garbage collector.
• A remote object implementation should
  implement the Unreferenced interface to
  receive notification when there are no more clients
  that reference that remote object.
• The unreferenced method of that interface is
  called when all remote references have been
  dropped.
29
           Garbage Collection
• When all local references to an object have been
  dropped, an object becomes a candidate for
  garbage collection.
• The distributed garbage collector works with the
  local garbage collector.
• If there are no remote references & all local
  references to a remote object have been dropped,
  then it becomes a candidate for garbage collection
  through the normal means.
30
                 Exceptions
• Exceptions are either runtime exceptions or
  exceptions.
• The java compiler forces a program to handle all
  exceptions.
• RMI forces programs to deal with any possible
  RemoteException objects that may be thrown.
• This was done to ensure the robustness of
  distributed applications.

31
• An interface does not contain executable code.
• The first class is the implementation of the
  behavior, & it runs on the server.
• The second class acts as a proxy for the remote
  service & it runs on the client.

32
• A client program makes method calls on the
  proxy object, RMI sends the request to the
  remote JVM, & forwards it to the
  implementation.
• Any return values provided by the
  implementation are sent back to the proxy &
  then to the client's program.
 33
              Architecture Layers
• The stub & skeleton layer:
   – Lies just beneath the view of the developer.
   – Intercepts method calls to the interface reference variable &
     redirects these calls to a remote RMI service.
• The remote reference layer:
   – How to interpret references by clients to remote objects.
   – This layer connects clients to remote service objects that are
     running & exported on a server.
• The transport layer: TCP/IP connections.

  34
• By using a layered architecture each of the
  layers could be enhanced or replaced
  without affecting the rest of the system.
• For example, the transport layer could be
  replaced by a UDP/IP layer without
  affecting the upper layers.
35
        A proxy designed pattern
• An object in one context is represented by another
  (the proxy) in a separate context.
• The proxy knows how to forward method calls
  between the participating objects.




  36
           Stub & Skeleton Layer
• The stub class is the proxy, & the remote service
  implementation class is the RealSubject.
• The skeleton understands how to communicate
  with the stub across the RMI link.
• The skeleton carries on a conversation w. stub;
     – It reads parameters for the method call from the link,
     – Calls the remote service implementation object,
     – Accepts the return value, & then writes the return value
       back to the stub.
37
      Remote Reference Layer
• Defines & supports the invocation semantics of
  the RMI connection.
• Provides a RemoteRef object that represents the
  link to the remote service implementation object.
• The stub objects use the invoke() method in
  RemoteRef to forward the method call.
• The RemoteRef object understands the
  invocation semantics for remote services.

38
           Transport Layer
• Makes a stream-based network connections
  that use TCP/IP between JVMs.




39
       Naming Remote Objects
• Clients find remote services by using a naming or
  directory service.
• A naming or directory service is run on a well-
  known host & port number.
• RMI itself includes a simple service called the
  RMI registry, rmiregistry.
• The RMI registry runs on each machine that hosts
  remote service objects & accepts queries for
  services, by default on port 1099.

40
        Registry (Server Side)
• On a host machine, a server program creates
  a remote service by first creating a local
  object that implements that service.
• It exports that object to RMI.
• RMI creates a listening service that waits
  for clients to connect & request the service.
• The server registers the object in the RMI
  registry under a public name.
41
         Registry (Client Side)
• RMI is accessed through the static class naming.
• It provides the method lookup() that a client
  uses to query a registry.
• The method lookup() accepts a URL
rmi://host_name:name_service_port/service_name

• The method returns a remote reference to the
  service object.


42
          RMI Working Parts
• Interface definitions for the remote services.
• Implementations of the remote services.
• Stub & skeleton files.
• A server to host the remote services.
• An RMI naming service that allows clients to find
  the remote services.
• A class file provider (an HTTP or FTP server).
• A client program that needs the remote services.
43
            To build a system
• Write & compile java code for interfaces.
• Write & compile java code for implementation
  classes.
• Generate stub & skeleton class files from the
  implementation classes.
• Write java code for a remote service host program.
• Develop java code for RMI client program.
• Install & run RMI system.
44
                  Interfaces
public interface Calculator extends java.rmi.Remote {
public long add(long a,long b) throws
    java.rmi.RemoteException;
public long sub(long a,long b) throws
    java.rmi.RemoteException;
public long mul(long a,long b) throws
    java.rmi.RemoteException;
public long div(long a,long b) throws
    java.rmi.RemoteException; }
•    Compile:
>javac calculator Java
45
                     Implementation
public class CalculatorImpl
 extends java.rmi.server.UnicastRemoteObject
 implements Calculator{Implementations must have an explicit

    constructor in order to declare the RemoteException exception
 public CalculatorImpl() throws
    java.rmi.RemoteException {super();}
 public long add(long a, long b) throws
    java.rmi.RemoteException {return a+b;}
 public long sub(long a, long b) throws
    java.rmi.RemoteException {return a-b;}
 public long mul(long a,long b) throws
    java.rmi.RemoteException {return a*b;}
 public long div(long a,long b) throws
    java.rmi.RemoteException {return a/b;}}
   46
          Stubs & skeletons
• We use the RMI compiler, rmic, to
  generate the stub & skeleton files.
• The compiler runs on the remote service
  implementation class file.
>rmic CalculatorImpl
• After you run rmic you should find the file
  calculator_stub.Class and
  calculator_Skel.Class.
47
                  Host server
import java.rmi.Naming;
public class CalculatorServer {
 public CalculatorServer() {
  try {
 Calculator c = new CalculatorImpl();
 Naming.rebind("rmi://localhost:1099/Calculator",c);
 } catch (Exception e) {
   System.out.println("Trouble: " + e);
 }}
 public static void main(String args[]) {
     new CalculatorServer();
 }}

48
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
public class CalculatorClient {
                                              Client
    public static void main(String[] args) {
         try {Calculator c=
(Calculator)Naming.lookup("rmi://localhost/CalculatorService");
             System.out.println( c.sub(4, 3));
             System.out.println( c.add(4, 5));
             System.out.println( c.mul(3, 6));
             System.out.println( c.div(9, 3));}
         catch (MalformedURLException murle) {
             System.out.println("MalformedURLException");
             System.out.println(murle);}
         catch (RemoteException re) {
             System.out.println("RemoteException");
             System.out.println(re);}
         catch (NotBoundException nbe) {
             System.out.println("NotBoundException");
             System.out.println(nbe);}
         catch (java.lang.ArithmeticException ae) {
             System.out.println("java.lang.ArithmeticException");
      49
             System.out.println(ae);}}}
       Parameters in a single JVM
• The normal semantics is pass-by-value.
• When a primitive data type is passed as a
  parameter to a method, the mechanics of pass-by-
  value are straightforward.
• An object resides in heap memory & is accessed
  through one or more reference variables.
• Passing an object as a parameter:
     – in the mechanics it is the reference variable that is
       passed to the method.

50
      RMI Primitive parameters
• A primitive data type is passed by value.
• RMI makes a copy of a primitive data type & send
  it to the remote method.
• If a method returns a primitive data type, it is also
  returned to the calling JVM by value.
• Values are passed between JVMs in a standard,
  machine-independent format.
• This allows JVMs running on different platforms
  to communicate with each other reliably.

51
           Object parameters
• RMI sends the object by value.
• A return value is returned by value.
• An object can refer to other objects in complex
  graph-like structure.
• Because different JVMs do not share heap
  memory, RMI must send the referenced object &
  all objects it references.
• Object serialization transforms an object into a
  linear format that can then be sent over the
  network wire.
52
       Remote object parameters
BankManager bm;
Account a;
try{bm=(BankManager)
 Naming.lookup("rmi://Bank/BankManager");
 a=bm.getAccount(“SPL031");
 // Code that uses the account
} catch (RemoteException re) {}

• The getAccount() returns a reference to a remote service
public Account getAccount(String accountName) {
    //Code to find the matching account
    AccountImpl ai =//return reference from search
    return ai;}


53
• When a method returns a reference to a remote object,
  RMI substitutes another object (the remote proxy) in
  the return stream.
      – Return a remote reference from server to client A.
      – Send the remote reference from client A to client B.
      – Send the remote reference from client B back to server.




 54
• When the AccountImpl object is returned to client
  A, the account proxy object is substituted.
• Subsequent method calls continue to send the
  reference first to client B & then back to server.
• During this process, the reference continues to refer
  to one instance of the remote service.
• When the reference is returned to server, it is not
  converted into a local reference to the
  implementation object.
• While this would result in a speed improvement,
  maintaining this indirection ensures that the
  semantics of using a remote reference is maintained.
55
Automatic distribution of classes
• The RMI extends class loading to include the
   loading of classes from FTP / HTTP servers.
• Classes can be deployed in one, or only a few
   places, & all nodes in a RMI system will be able
   to get the proper class files to operate.
• RMI supports this remote class loading through
   the RMIClassLoader.
• If a client or server is running an RMI system & it
   sees that it must load a class from a remote
   location, it calls on the RMIClassLoader to do
56 this work.
     Networking

     Sockets in Java




57
                  Where we are?
• Networking allows processes running on different
  hosts to exchange messages.
• At the highest abstraction level, distributed objects
  are location transparent:
     – objects send messages to other messages regardless of
       their location inside hosts & processes.
     – The RTE manages transparently the transfer of these
       messages.
     – This was detailled in the previous lecture.

58
       Where we are? (Cont.)
• Distributed object technologies (RMI &
  CORBA) are built on top of sockets or
  similar technology.
• Underst&ing the low-level helps
  underst&ing the higher level.
• Java's implementation of the socket model
  is free from many of the annoying details
  that are still visible in C's interface.

59
               In This Lecture
• We present one level down of the
  abstraction:
     – session-oriented
     – reliable connections implemented by the TCP
       protocol
     – used through the socket interface.



60
                       Terminology
• Process: program running within a host, accesses a
  single virtual memory space.
• Within same host, two processes communicate
  using inter-process communication (defined by
  OS).
• A stream is a sequence of characters that flow into
  or out of a process.
      – An input stream is attached to some input source for the
        process, eg, keyboard or socket.
      – An output stream is attached to an output source, eg,
        monitor or socket.

 61
            Client Server Paradigm

• Typical network app has two pieces:
• Client:
     – Initiates contact with server (“speaks first”)
     – Typically requests service from server,
• Server:
     – Provides requested service to client


62
Processes communicating across
            network
• Process sends/receives messages to/from its
  socket
• Socket analogous to door
• Sending process shoves message out door
• Sending process assumes transport
  infrastructure on other side of door which
  brings message to socket at receiving
  process
63
         Addressing processes
• For a process to receive messages, it must have an
  identifier
• Every host has a unique 32-bit IP address
• Q: does the IP address of the host on which the
  process runs suffice for identifying the process?
• Answer: no, many processes can be running on the
  same host
• Identifier includes both the IP address & port
  numbers associated with the process on the host.

64
                Socket Programming
 • OS-controlled interface into which process can
   send & receive messages to/from another process
 • two types of transport service via socket API:
       – UDP:unreliable datagram
       – TCP:reliable transfer of bytes from 1 process to another
                                                         controlled by
controlled by                                 process    application
  application   process
                                                         developer
   developer     socket                       socket
                TCP with                     TCP with    controlled by
controlled by
                                             buffers,    operating
   operating    buffers,      internet                   system
      system    variables                    variables

                host or                      host or
                server                       server
  65
      Socket Programming with TCP              keyboard             monitor

Example client-server:
1) client reads line from standard




                                                      inFromUser
                                              input

   input (inFromUser stream),                stream

                                   Client
   sends to server via socket      Process
                                   process
   (outToServer stream)
2) server reads line from socket
3) server sends back to client




                                                                       inFromServer
                                                      outToServer
                                         output                                        input
                                         stream                                       stream
4) client reads, prints modified
   line from socket                              client TCP
                                                   clientSocket
   (inFromServer stream)                           socket                                 TCP
                                                                                         socket

                                               to network           from network

 66
     Client Server Socket Interaction: TCP
Server (running on hostid)                     Client
      create socket,
      port=x, for
      incoming request:
      welcomeSocket =
         ServerSocket()

                              TCP             create socket,
      wait for incoming
      connection request connection   setup   connect to hostid, port=x
      connectionSocket =                      clientSocket =
      welcomeSocket.accept()                         Socket()

                                                send request using
      read request from                         clientSocket
      connectionSocket

      write reply to
      connectionSocket                          read reply from
                                                clientSocket
      close
      connectionSocket                           close
                                                 clientSocket
67
              Example: Java Client (TCP)
                     import java.io.*;
                     import java.net.*;
                     class TCPClient {

                       public static void main(String argv[]) throws Exception
                       {
                         String sentence;
                         String modifiedSentence;
            Create
      input stream        BufferedReader inFromUser =
                           new BufferedReader(new InputStreamReader(System.in));
           Create
    client socket,        Socket clientSocket = new Socket("hostname", 6789);
 connect to server
            Create        DataOutputStream outToServer =
     output stream         new DataOutputStream(clientSocket.getOutputStream());
attached to socket
      68
        Example: Java client (TCP), cont.
            Create             BufferedReader inFromServer =
      input stream              new BufferedReader(new
attached to socket              InputStreamReader(clientSocket.getInputStream()));

                               sentence = inFromUser.readLine();
          Send line
          to server            outToServer.writeBytes(sentence + '\n');

           Read line           modifiedSentence = inFromServer.readLine();
        from server
                               System.out.println("FROM SERVER: " + modifiedSentence);

                               clientSocket.close();

                           }
                       }
   69
            Example: Java Server (TCP)
                      import java.io.*;
                      import java.net.*;

                      class TCPServer {

                       public static void main(String argv[]) throws Exception
                        {
                          String clientSentence;
           Create
 welcoming socket
                          ServerSocket welcomeSocket = new ServerSocket(6789);
     at port 6789
                          while(true) {
Wait, on welcoming
socket for contact            Socket connectionSocket = welcomeSocket.accept();
          by client
                             BufferedReader inFromClient =
     Create input             new BufferedReader(new
stream, attached              InputStreamReader(connectionSocket.getInputStream()));
        to socket
    70
         Example: Java Server (TCP), Cont
   Create output
stream, attached           DataOutputStream outToClient =
       to socket            new DataOutputStream(connectionSocket.getOutputStream());
    Read in line
    from socket            clientSentence = inFromClient.readLine();



  Write out line
                           outToClient.writeBytes(clientSentence + '\n');
      to socket
                       }
                   }
              }                  End of while loop,
                                 loop back & wait for
                                 another client connection


    71
     Socket Programming with UDP
UDP: no “connection”
  between client & server
• No handshaking               application viewpoint
• Sender explicitly attaches   UDP provides unreliable transfer
  IP address & port of         of groups of bytes (“datagrams”)
  destination to each packet        between client & server
• Server must extract IP
  address, port of sender
  from received packet
UDP: transmitted data may
  be received out of order,
  or lost
72
     Client Server Socket Interaction: UDP
Server (running on hostid)   Client

      create socket,         create socket,
      port=x, for            clientSocket =
      incoming request:      DatagramSocket()
      serverSocket =
      DatagramSocket()
                             Create, address (hostid, port=x,
                             send datagram request
                             using clientSocket
       read request from
       serverSocket

       write reply to
       serverSocket
       specifying client       read reply from
       host address,           clientSocket
       port number             close
                               clientSocket


73
     Example: Java Client (UDP)keyboard           monitor




                                     inFromUser
                             input
                            stream


                 Client
                 Process
                                                                                 Input: receives
                 process
                                                                                 packet (TCP
     Output: sends                                                               received “byte
     packet (TCP sent                                                            stream”)




                                                     receivePacket
                                     sendPacket
     “byte stream”)         UDP                                       UDP
                           packet                                    packet



                                    client UDP
                                      clientSocket
                                      socket                             UDP
                                                                        socket

                               to network         from network




74
              Example: Java Client (UDP)
                      import java.io.*;
                      import java.net.*;

                      class UDPClient {
                         public static void main(String args[]) throws Exception
                         {
            Create
      input stream        BufferedReader inFromUser =
                           new BufferedReader(new InputStreamReader(System.in));
            Create
      client socket       DatagramSocket clientSocket = new DatagramSocket();
         Translate
                          InetAddress IPAddress = InetAddress.getByName("hostname");
   hostname to IP
address using DNS         byte[] sendData = new byte[1024];
                          byte[] receiveData = new byte[1024];

                          String sentence = inFromUser.readLine();
                          sendData = sentence.getBytes();
      75
           Example: Java Client (UDP), Cont.
    Create datagram
  with data-to-send,     DatagramPacket sendPacket =
length, IP addr, port     new DatagramPacket(sendData, sendData.length, IPAddress, 9876);

    Send datagram        clientSocket.send(sendPacket);
         to server
                         DatagramPacket receivePacket =
                          new DatagramPacket(receiveData, receiveData.length);
    Read datagram
                         clientSocket.receive(receivePacket);
      from server
                         String modifiedSentence =
                            new String(receivePacket.getData());

                         System.out.println("FROM SERVER:" + modifiedSentence);
                         clientSocket.close();
                         }
                     }

      76
           Example: Java Server (UDP)
                     import java.io.*;
                     import java.net.*;

                     class UDPServer {
                      public static void main(String args[]) throws Exception
          Create        {
 datagram socket
                         DatagramSocket serverSocket = new DatagramSocket(9876);
    at port 9876
                         byte[] receiveData = new byte[1024];
                         byte[] sendData = new byte[1024];

                         while(true)
                          {
  Create space for
                           DatagramPacket receivePacket =
received datagram
                            new DatagramPacket(receiveData, receiveData.length);
           Receive          serverSocket.receive(receivePacket);
         datagram
   77
          Example: Java Server (UDP), Cont
                            String sentence = new String(receivePacket.getData());
      Get IP addr
                            InetAddress IPAddress = receivePacket.getAddress();
       port #, of
           sender           int port = receivePacket.getPort();



                            sendData = sentence.getBytes();
Create datagram
                            DatagramPacket sendPacket =
to send to client            new DatagramPacket(sendData, sendData.length, IPAddress,
                                       port);
      Write out
       datagram             serverSocket.send(sendPacket);
       to socket        }
                    }
                }                    End of while loop,
                                     loop back & wait for
     78                              another datagram
        Internet Transport Protocols
TCP service:                          UDP service:
• connection-oriented: setup          • Unreliable data transfer
  required between client & server      between sending &
  processes                             receiving process
• reliable transport between sending • Does not provide:
  & receiving process                   connection setup,
• flow control: sender won’t            reliability, flow control,
  overwhelm receiver                    congestion control, timing,
• congestion control: throttle sender   or bandwidth guarantee
  when network overloaded             • Q: why bother? Why is
• does not provide: timing,             there a UDP?
  minimum bandwidth guarantees
   79
     DatagramSocket Classes
• UDP connections are obtained in Java using
  the DatagramSocket object.
• A datagram is an independent, self-
  contained message sent over the network
  whose arrival, arrival time, and content are
  not guaranteed.


80
     Multicasting Connections
• Datagram-oriented.
• Unreliable: messages sent through the
  connection can be received out of order on
  the other end.
• Point-to-group: messages sent by one end
  are received by a whole group of connected
  clients.

81
     MulticastSocket Classes
• In addition to DatagramSocket, which lets
  programs send packets to one another, java.net
  includes a class called MulticastSocket.
• This kind of socket is used on the client-side to
  listen for packets that the server broadcasts to
  multiple clients.
• On the server side, a regular DatagramSocket
  can be used.
• An important point is to use a multicasting IP
  address to refer to the group.
82
                      Server
...
While (true) try {
 byte[] buf new byte[256];
 String dString = null;
 dString = new Date().toString();
 InetAddress group=InetAddress.getByName("230.0.0.1");
 DatagramPacket packet;
 packet=new DatagramPacket(buf,buf.length,group,4446);
            socket.send(packet);
 try {sleep((long)Math.random()*FIVE_SECONDS);}
 catch (InterruptedException e) { }
} catch (IOException e) {
 e.printStackTrace();
}}
...

83
                 Client
• The client program creates its
  MulticastSocket with the server port
  number.
• To become a member of the IP-multicast
  group, the client calls the joinGroup
  method with the InetAddress that
  identifies the group.

84
             Client Side Code
MulticastSocket socket = new MulticastSocket(4446);
InetAddress group = InetAddress.getByName("230.0.0.1");
socket.joinGroup(group);
DatagramPacket packet;
for (int i = 0; i < 5; i++) {
    byte[] buf = new byte[256];
    packet = new DatagramPacket(buf, buf.length);
    socket.receive(packet);
    String received = new String(packet.getData());
    System.out.println("Quote of the Moment: " +
   received);}
socket.leaveGroup(group);
socket.close();


85
        Server Architectures
• Iterative Server: Single-process serialized
• Forking a new process for each request
• Keeping a pool of separate server processes
  that continuously accept requests and
  process them



86
     Server Architectures (Cont.)
• Spawning a new thread to handle each
  request
• Keeping a pool of separate threads around
  to handle the load of requests
• Using an asynchronous I/O server
  architecture that is capable of managing
  multiple parallel connections from within a
  single process/ thread

87
      Thread Creation Strategies
• Thread-per-connection.
public class EchoServer{
 ServerSocket sock = new ServerSocket(port);
 while (true) {
  Thread worker=new Thread(new Connection(sock.accept()));
  worker.start();}}
public class Connection implements Runnable{
 private static Handler handler = new Handler();
 public Connection(Socket client){this.client=client;}
 public void run() {handler.process(client);}}
public class Handler{
 public void process(Socket client){echo the messages back}}


88
Thread Creation Strategies (Cont.)
Thread pool:
     – Create several threads initially.
     – When work arrives, have one of these warm threads
       perform the work.
• The main benefit of thread pools is not that we
  eliminate the latency involved with starting a
  thread, but rather that we bound the number of
  threads that are concurrently active.


89
  Echo Server Using a Thread Pool
public class EchoServer {
 ThreadPool pool = new ThreadPool(3);
 pool.startPool();
 ServerSocket sock=new ServerSocket(port);
 while (true)
  pool.add(new Connection(sock.accept()));}
• Thread creation using either the thread-per-connection or
  thread pool isn't scalable for many server designs.
• With the echo server, a separate thread is used to
  maintain the state of each connection!
  90
           Event-Oriented I/O
• Processing is divided into small tasks and each
  task can be performed without blocking.
• Connecting to a socket or data arriving at a socket
  connection are examples of I/O event.
• I/O events can be served with a single thread.
• Java 1.4 introduces non-blocking I/O calls -
  similar to UNIX select() and Win32
  WaitForSingleEvent().

91
                         Buffers
ByteBuffer buffer = ByteBuffer.allocate(256);
     – A java.nio.ByteBuffer will be used to store the data.
     – Note there is no constructor, but rather an
       allocate() factory method.
• Buffers have
     – Capacity: the size of the buffer
     – Limit: the number of elements in the buffer
     – Position: where we are reading in the buffer. For reads -
       begin from position 0 and read to the limit.

92
                     Channels
• java.nio.Channel - A channel is a
  connection to an I/O resource such as a file,
  hardware device, network socket, etc.
• A selectable channel can select different I/O
  events for the channel.
ServerSocketChannel server=ServerSocketChannel.open();
ServerSocket sock=server.socket();
sock.bind(new InetSocketAddress(DEFAULT_PORT));
server.configureBlocking(false);


   93
                  Selectors
• A java.nio.Selector allows us to
  multiplex a selectable channel.
Selector accptSlctr=Selector.open();
• We register the selector with the channel,
  and accept connections.
SelectionKey acceptKey=
server.register(accptSlctr,SelectionKey.OP_ACCEPT);




94
                  The Server
• Initially we wait for accept events on the socket
  channel:
accptSlctr.select()
• When there are events in the queue for the
  channel, we iterate through them:
for (Iterator itr = keys.iterator();
  itr.hasNext();) {
    SelectionKey key = (SelectionKey)itr.next();
    itr.remove();we must remove it from the set
   // . . . follows below
}
95
                First the Selector Key
if (key.equals(acceptKey)){a client is trying to connect to the server
 if (key.isAcceptable()){accept the client connect and set to non-blocking
  SocketChannel client = server.accept();
  client.configureBlocking(false);
  SelectionKey clientKey=
   client.register(accptSlctr,SelectionKey.OP_READ);
  attach an Integer object to keep track of each client
  clientKey.attach(new Integer(clientCount++));}}




    96
Either accept connections or data from clients keys
a client must have sent some data
SocketChannel clientChannel=(SocketChannel)key.channel();
int bytesRead = clientChannel.read(buffer);now read the data
buffer.flip();first spit it out locally
String message = decoder.decode(buffer).toString();
int clientNumber = ((Integer)key.attachment()).intValue();
System.out.println(clientNumber + ":" + message);
if (message.trim().equals(".")){write a BYE message
 clientChannel.write(encoder.encode(CharBuffer.wrap("BYE")));
 key.cancel();cancel the key and close the channel
 clientChannel.close();
} else {
 buffer.rewind();now echo it back to the client
 clientChannel.write(buffer);}
buffer.clear();prepare the buffer for another read
  97
           Scalable network services
• Most distributed services have same basic
  structure:
   –   Read request
   –   Decode request
   –   Process service
   –   Encode reply
   –   Send reply
• But differ in nature and cost of each step
• Each handler may be started in its own
  thread
  98
              class Server

class Server implements Runnable {
public void run(){try{
 ServerSocket ss = new ServerSocket(PORT);
 while (!Thread.interrupted())
  new Thread(new Handler(ss.accept())).start();
  // or, single-threaded, or a thread pool
 } catch (IOException ex) { /* ... */ }
}




99
             class Handler

static class Handler implements Runnable {
 final Socket socket;
 Handler(Socket s) { socket = s; }
 public void run(){try{
  byte[] input = new byte[MAX_INPUT];
  socket.getInputStream().read(input);
  byte[] output = process(input);
  socket.getOutputStream().write(output);
 } catch (IOException ex) { /* ... */ }}
 private byte[] process(byte[] cmd) { /* ... */
  }}}

100
                 Scalability Goals
• Graceful degradation under increasing load (more
  clients)
• Continuous improvement with increasing
  resources (CPU, memory, disk, bandwidth)
• Also meet availability and performance goals
      – Short latencies
      – Meeting peak demand
      – Tunable quality of service
• Divide-and-conquer is usually the best approach
  for achieving any scalability goal
101
               Divide and conquer
• Divide processing into small tasks
      – Each task performs an action without blocking
• Execute each task when it is enabled
      – Here, an IO event usually serves as trigger
• Basic mechanisms supported in java.nio
      – Non-blocking reads and writes
      – Dispatch tasks associated with sensed IO events
• Endless variation possible
      – A family of event-driven designs read decode compute
        encode send handler
102
           Event-driven Designs
• Usually more efficient than alternatives
      – Fewer resources
         • Don't usually need a thread per client
      – Less overhead
         • Less context switching, often less locking
      – But dispatching can be slower
         • Must manually bind actions to events
• Usually harder to program
      – Must break up into simple non-blocking actions
         • Similar to GUI event-driven actions
         • Cannot eliminate all blocking: GC, page faults, etc
      – Must keep track of logical state of service
103
            Reactor Pattern
• Reactor responds to IO events by dispatching
  the appropriate handler
• Handlers perform non-blocking actions
• Manage by binding handlers to events




  104
              java.nio Support
• Channels
      – Connections to files, sockets etc that support non-
        blocking reads
• Buffers
      – Array-like objects that can be directly read or written
        by Channels
• Selectors
      – Tell which of a set of Channels have IO events
• SelectionKeys
      – Maintain IO event status and bindings
105

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:3
posted:5/7/2012
language:English
pages:105