You are on page 1of 30

DISTRIBUTED SYSTEMS

Principles and Paradigms


Second Edition
ANDREW S. TANENBAUM
MAARTEN VAN STEEN

Chapter 4
Communication (2)

Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
Plan
•  Communication protocols
–  Lower-level and higher-level
–  Communication types
•  Communication services
–  Remote Procedure Call
–  Message-oriented communication
–  Multicasting
•  Concrete instances
–  Java sockets
–  Java Remote Method Invocation (RMI)
–  Java Message Service (JMS)

Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
Plan
•  Wireshark
•  Java technology
–  (Java) Sockets
–  Remote Procedure Call
•  Java RMI
–  Message-Oriented Communication
•  Transient: Java sockets
•  Persistent: Mickey Mouse JMS
–  Multicast
•  Will look at JGroups later

Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
Wireshark
•  Great open-source tool for understanding and
debugging protocol behavior
–  http://www.wireshark.org

•  Features:
–  Trace packets over the wire
–  Sophisticated filtering language
–  Display contents of each protocol
–  Dump contents into file
–  Display TCP conversation

•  (Formerly known as “Ethereal”)


Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
Wireshark

Protocol decoders
for these levels

Captures frames/packets
on this level

Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
A Google Client

Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
Berkeley and Java Sockets
new Socket()/new ServerSocket(…)
Socket.bind(…)
new ServerSocket(…)
ServerSocket.accept()
Socket.connect(…)
Socket.getOutputStream().write(...)
Socket.getInputStream().read(…)
Socket.close()

•  Figure 4-14. The socket •  For Java


primitives for TCP/IP. –  Socket, ServerSocket,
DatagramSocket, …

Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
A Simple Client and Server

Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
Uses for Wireshark
•  Learning about protocols
–  We just saw the flow of a TCP connection
–  Reverse engineer protocols

•  Diagnostics
–  Why does my hand-in not work…

•  Hacking 
–  Try using Wireshark with telnet or FTP
(DO NOT USE REAL PASSWORDS!!!)

Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
Java Socket Hierarchy

Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
Java RMI
•  Simple, single-language RPC system for
Java
–  Stream-based transport as default mechanism
•  Invoke objects on remote objects using
syntax for local invocations
–  However, interfaces extend java.rmi.Remote
–  And methods throw
java.rmi.RemoteException checked exception

•  So not completely transparent


Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
Example: EHR in Java RMI…
•  What we want:
:EHR god: Patient

getPatient(“Michael”)

god

getDose(“heroin”)

Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
EHR.java
package ddist;

import java.rmi.*;

public interface EHR extends Remote {


public Patient getPatient(String name) throws
RemoteException;
} serializable

Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
Patient.java
package ddist;

import java.rmi.*;

public interface Patient extends Remote {


public int getDose(String medicine)
throws RemoteException;
public void setDose(String medicine, int dose)
throws RemoteException;
}

Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
PatientServer.java
package ddist;

import java.rmi.*;
import java.rmi.server.*;
Import java.util.*;

public class PatientServer extends UnicastRemoteObject


implements Patient {
Map<String, Integer> doses;

public PatientServer() throws RemoteException {


doses = new HashMap<String, Integer>();
}

public int getDose(String medicine) {


Integer dose = doses.get(medicine);
if (dose == null) return 0;
return dose;
}

public void setDose(String medicine, int dose) {


doses.put(medicine, dose);
}
}
Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
EHRServer.java (1)
package ddist;

import java.rmi.*;
import java.rmi.server.*;

public class EHRServer extends UnicastRemoteObject implements EHR {

private Patient god, godess;

public EHRServer() throws RemoteException {


god = new PatientServer(); godess = new PatientServer();
god.setDose(”heroin", 100);
}

public Patient getPatient(String name) throws RemoteException {


if (name.equals(”Michael")) return god;
if (name.equals(”Britney")) return godess;
return null;
}

Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
EHRServer.java (2)

public static void main (String[] args) throws


Exception {
if (System.getSecurityManager() == null)
System.setSecurityManager(new SecurityManager());

String host = "localhost”;


If (args.length > 0) host = args[0];
System.out.println("Starting Server at " + host);

EHRServer server = new EHRServer();


Naming.rebind("//" + host + "/EHRServer", server);
}

Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
EHRClient.java
package ddist;

import java.rmi.*;

public class EHRClient {


public static void main(String[] args) throws Exception {
if (System.getSecurityManager() == null)
System.setSecurityManager(new SecurityManager());
String host = "localhost";
if (args.length > 0) host = args[0];
EHR ehr = (EHR) Naming.lookup("//" + host + "/EHRServer"); // Use the EHR service

String medicine = ”heroin";

String name = ”Michael";


Patient patient = ehr.getPatient(name);
S.o.println(name + " needs " + patient.getDose(medicine) + " unit(s) of ” + medicine);

name = ”Britney";
patient = ehr.getPatient(name);
S.o.println(name + " needs " + patient.getDose(medicine) + " unit(s) of ” + medicine);

medicine = ”amphetamine”;
S.o.println(name + " needs " + patient.getDose(medicine) + " unit(s) of ” + medicine);

patient.setDose(medicine, patient.getDose(medicine) + 25);


}
}

Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
RMI Basic Steps
•  Server side
–  Design and implement Remote interface
•  E.g., using UnicastRemoteObject (based on TCP/IP)
–  Define and implement security policy
–  Use Naming.rebind to bind server object to name

•  Client side
–  Use Naming.lookup to find server object
–  Invoke methods

•  Compiling and deploying


–  Make classes available over the network
Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
rmic
•  Last lecture, I said that we needed stubs
on the server and client

•  This was also true ”back in the days” with


Java (prior to 1.1 and partly 1.5)

•  Today, stubs are generated on-the-fly


using reflection

Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
RMI Parameter Passing
•  public Patient getPatient(String name)

•  All parameters are input parameters


–  A result is the single output parameter

•  Primitive types or objects that are


java.io.Serializable are passed by value

•  Objects that are java.rmi.Remote are passed ”by


reference”
–  Actually an instance of Proxy (generated by reflection)
that implements <interface>

Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
A Closer Look

Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
RMI Wire Protocol (1)
•  The wire format is represented by paired streams (seen from the client)

•  Output Stream
–  <Header> <Messages>
–  <HttpMessage>
•  Header
–  0x4a 0x52 0x4d 0x49 <Version> <Protocol>
•  Version
–  0x00 0x01
–  0x00 0x02
•  Protocol
–  <StreamProtocol>
–  <SingleOpProtocol>
–  <MultiplexProtocol>
•  StreamProtocol
–  0x4b
•  Messages
–  <Message>
–  <Messages> <Message>
•  Message
–  <Call>
–  <Ping>
–  <DgcAck>
•  Call
–  0x50 <CallData>

Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
RMI Wire Protocol (2)
•  The wire format is represented by paired streams (seen from the
client)

•  Input Stream
–  <ProtocolAck> <Returns>
–  <ProtocolNotSupported>
–  <HttpReturn>
•  ProtocolAck
–  0x4e
•  Return
–  <ReturnData>
–  <PingAck>
•  ReturnData
–  0x51 <ReturnValue>
•  PingAck
–  0x53

Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
Java Message Service
•  A specification for a message queue
•  Numerous implementations
–  SUN, IBM, …
–  We will see an example of using ActiveMQ which is
open source

•  Communication pattern
–  Loosely coupled
–  Reliable
–  Asynchronous
–  Queue or publish/subscribe
Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
Summary
•  We have introduced tools for distributed
programming in Java
–  Protocol analyzer
•  Wireshark
–  Java APIs
•  Sockets
•  RMI
•  JMS
•  You will get a chance to get further
acquainted with these during exercises
and hand-ins…
Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5

You might also like