You are on page 1of 8

LABCYCLE 8: (VTU List of Programs)

Client-Dispatcher-Server
Example: A simplified implementation of RPC
Intent: A dispatcher component is an intermediary between clients and servers. The dispatcher
provides location transparency with a name service and hides details of the communication
connection.
Client:
Implements a system task.
Requests server connections from the dispatcher.
Invokes services of servers
Server:
Provides services to clients.
Registers itself with the dispatcher
Dispatcher:
Establishes communication channels between clients and servers.
Locates servers.
Registers/unregisters servers
Maintains a map of server locations
Context : A software system integrating a set of distributed servers, with the servers running locally or
distributed over a network.

Use Case Diagram

Class Diagram

Sequence Diagram

Communication Diagram

Activity Diagram

State Machine Diagram

Implementaion:
CDS.java
public class CDS extends PrintService
{
publicCDS(String srv, String svc)
{
super(srv, svc);
}
public static Dispatcher disp;
public static void main(String[] a)
{

disp=new Dispatcher();
Client c1=new Client();
Service svc = new PrintService("printservice1","server1");
Service svc1=new PrintService("printservice2","server2");
c1.doTask();
}
}
Client.java
public class Client {
publicDispatcher dispatcher;
publicPrintServiceprintService;
public void setPrintService(PrintServicethePrintService)
{
printService = thePrintService;
}
public void doTask()
{
Service s;
try
{
s=CDS.disp.locateServer("printservice1");
s.runService();
}
catch(Exception e) { System.out.println(e.getMessage()); }
try
{
s=CDS.disp.locateServer("printservice2");
s.runService();
}
catch(Exception e) { System.out.println("Not Found "); }
}
}
Dispatcher.java
importjava.util.Hashtable;
importjava.util.Random;
importjava.util.Vector;
classNotFoundextends Exception
{
private static final long serialVersionUID = 1L;
}
public class Dispatcher
{
publicClient client;
publicHashtableregistry=new Hashtable();
privateRandom rnd=new Random(123456);
public void registerService(String svc,Serviceobj)

{
Vector v=(Vector) registry.get(svc);
if(v==null)
{
v=new Vector();
registry.put(svc,v);
}
v.addElement(obj);
}
publicService locateServer(String svc) {
Vector v=(Vector)registry.get(svc);
if(v==null || v.size()==0)
{
try{ throw new NotFound(); }
catch(NotFound e)
{
e.printStackTrace();
}
}
inti=(rnd.nextInt())%(v.size());
return(Service)v.elementAt(i);
}
}
Service.java
public abstract class Service {
privateString nameofservice;
privateObject nameofserver;
publicString getNameofservice() {
returnnameofservice;
}
publicObject getNameofserver() {
returnnameofserver;
}
publicService(String svc, String srv) {
nameofservice=svc;
nameofserver=srv;
CDS.disp.registerService(nameofservice, this);
}
public void runService() { }
}
PrintService.java
public class PrintServiceextends Service {
publicPrintService(String srv, String svc) {
super(srv,svc);
}
public void runService() {

System.out.println("Service : "+super.getNameofservice()+" ran by


"+getNameofserver());
}
}
OUTPUT:Service : printservice1 ran by server1
Service : printservice2 ran by server2

You might also like