Lab Session on Assignment One “A Simple File

Download Report

Transcript Lab Session on Assignment One “A Simple File

Briefing on
Assignment One &
RMI Programming
February 13, 2007
LAB 1
CSIS0402
1
Lab One Organization
RMI
Introduction
A Simple Example
Recap of RMI Components
Assignment One
Overview
System Architecture
Remarks
Lab Exercise on RMI
Reference
LAB 1
CSIS0402
2
Introduction to RMI
Often consists of 2 separate programs: a
server and a client
Provide the mechanism by which the
server and the client communicate and
pass information back and forth.
Make the communication like making a
method call inside a single process.
LAB 1
CSIS0402
3
A Simple RMI Example
Server creates a
remote object ,
registers the service
to RMIRegistry and
passes the object
reference to the
registry
Server
The skeleton
de-marshals
the data and
makes the
method call on
the actual
server object.
LAB 1
RMIRegistry
Client looks up the
remote object by its
name from the
server’s registry and
obtains a remote
reference (i.e. the
stub) of it.
The stub demarshals the
return value and
The skeleton gets the return returns it to the
value back from the server
client.
object, marshals the return
value and sends to the stub.
Skeleton
Network
Stub
The stub either creates a socket connection to the skeleton on
the server or reuses a pre-existing connection. It marshals all
the information associated to the method call, with name of
method and arguments and sends
to the skeleton.
CSIS0402
Client
Client
invokes a
method on
the stub
4
Recap of RMI Components
RMIRegistry
Runs on a central server and functions like a phone book
Maps logical names to actual servers so that client programs
can easily locate and use appropriate server applications
Stub
Acts as the local representative for the remote object
Implements the same remote interface as the remote object
Maintains a socket connection to the remote object’s JVM
automatically
Responsible for marshalling and de-marshalling data on the
client side
LAB 1
CSIS0402
5
Skeleton
A server-side object responsible for
maintaining network connections with the stub
marshalling and de-marshaling data on the server
side
Stubs and Skeletons are generated from
the implementation class files by a
command-line application called rmic
LAB 1
CSIS0402
6
Assignment One
Overview
LAB 1
CSIS0402
7
A Simple File Hosting System
OnlineDrive v1.0
Offer a sort of "network storage" for personal
backup, file access or file distribution
Support three sharing options
Personal
Can be viewed and downloaded by the file owner only.
Group
Public
In Assignment One, you only need to develop the GUI
and the simplest mode of sharing: Personal.
You are required to use RMI to handle ALL
communications between server and clients.
LAB 1
CSIS0402
8
GUI
From Java.Swing:
- JFrame
- JSplitPane
- JTree
- JTextArea
- JButton
- JLabel
…
LAB 1
CSIS0402
9
System Architecture
OnlineDriveServer.java
-Server interface that describes
the behavior of the server
implements
OnlineDriveClientImp.java
OnlineDriveServerImp.java
- Class that implement the server
- Client application that maintains
the Chatroom functionalilities
functions
Server side components
LAB 1
Client side component
CSIS0402
10
Remarks
Class names to be used…
Server class: OnlineDriveServerImp.java
Client class: OnlineDriveClientImp.java
RMIC for server
To generate stubs and skeletons
Make sure…
your server can be started by
> java <options> OnlineDriveServerImp
your client can be started by
> java <options> OnlineDriveClientImp <hostname>
LAB 1
CSIS0402
11
Lab Exercise on RMI
CountServer
LAB 1
CSIS0402
12
CountServer Overview
A RMI application consists of
a RMI server and
a client swing application.
Client inputs a message and sends to the
server.
Server receives the message, count the
number of characters and return the
count to the client.
Client displays the count on the GUI.
LAB 1
CSIS0402
13
System Architecture
CountServer interface
that describes the
behavior of the server
CountServer.java
extends
Class that
implements the
server functions
LAB 1
CountServerImp.java
CSIS0402
CountClientImp.java
Swing client
application that
captures user’s
input, sends to
the server and
displays server’s
reply to the GUI.
14
CountServer.java
(Interface class of the server)
Exception thrown by
RMI to signal that
something unforeseen
has happened at the
network level
…
import java.rmi.*;
public interface CountServer extends Remote {
public int countMsg(String msg) throws RemoteException;
}
Return types of the
remote method
LAB 1
CSIS0402
15
CountServerImp.java
(Implementation class of the server)
Automatically relates the instance into
the RMI runtime and listens for remote
method calls.
import java.rmi.*;
import java.rmi.server.*;
public class CountServerImp extends UnicastRemoteObject
implements CountServer {
public int countMsg(String msg) throws RemoteException {
return msg.length();
}
Return the
string to the
remote client
LAB 1
CSIS0402
16
CountServerImp.java(2)
(Implementation class of the server)
Create a CountServer
object to provide
services for clients
public static void main( String args[] ) {
…
CountServerImp server = new CountServerImp();
Naming.rebind( "//localhost/CountServer",server );
…
}
Static method of the naming registry
class to bind the server object to the
specified host with the name
‘CountServer’
LAB 1
CSIS0402
17
CountClientImp.java
(Implementation class of the client)
import java.rmi.*;
…
public class CountClientImp extends JFrame {
…
public CountClientImp( String hostname ) {
serverLookup();
…
}
Swing Application
Call the static method of the
Naming class to look up the
remote server on the
requested host and the
specified server name
private void serverLookup(){
try{
server = (CountServer)Naming.lookup( "//localhost/CountServer" );
} catch(Exception e){
System.err.println(e);
}
Get a handle
}
to the remote
object
LAB 1
CSIS0402
18
CountClientImp.java(2)
(Implementation class of the client)
JButton sendButton = new JButton(“Count Message");
…
sendButton.addActionListener(new ActionListener() {
Register the
public void actionPerformed(ActionEvent e){
action listener
sendMsgToServer();
for send button
}
…
private void sendMsgToServer(){
String msg = tArea.getText();
try{
Integer count = (Integer) server.countMsg(msg);
msgCount = Integer.toString(count);
tField.setText(msgCount);
}catch(RemoteException re) {
Remotely invoke
System.err.println(re);
the RMI Server
}
and send the
}
message to it
…
LAB 1
CSIS0402
19
CountClientImp.java (3)
(Implementation class of the client)
public static void main( String args[] ) {
…
Create a CountClientImp
object
try{
CountClientImp client = new CountClientImp();
}catch( Exception e ){
e.printStackTrace();
}
}
LAB 1
CSIS0402
20
Lab Exercise - How to start
Download the lab1.zip from the course
homepage
Compile the files by
javac *.java
Generate stub and skeleton
rmic CountServerImp
Start the RMI registry (keep on running)
rmiregistry
LAB 1
CSIS0402
21
Lab Exercise - How to start(2)
Start the RMI Server in another command
prompt
java CountServerImp
Start the client in another command
prompt and connect to the server
java CountClientImp
LAB 1
CSIS0402
22
Lab Exercise - How to start(3)
Test the application
Type a message in the text area on the Client
GUI, click “Count Message”.
Check if the count is returned and displayed
on the GUI.
LAB 1
CSIS0402
23
Reference (1)
Swing (for the GUI) in Java Tutorial
http://java.sun.com/docs/books/tutorial/uiswing
Getting start with Swing
http://java.sun.com/docs/books/tutorial/uiswing/start/index.
html
Layout Manager
http://java.sun.com/docs/books/tutorial/uiswing/layout/inde
x.html
Event Listener
http://java.sun.com/docs/books/tutorial/uiswing/events/inde
x.html
LAB 1
CSIS0402
24
Reference (2)
RMI in Java Tutorial
http://java.sun.com/docs/books/tutorial/rmi/ov
erview.html
RMI course from jGuru
http://developer.java.sun.com/developer/onlin
eTraining/rmi/RMI.html#JavaRMIArchitecture
LAB 1
CSIS0402
25