Developing RMI Based Server (ChatServer)

Similar documents
Remote Method Invocation

JAVA RMI. Remote Method Invocation

Generic architecture

Remote Method Invocation

55:182/22C:182. Distributed Application Frameworks Java RMI, CORBA, Web Services (SOAP)

Distributed Applications Programming. Lab 4

1 interface TemperatureSensor extends java.rmi.remote 2 { 3 public double gettemperature() throws java.rmi.remoteexception; 4 public void

Distributed Applications Programming. Lab 7

JAC444 - Lecture 11. Remote Method Invocation Segment 2 - Develop RMI Application. Jordan Anastasiade Java Programming Language Course

Remote Procedure Call

COMP 6231 Distributed Systems Design. Tutorial 2 by Alexandre Hudon January 21 st, 2013

5.4. Events and notifications

03 Remote invocation. Request-reply RPC. Coulouris 5 Birrel_Nelson_84.pdf RMI

Remote Method Invocation

Component-Based Software Engineering

Chain of Responsibility Pattern

CC755: Distributed and Parallel Systems

Lab 2 : Java RMI. request sayhello() Hello interface remote object. local object. response "Hello world"

Questions and Answers. A. RMI allows us to invoke a method of java object that executes on another machine.

Remote Method Invocation in Java

Architecture of So-ware Systems RMI and Distributed Components. Mar<n Rehák

Object Interaction. Object Interaction. Introduction. Object Interaction vs. RPCs (2)

CS555: Distributed Systems [Fall 2017] Dept. Of Computer Science, Colorado State University

Written by: Dave Matuszek

Lecture 18 Inside Java RMI

RMI Example RMI. CmpE 473 Internet Programming RMI

RMI (Remote Method Invocation) Over the year, there have been 3 different approaches to application development:

Introduction & RMI Basics. CS3524 Distributed Systems Lecture 01

Distributed Systems COMP 212. Lecture 10 Othon Michail

Last Class: Network Overview. Today: Distributed Systems

RMI. Remote Method Invocation. 16-Dec-16

IBD Intergiciels et Bases de Données

presentation DAD Distributed Applications Development Cristian Toma

Contents. Java RMI. Java RMI. Java RMI system elements. Example application processes/machines Client machine Process/Application A

Agent and Object Technology Lab Dipartimento di Ingegneria dell Informazione Università degli Studi di Parma. Distributed and Agent Systems RMI

The UNIVERSITY of EDINBURGH. SCHOOL of INFORMATICS. CS4/MSc. Distributed Systems. Björn Franke. Room 2414

Java RMI Activation: A running example We have the following classes: MyRemoteInterface: the remote interface. Client: the client that invokes a

IJESRT. http: //

Instance Method Development Demo

Distributed Systems Project 4 Assigned: Friday March 20 Due: Friday April 3, 11:59pm

DISTRIBUTED COMPUTING

Distributed Systems. 02r. Java RMI Programming Tutorial. Paul Krzyzanowski TA: Long Zhao Rutgers University Fall 2017

CSci Introduction to Distributed Systems. Communication: RPC In Practice

Grid Computing. Java Remote Method Invocation (RMI) RMI Application. Grid Computing Fall 2006 Paul A. Farrell 9/5/2006

RMI Case Study. A Typical RMI Application

Reflection/RMI 4/28/2009

CS193k, Stanford Handout #12. Threads 4 / RMI

Distributed Computing

RMI. (Remote Method Invocation)

Question1 (10 points) : Patron Aggregator

Chapter 4 Remote Procedure Calls and Distributed Transactions

Info 408 Distributed Applications Programming Exercise sheet nb. 4

Communication and Distributed Processing

Chapter 15: Distributed Communication. Sockets Remote Procedure Calls (RPCs) Remote Method Invocation (RMI) CORBA Object Registration

Concurrent Programming (RIO) Lesson 9. Ch 8 [BenA 06] Messages Channels Rendezvous RPC and RMI. X=f(..); send X to B. OS kernel.

Communication and Distributed Processing

Desarrollo de Aplicaciones en Red RMI. Introduction. Considerations. Considerations. RMI architecture

Concurrency Control in Distributed Environment

Remote Method Invocation R.M.I.

Applications. RMI, RPC and events. Request reply protocol External data representation. Operating System

Bharati Vidyapeeth s Institute of Computer Applications and Management A-4, Paschim Vihar, New Delhi-63.

Concurrent programming, Autumn 2006, Project: -Part A and C-

4:40pm - 6:10pm (90 min)

Object-Oriented Systems Design RMI

Distributed Systems. 5. Remote Method Invocation

Distributed Objects SPL/ SPL 201 / 0 1

Modulo II Socket, RMI e Corba

RPC is a call to a procedure/function located on another machine

Creating a Client Program

Outline. EEC-681/781 Distributed Computing Systems. The OSI Network Architecture. Inter-Process Communications (IPC) Lecture 4

Distributed Software Systems

A Typical RMI Application. Case Study

Distributed object component middleware I - Java RMI

Verteilte Systeme (Distributed Systems)

Info 408 Distributed Applications programming 2 nd semester of Credits: 5 Lecturer: Antoun Yaacoub Ph.D.

COMP 6231: Distributed System Design

Distributed object component middleware I - Java RMI

Remote Objects and RMI

Distributed Programming in Java. Distribution (2)

Distributed Objects. Remote Method Invokation

Java Classes - Using your classes. How the classes you write are being used

CS 5523 Operating Systems: Remote Objects and RMI

Distributed Systems. Distributed Object Systems 2 Java RMI. Java RMI. Example. Applet continued. Applet. slides2.pdf Sep 9,

Remote Method Invocation. Benoît Garbinato

Remote Method Invocation Benoît Garbinato

Course Snapshot. The Next Few Classes. Parallel versus Distributed Systems. Distributed Systems. We have covered all the fundamental OS components:

DISTRIBUTED OBJECTS AND REMOTE INVOCATION

Firewall Issues. The possible scenarios: the RMI client, the server, or both can be operating from behind a firewall

Course Snapshot. The Next Few Classes

0.8.0 SimpleConsumer Example

Outline. Chapter 4 Remote Procedure Calls and Distributed Transactions. Remote Procedure Call. Distributed Transaction Processing.

RMI - Remote Method Invocation SYSTEM SOFTWARE 1

Activation of remote objects

RPC flow. 4.3 Remote procedure calls IDL. RPC components. Procedure. Program. sum (j,k) int j,k; {return j+k;} i = sum (3,7); Local procedure call

Web. Remote Method Invocation. core. programming. Training Courses: Java, JSP, Servlets, Struts, & JSF:

On Designing and Implementing a Collaborative System Using Java-RMI

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Distributed Programming with RMI. Overview CORBA DCOM. Prepared By: Shiba R. Tamrakar

2018/2/5 话费券企业客户接入文档 语雀

EAST WEST UNIVERSITY

Transcription:

Introduction Developing RMI Based Server (ChatServer) In the last module, we have learnt about RMI. In this module we will look at how to create an interactive application like chat server using RMI. Demo Of creating ChatServer using RMI. In this demo we will develop a ChatServer. Developing the Chat Server application using the RMI would require the following components. A ChatServer interface, which would be the remote interface A ChatClient interface, which is again another remote interface from client side An implementation of the ChatServer interface (ChatServerImpl) An application to create instance of ChatServer and make it available (ChatServerMain) A GUI implementation of the ChatClient interface (ChatClientGUIImpl) A GUI application for the client side (ChatFrame) Developing the Chat Server The entire Chat Server will be developed in two modules. This module is the first module and will cover the following components ChatServer interface ChatClient interface ChatServerImpl class ChatServerMain class In the next module we will develop the GUI client application.

Developing the ChatServer interface The ChatServer interface is the Remote interface, and has methods which will be invoked by the client. The following methods are identified as interaction from client to the server. String[] login(chatclient client), this is used for logging in a ChatClient with the Server, it would return an array containing the names of the other clients who are currently logged in. String[] list(), this can be used for getting the list names of the clients who are currently logged in. void sendmessage(string from,, String to, String message), this method can by client for sending a message to another loggedin client. void sendmessage(string from, String message), this method can be used by client to send message to all the currently loggedin clients. void logout(string name), this method can be used by a client to logout. A remote interface has to extend from the interface called Remote and all the methods must have a throws for the RemoteException. The code for ChatServer interface is as follows: Source code of ChatServer interface package org.epgpathshala.dad.rmi.server; import java.rmi.remote; import org.epgpathshala.dad.rmi.client.chatclient; public interface ChatServer extends Remote { String[] login(chatclient client) throws RemoteException; String[] list() throws RemoteException; void sendmessage(string from, String to, String message) throws RemoteException; void sendmessage(string from, String message) throws RemoteException; void logout(string name) throws RemoteException;

Developing the ChatClient interface The ChatClient interface is the Remote interface, and has methods which will be invoked by the server. The following methods are identified as interaction from server to the client. String getname(), this method is used by the server to get the name of the client. void joined(string name), this method is used by the server to indicate to the client that a new client has logged in. void left(string name), this method is used by the server to indicate to the client that a client has logged out. void showmessage(string from, String message), this method is used by the server to ask the client to show a new message, which has been received for that client. A remote interface has to extend from the interface called Remote and all the methods must have a throws for the RemoteException. The code for ChatClient interface is as follows: Source code of ChatClient interface package org.epgpathshala.dad.rmi.client; import java.rmi.remote; public interface ChatClient extends Remote { String getname() throws RemoteException; void joined(string name) throws RemoteException; void left(string name) throws RemoteException; void showmessage(string from, String message) throws RemoteException;

Developing the ChatServerImpl The ChatServerImpl class is the implementation of the ChatServer interface. This class, has to extend from the UnicastRemoteObject class of the java.remote.server package, and implement the ChatServer interface (the remote interface developed earlier). This implementation class must have a constructor which has a throws for RemoteException. In our implementation here, the ChatServerImpl maintains all the Remote objects for the logged in ChatClients, name-wise. This is done by using a Map<String, ChatClient>. So, this class used such a map as instance variable, called clientmap. The implementation of the various methods is as follows: public String[] login(chatclient client), when this method is invoked by the client, it passes an instance of the ChatClient, this method. The method would get the name of the client and check if another is already loggedin with this name and throw exception, if that is the case, otherwise it would get a list of all the client names in an array and then add the new client in the map. It would then invoke the joined method on all the other loggedin clients to send the name of the new client logging in and then returns the list of all the other client names to the client logging in. public String[] list(), this method is provided for a client to get a list of all the loggedin clients, it does this by get the client names from the clientmap. public void sendmessage(string from, String to, String message), this method is used by a client to send a message to another client. The method fetches the ChatClient object for the recipient client from the clientmap and invokes the show Message method on it. public void sendmessage(string from, String message), this method is used by a client to send a message to all the currently loggedin clients. This method simply gets the list of all the currently loggedin users by using the list method and then invokes the previous methods by using the recipient from the array received from the list. public void logout(string name), when this method is invoked by the client, it passes its name to this method. The method would remve the entry from the clientmap for this client and then gets a list of all the client names in an array and invokes the left method on all the other loggedin clients to send the name of the this client logging out. The code for the ChatServerImpl is as follows: Source code of ChatServerImpl class

package org.epgpathshala.dad.rmi.server; import java.rmi.remote; import java.rmi.server.unicastremoteobject; import org.epgpathshala.dad.rmi.client.chatclient; import java.util.map; import java.util.hashmap; public class ChatServerImpl extends UnicastRemoteObject implements ChatServer { private Map<String, ChatClient> clientmap = new HashMap<>(); public ChatServerImpl() throws RemoteException { public String[] login(chatclient client) throws RemoteException { String name = client.getname(); if (clientmap.containskey(name)) { throw new RuntimeException("name already in use"); String[] clientnames = list(); clientmap.put(name, client); for (String clientname : clientnames) { clientmap.get(clientname).joined(name); return clientnames; public String[] list() { return clientmap.keyset().toarray(new String[clientMap.size()]); public void sendmessage(string from, String to, String message) throws RemoteException { clientmap.get(to).showmessage(from, message); public void sendmessage(string from, String message) throws RemoteException { String[] clientnames = list(); for (String clientname : clientnames) { sendmessage(from, clientname, message); public void logout(string name) throws RemoteException { clientmap.remove(name); String[] clientnames = list(); for (String clientname : clientnames) { clientmap.get(clientname).left(name);

Developing the ChatServerMain The ChatServerMain class contains the main method its only purpose is to create the object of ChatServerImpl and make it available for lookup by a client. The code for ChatServerMain class is as follows: Source code of ChatServerMain class package org.epgpathshala.dad.rmi.server; import java.rmi.registry.locateregistry; import java.rmi.registry.registry; public class ChatServerMain { public static void main(string[] args) throws RemoteException { String chatroom = args[0]; Registry r = LocateRegistry.createRegistry(1099); ChatServer server = new ChatServerImpl(); r.rebind(chatroom, server);