You are on page 1of 16

(TCS-852) DISTRIBUTED SYSTEMS LAB

The following programs should be developed preferably on UNIX platform:1. Simulate the functioning of Lamports Logical Clock in C. 2. Simulate the Distributed Mutual Exclusion in C. 3. Implement a Distributed Chat Server using TCP Sockets in C. 4. Implement RPC mechanism for a file transfer across a network in C 5. Implement Java RMI mechanism for accessing methods of remote systems. 6. Simulate Balanced Sliding Window Protocol in C. 7. Implement CORBA mechanism by using C++ program at one end and Java program on the other.

2. Simulate the Distributed Mutual Exclusion in C.

#include<stdio.h> #include<conio.h> #include<graphics.h>

void main() { int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); circle(90,50,20); circle(160,50,20); circle(230,50,20); circle(160,150,20); outtextxy(90,50,"0"); outtextxy(160,50,"1"); outtextxy(230,50,"2"); outtextxy(160,150,"3"); outtextxy(110,175,"CO-ORDINATOR"); delay(1000); outtextxy(90,100,"REQUEST"); line(150,68,150,130); line(150,130,145,125); line(150,130,155,125); delay(1000);

line(170,70,170,133); line(170,70,165,75); line(170,70,175,75); outtextxy(180,100,"REQUEST GRANTED"); outtextxy(50,250,"Process1 requesting for C.S. & the request is granted"); getch(); clrscr(); circle(90,50,20); circle(160,50,20); circle(230,50,20); circle(160,150,20); outtextxy(90,50,"0"); outtextxy(160,50,"1"); outtextxy(230,50,"2"); outtextxy(160,150,"3"); outtextxy(110,175,"CO-ORDINATOR"); delay(1000); outtextxy(110,100,"REQUEST"); line(222,68,160,130); line(160,130,173,125); line(160,130,163,120); delay(1000); setlinestyle(3,1,1); line(174,134,213,100); setlinestyle(0,1,1); line(213,100,212,108);

line(213,100,205,100); outtextxy(200,120,"NO REPLY"); outtextxy(20,240,"Process2 requesting for C.S. but another process is already in the C.S."); getch(); clrscr(); circle(90,50,20); circle(160,150,20); circle(230,50,20); circle(180,150,20); outtextxy(90,50,"0"); outtextxy(160,50,"1"); outtextxy(230,50,"2"); outtextxy(160,150,"3"); outtextxy(110,175,"CO-ORDINATOR"); delay(1000); outtextxy(110,100,"RELEASE"); line(150,68,150,130); line(150,130,155,125); line(150,130,155,125); delay(1000); line(220,70,170,133); line(220,70,210,75); line(220,70,210,75); //outtextxy(200,120,"NO REPLY"); //outtextxy(20,240,"Process2 requesting for C.S. but another process is already in the C.S.");

getch(); clrscr(); }

3. Implement a Distributed Chat Server using TCP Sockets in Java.

//Server import java.net.*; import java.io.*; public class servertcp { public static void main(String args[]) throws IOException { ServerSocket ss=new ServerSocket(12); Socket s=ss.accept(); System.out.println("Connection from "+s); PrintWriter pw1=new PrintWriter(s.getOutputStream(),true); BufferedReader br3=new BufferedReader(new InputStreamReader(s.getInputStream())); BufferedReader br4=new BufferedReader(new InputStreamReader(System.in)); System.out.println("i m ready"); String s1, s2; /*while((s1=br4.readLine())!=null) { pw1.println(s1); System.out.println(s2); } */ while(true) { do { s1=br4.readLine(); pw1.println(s1); } while(!s1.equals("over")); do { s2=br3.readLine(); System.out.println(s2); } while(!s2.equals("over")); } } }

// Client import java.net.*; import java.io.*; public class clienttcp { public static void main(String args[]) throws IOException { Socket cc=new Socket(InetAddress.getLocalHost(),12); PrintWriter pw=new PrintWriter(cc.getOutputStream(),true); BufferedReader br1=new BufferedReader(new InputStreamReader(cc.getInputStream())); BufferedReader br2=new BufferedReader(new InputStreamReader(System.in)); String str1, str2; /*while((str1=br2.readLine())!=null) { System.out.println(str1); //pw.println(str2); } */ while(true) { do { str1=br1.readLine(); System.out.println(str1); } while(!str1.equals("over")); do { str2=br2.readLine(); pw.println(str2); } while(!str2.equals("over")); } } }

4. Implement RPC mechanism for a file transfer across a network in C /* * rdate.c client program for remote date program

*/ #include <stdio.h> #include <rpc/rpc.h> /* standard RPC include file */ #include "date.h" /* this file is generated by rpcgen */ main(int argc, char *argv[]) { CLIENT *cl; /* RPC handle */ char *server; long *lresult; /* return value from bin_date_1() */ char **sresult; /* return value from str_date_1() */ if (argc != 2) { fprintf(stderr, "usage: %s hostname\n", argv[0]); exit(1); } server = argv[1]; /* * Create client handle */ if ((cl = clnt_create(server, DATE_PROG, DATE_VERS, "udp")) == NULL) { /* * cant establish connection with server */ clnt_pcreateerror(server); exit(2); } /*

* First call the remote procedure "bin_date". */ if ( (lresult = bin_date_1(NULL, cl)) == NULL) { clnt_perror(cl, server); exit(3); } printf("time on host %s = %ld\n",server, *lresult); /* * Now call the remote procedure str_date */ if ( (sresult = str_date_1(lresult, cl)) == NULL) { clnt_perror(cl, server); exit(4); } printf("time on host %s = %s", server, *sresult); clnt_destroy(cl); /* done with the handle */ exit(0); }

/* * date.x Specification of the remote date and time server */ /* * Define two procedures * bin_date_1() returns the binary date and time (no arguments)

* str_date_1() takes a binary time and returns a string * */ program DATE_PROG { version DATE_VERS { long BIN_DATE(void) = 1; /* procedure number = 1 */ string STR_DATE(long) = 2; /* procedure number = 2 */ } = 1; /* version number = 1 */ } = 0x31234567;

/* * dateproc.c remote procedures; called by server stub */ #include <time.h> #include <rpc/rpc.h> /* standard RPC include file */ #include "date.h" /* this file is generated by rpcgen */ /* * Return the binary date and time */ long *bin_date_1_svc(void *arg, struct svc_req *s) { static long timeval; /* must be static */ timeval = time((long *) 0); return(&timeval);

} /* * Convert a binary time and return a human readable string */ char **str_date_1_svc(long *bintime, struct svc_req *s) { static char *ptr; /* must be static */ ptr = ctime((const time_t *)bintime); /* convert to local time */ return(&ptr); }

5. Implement Java RMI mechanism for accessing methods of remote systems.

import java.rmi.*; public class AddClient { public static void main(String args[]) { try {

String addServerURL = "rmi://" + args[0] + "/Addserver"; AddServerIntf addServerIntf = (AddServerIntf)Naming.lookup(addServerURL); System.out.println("the first number is :"+args[1]); double d1 =Double.valueOf(args[1]).doubleValue(); System.out.println("the second number is :"+args[2]); double d2 =Double.valueOf(args[2]).doubleValue(); System.out.println("The sum is:" + addServerIntf.add(d1,d2)); } catch (Exception e) { System.out.println(" Exception:" +e); } } }

//Add Server import java.net.*; import java.rmi.*; public class AddServer { public static void main(String [] args) { try { AddServerImpl addServerImpl = new AddServerImpl(); Naming.rebind("Addserver",addServerImpl); } catch(Exception e) { System.out.println("Exception: " +e); } } }

// Add Server IMPL import java.rmi.*; import java.rmi.server.*; public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf { public AddServerImpl() throws RemoteException { } public double add(double d1, double d2) throws RemoteException { return d1+d2; }}

//ADD SERVER INTF import java.rmi.*; public interface AddServerIntf extends Remote { double add(double d1, double d2) throws RemoteException; }

6. Simulation of Sliding Window Protocol


#include"stdio.h" #include"conio.h" #include"stdlib.h" void getdata() { FILE *f1; char c; clrscr(); printf("\n\t\tEnter the data\n\t\t"); f1=fopen("sc.txt", "w"); while((c=getchar())!='\n') putc(c, f1); putc('\n', f1); fclose(f1); } void send() { FILE *f1, *f2; char c; int i, k=0, windowsize=2, randno, k1; f1=fopen("sc.txt", "r"); f2=fopen("ms.txt", "w"); loop: for(i=0; i < windowsize;i++) { c=getc(f1); if(c!='\n') { printf("%c\n", c); putc(c, f2); k++; } else

goto end; } fclose(f1); randomize(); randno=random(k);//generates a random number f1=fopen("sc.txt", "a"); putc(randno, f1); fclose(f1); k1=randno; printf("The Frames of data sent %d\n", k); getch(); f1=fopen("sc.txt", "r"); fseek(f1, k, SEEK_SET); goto loop; end: fclose(f1); fclose(f2); printf("The Frames of data sent %d\n", k); } void disp() { int choice; printf("\n\tChoose an option\n"); printf("\n\t1.Send"); printf("\n\t2.Quit"); scanf("%d", &choice); if(choice==1) send(); } void main() { clrscr(); getdata(); disp(); getch(); }

7. Implement CORBA mechanism by using C++ program at one end and Java program on the other.

//hello client import import import import HelloApp.*; org.omg.CosNaming.*; org.omg.CosNaming.NamingContextPackage.*; org.omg.CORBA.*;

public class HelloClient { static Hello helloImpl; public static void main(String args[]) { try{ // create and initialize the ORB ORB orb = ORB.init(args, null); // get the root naming context org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); // Use NamingContextExt instead of NamingContext. This is // part of the Interoperable naming Service. NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef); // resolve the Object Reference in Naming String name = "Hello"; helloImpl = HelloHelper.narrow(ncRef.resolve_str(name)); System.out.println("Obtained a handle on server object: " + helloImpl); System.out.println(helloImpl.sayHello()); helloImpl.shutdown(); } catch (Exception e) { System.out.println("ERROR : " + e) ; e.printStackTrace(System.out); } } } // HelloServer.java // Copyright and License import HelloApp.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; import org.omg.PortableServer.*;

import org.omg.PortableServer.POA; import java.util.Properties; class HelloImpl extends HelloPOA { private ORB orb; public void setORB(ORB orb_val) { orb = orb_val; } // implement sayHello() method public String sayHello() { return "\nHello world !!\n"; } // implement shutdown() method public void shutdown() { orb.shutdown(false); } } public class HelloServer { public static void main(String args[]) { try{ // create and initialize the ORB ORB orb = ORB.init(args, null); // get reference to rootpoa & activate the POAManager POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA")); rootpoa.the_POAManager().activate(); // create servant and register it with the ORB HelloImpl helloImpl = new HelloImpl(); helloImpl.setORB(orb); // get object reference from the servant org.omg.CORBA.Object ref = rootpoa.servant_to_reference(helloImpl); Hello href = HelloHelper.narrow(ref); // get the root naming context org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); // Use NamingContextExt which is part of the Interoperable // Naming Service (INS) specification. NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef); // bind the Object Reference in Naming String name = "Hello"; NameComponent path[] = ncRef.to_name( name ); ncRef.rebind(path, href); System.out.println("HelloServer ready and waiting ...");

// wait for invocations from clients orb.run(); catch (Exception e) { System.err.println("ERROR: " + e); e.printStackTrace(System.out); } System.out.println("HelloServer Exiting ...");

module HelloApp { interface Hello { string sayHello(); oneway void shutdown(); }; };

You might also like