Java RMI: Project 5 - Building a Peer-to-Peer Messaging System, Study Guides, Projects, Research of Programming Languages

Instructions for project 5 in the cmsc 433 - programming language technologies and paradigms course, focusing on java remote method invocation (rmi). Students are required to create a peer-to-peer messaging system that can connect to a well-known host, send and receive messages, and handle failed clients. The document also covers the basics of an rmi application, including interfaces, implementation, and registration.

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 07/30/2009

koofers-user-y3l
koofers-user-y3l 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2003
More on Java RMI
April 22, 2003
Project 5
Your code needs to be able to
Connect to well known host and ask to peer
Send PeerWithMe messages to get more peers
Accept peerWithMe calls (respect maxPeers)
Send messages
Discard duplicates
Forward text messages (not back to source)
Respond to PeerWithMe messages
Handle failed client
What You Don’t Need to Do
Don’t worry about handling slow clients
You’ll need to do this for project 6
A Typical RMI Application
Client and Server run on different machines
Remote Object(s) registered by Server
Remote Object(s) looked up by Client
Code transferred point of use
Using code base, usually via HTTP
RMI carries out operations on Remote
Objects
Example
Compute Server Application
Example from Java RMI tutorial
http://java.sun.com/docs/books/tutorial/rmi/
Goal
Execute object methods on a remote machine
Often because local resources aren’t sufficient
pf3
pf4
pf5

Partial preview of the text

Download Java RMI: Project 5 - Building a Peer-to-Peer Messaging System and more Study Guides, Projects, Research Programming Languages in PDF only on Docsity!

CMSC 433 – Programming Language

Technologies and Paradigms

Spring 2003

More on Java RMI

April 22, 2003

Project 5

  • Your code needs to be able to
    • Connect to well known host and ask to peer
    • Send PeerWithMe messages to get more peers
    • Accept peerWithMe calls (respect maxPeers)
    • Send messages
      • Discard duplicates
      • Forward text messages (not back to source)
    • Respond to PeerWithMe messages
    • Handle failed client

What You Don’t Need to Do

  • Don’t worry about handling slow clients
    • You’ll need to do this for project 6

A Typical RMI Application

  • Client and Server run on different machines
  • Remote Object(s) registered by Server
  • Remote Object(s) looked up by Client
  • Code transferred point of use
    • Using code base, usually via HTTP
  • RMI carries out operations on Remote

Objects

Example Compute Server Application

  • Example from Java RMI tutorial
    • http://java.sun.com/docs/books/tutorial/rmi/
  • Goal
    • Execute object methods on a remote machine
    • Often because local resources aren’t sufficient

Compute Interface

public interface Compute extends Remote { RemoteException;^ Object^ executeTask(Task^ t)^ throws }

  • Compute extends Remote
    • Any class implementing Compute can be used remotely
      • Its remote methods can be called from any JVM
      • Its implementation resides on the JVM in which it was created
  • executeTask() is a remote method
    • It may throw RemoteException

Task Interface

public interface Task extends Serializable { Object execute(); }

  • Task implementations implement Serializable, rather than Remote - Why?
  • execute() method returns an Object
    • Client must downcast result
    • This method not required to throw RemoteException

Implementing Compute Engine

  • Our implementation of compute interface

will be called ComputeEngine

  • In general, the implementer of a remote

interface should at least

  1. Declare the remote interfaces being implemented
  2. Define the constructor for the remote object
  3. Implement each remote method in the remote interfaces

Further Requirements for Servers

  • Server creates and installs remote objects
    • Often done in method of remote object
      • but can be done anywhere
  • Setup procedure should
    1. Create and install a security manager
      • if you’ll be downloading code
    2. Create one or more instances of remote object
    3. Register remote object(s) with the RMI registry

Declare the Remote Interfaces

  • ComputeEngine class declared as
    • public class ComputeEngine extends UnicastRemoteObject implements Compute
  • Declaration states that
    • class implements Compute remote interface
    • extends the class java.rmi.server.UnicastRemoteObject - Magic

Define the Constructor

public ComputeEngine() throws RemoteException { super(); // (not necessary) }

  • Must throw RemoteException
    • Because the UnicastRemoteObject constructor does
    • RemoteException can occur during construction if attempt to export object fails - For example, if communication resources are unavailable or the appropriate stub class is not found

RMI Registry

  • Simple remote object name service
    • Allows remote clients to get reference to remote object by name
  • Can start the registry
    • From the command line as separate process, or
    • From within your server program

Add Remote Object to Registry

  • java.rmi.Naming provides API to registry
    • Binding (or reigstering), unbinding, look-ups
  • ComputeEngine adds remote object to registry String name = "//host/Compute"; Naming.rebind(name, engine);

More on Registering Objects

  • Naming.rebind(String, Object)
    • First parameter is URL-formatted java.lang.String representing location and name of remote object
    • Default port number is 1099
  • For security reasons, can bind, unbind, or

rebind only with registry on local host

  • Once server has registered the remote

object, setup procedure exits

Creating a Client Program

  • Two separate classes make up the client in

our example

  • ComputePi
  • Pi
  • ComputePi obtains reference to Compute

object, creates Task object, and then request

that the task be executed

  • Pi implements the Task interface,

calculating Pi to some degree of precision

ComputePi

if (System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); } String name = “//” + args[0] + “/Compute”; Compute comp = (Compute) Naming.lookup(name); Pi task = new Pi(Integer.parseInt(args[1])); BigDecimal pi = (BigDecimal) comp.executeTask(task);

ComputePi

  • Begins by installing a security manager
  • Uses Naming.lookup() to look up remote

object by name in the remote host's registry

  • Creates new Pi object
  • Invokes executeTask() on Compute remote

object, which

  • returns a java.math.BigDecimal, so the

program downcast casts result and stores it

  • Program prints out the result

Pi

  • Calculates Pi

Compiling

  • Application has 4 directory trees
  • Server
    • Application directory
      • server code written and compiled here
    • Web accessible location
      • client downloads server code from here
  • Client
    • Application directory
      • client code written and compiled here
    • Web accessible location
      • server downloads client code from here

Tips for Building RMI Apps

  • If code for your objects will be downloaded, keep client and server code in separate directory trees - Maybe even separate machines - Otherwise hard to test, because of classpath
  • Use file: URLs for local testing without web
  • Simple HTTP server for RMI classes available
    • ftp://ftp.javasoft.com/pub/jdk1.1/rmi/class- server.zip

Remote Interfaces

  • Everyone needs the remote interfaces
    • Put in developer-accessible locations
    • Put in web-accessible locations
    • Everyone shares these files
      • don’t change them
  • Might be handy to have interfaces in jar file

Building the Server

  • cd ServerDevDir
  • javac engine/ComputeEngine.java
  • rmic -v1.2 -d. engine.ComputeEngine
    • Notice takes a class as an argument
    • (Classpath omitted from above two lines)
  • mkdir ServerWebDir/engine
  • cp engine/ComputeEngine_Stub.class ServerWebDir/engine
  • Stubs now web-accessible

Building the Client

  • cd ClientDevDir
  • javac client/ComputePi.java
  • javac -d ClientWebDir client/Pi.java
    • Recall that Pi is Serializable
    • So full source code (not just stub) must be availble
  • Serializable class now web-accessible