Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Distributed Systems 2018-2019
- M.Sc. I.T. Part I
- Semester I
- DISTRIBUTED SYSTEM
- PRACTICAL
- 2018 – 2019
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 1
- Distributed Systems 2018-2019
- Vidyalankar School of Information Technology
- Wadala (East), Mumbai – 400037
- CERTIFICATE
- This is to certify that, Mr. MD Taz Saheb Seat Number ______________ studying in Master of Science in Information Technology Part-I (Semester-I) has satisfactorily completed the Practical in the subject of “DISTRIBUTED SYSTEM” as prescribed by University of Mumbai, during the academic year 2018-2019.
- Subject Faculty Coordinator, M.Sc. (IT)
- External Examiner
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 2
- Distributed Systems 2018-2019
- INDEX
- Sr.
- Title
- Date
- Page
- Signature
- No.
- No.
- 1.
- Implement the concept for sharing
- the resources using distributed
- system.
- 2.
- Write a program for implementing
- Client Server communication model.
- 3.
- Write a program to show the object
- communication using RMI.
- 4.
- Show the implementation of Remote
- Procedure Call.
- 5.
- Show the implementation of Web
- Services.
- 6.
- Write a program to execute any one
- mutual exclusion algorithm.
- 7.
- Write a program to implement any
- one election algorithm.
- 8.
- Show the implementation of any one
- clock synchronization algorithm.
- 9.
- Implement the concept of distributed
- file system architecture.
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 3
- Distributed Systems 2018-2019
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- _______________________________________
- Distributed Systems 2018-2019
- Practical No: 01
- Aim: Implement the concept for sharing the resources using distributed system.
- Create a workgroup myworkgroup on windows machine and share a folder.
- If samba package is not installed on your linux machine then install it first.
- #vi /etc/samba/smb.conf
- Modify the file accordingly
- Workgroup = MYGroup
- Server String = Samba Server Version %v
- Netbios name = MYSERVER
- interface = lo eth0 192.154.2.10/24
- host allow 127. 192.154.12.
- Remove the semicolon at the start of the above lines if any.
- Go to end and add
- [common]
- Comment=shared folder
- Path = /common
- Public =yes
- Writable = yes
- Browsable yes
- Save the file
- #useradd mona
- #smbpasswd –a mona
- Will ask for password
- Enter the password
- Retype the password
- #/etc/init.d/smb restart
- #chkconfig cmb on
- #testparm
- #setenforce 0(zero)
- #setsebool samba_enable_home+dirs=1
- #getsebool –a | less
- The ip address in the following command is the ip address of windows machine. #smbclient //192.154.2.10/common –U mona
- Enter password
- Smb>mkdir ppbrdr
- Will create ppbrdr directory in shared folder
- Smb>get rdr.txt
- Read the file rdr.txt
- Smb>put ppb.txt
- Will create a file named ppb.txt
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 5
- Distributed Systems 2018-2019
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 6
- Distributed Systems 2018-2019
- Conclusion: Resources are shared between Windows and Linux.
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 7
- Distributed Systems 2018-2019
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 8
- Distributed Systems 2018-2019
- Practical No: 02
- Aim: Write a program for implementing Client Server communication model.
- A client server based program using TCP
- TCPClient.java import java.net.*; import java.io.*; import java.util.*;
- class TCPClient
- {
- public static void main(String args[ ] ) throws Exception
- {
- Socket s = new Socket( "127.0.0.1", 2010 ); Scanner kbin = new Scanner( System.in );
- DataInputStream in = new DataInputStream(s.getInputStream( )); DataOutputStream out = new DataOutputStream(s.getOutputStream( ));
- while( true )
- {
- out.writeUTF( kbin.next ( ) ); System.out.println( "Server: " +in.readUTF( ) );
- }
- }
- }
- TCPServer.java
- import java.net.*;
- import java.io.*;
- class TCPServer
- {
- public static void main(String args[ ] ) throws Exception {
- ServerSocket ss = new ServerSocket( 2010 );
- while( true )
- {
- Socket s = ss.accept( );
- DataInputStream in = new DataInputStream(s.getInputStream( )); DataOutputStream out = new DataOutputStream(s.getOutputStream( ));
- String msg="";
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 9
- Distributed Systems 2018-2019
- do
- {
- msg = in.readUTF( );
- System.out.println(msg);
- out.writeUTF( msg );
- }while( ! msg.equals("quit") );
- }
- }
- }
- Output:
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 10
- Distributed Systems 2018-2019
- B) A client server based program for TCP marshalling of objects.
- Complex.java
- import java.io.*;
- class Complex implements Serializable
- {
- int a , b;
- Complex( int x, int y )
- {
- a = x;
- b = y;
- }
- public String toString( )
- {
- return "[ "+ a +" + i " + b + " ]";
- }
- }
- MarshallingServer.java
- import java.net.*;
- import java.io.*;
- class MarshallingServer
- {
- public static void main(String args[ ] ) throws Exception {
- ServerSocket ss = new ServerSocket( 2010 );
- while( true )
- {
- Socket s = ss.accept( );
- System.out.println("Connection established...");
- ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream( )); ObjectInputStream in = new ObjectInputStream(s.getInputStream( ));
- Complex c1 = (Complex) in.readObject();
- System.out.println( "Received object " +c1 );
- Complex c2 = (Complex) in.readObject();
- System.out.println( "Received object " +c2 );
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 11
- Distributed Systems 2018-2019
- Complex c3 = new Complex( c1.a+c2.a , c1.b+c2.b );
- System.out.println( "Sending object " +c3 );
- out.writeObject( c3 );
- out.flush();
- s.close();
- }
- }
- }
- MarshallingClient.java
- import java.net.*;
- import java.io.*;
- import java.util.*;
- class MarshallingClient
- {
- public static void main(String args[ ] ) throws Exception {
- Socket s = new Socket( "127.0.0.1", 2010 );
- System.out.println( "connected");
- ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream( )); ObjectInputStream in = new ObjectInputStream(s.getInputStream( ));
- Complex c1 = new Complex( 3, 5 );
- Complex c2 = new Complex( -1, 4 );
- out.writeObject( c1 );
- out.flush();
- out.writeObject( c2 );
- out.flush();
- System.out.println( "Sum is : " + (Complex) in.readObject( ) );
- }
- }
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 12
- Distributed Systems 2018-2019
- Output:
- A client server based program using UDP
- UDPSender.java
- import java.net.*; class UDPSender
- {
- public static void main(String args[]) throws Exception
- {
- InetAddress ia = InetAddress.getByName( "vsit-y101-07" ); int port = 1099;
- String msg = " This is a great subject"; byte []buf = msg.getBytes();
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 13
- Distributed Systems 2018-2019
- DatagramPacket dp = new DatagramPacket( buf , buf.length, ia , port );
- DatagramSocket ds = new DatagramSocket( ) ;
- ds.send( dp );
- System.out.println ( msg + " sent to " + ia + ":" +port );
- ds.receive( dp );
- String msg2 = new String( dp.getData() );
- System.out.println ("Echo Received "+msg2.trim());
- }
- }
- UDPReceiver.java
- import java.net.*;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- class UDPReceiver {
- public static void main(String args[]) throws Exception
- {
- DatagramSocket ds = new DatagramSocket( 1099 ) ; while( true ){
- byte []buf = new byte [1000];
- DatagramPacket dp = new DatagramPacket( buf , buf.length );
- ds.receive( dp );
- String msg = new String( dp.getData() );
- Date date = new Date();
- System.out.println ( "Received : " + msg.trim() + " from " + dp.getAddress() + ":" + dp.getPort() + " at " + date );
- ds.send(dp);
- }
- }
- }
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 14
- Distributed Systems 2018-2019
- Output:
- D) A client server based program for UDP echo chat.
- UDPClient.java
- import java.net.*;
- class UDPSender {
- public static void main(String args[]) throws Exception {
- InetAddress ia = InetAddress.getByName( "LAPTOP-R8KE5PAE" ); int port = 1099;
- String msg = " This is Aditya";
- byte []buf = msg.getBytes();
- DatagramPacket dp = new DatagramPacket( buf , buf.length, ia , port ); DatagramSocket ds = new DatagramSocket( ) ;
- ds.send( dp );
- System.out.println ( msg + " sent to " + ia + ":" +port ); ds.receive( dp );
- String msg2 = new String( dp.getData() ); System.out.println ("Echo Received "+msg2.trim());
- }
- }
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 15
- Distributed Systems 2018-2019
- UDPServer.java
- import java.net.*;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- class UDPReceiver {
- public static void main(String args[]) throws Exception
- {
- DatagramSocket ds = new DatagramSocket( 1099 ) ;
- while( true ){
- byte []buf = new byte [1000];
- DatagramPacket dp = new DatagramPacket( buf , buf.length ); ds.receive( dp );
- String msg = new String( dp.getData() );
- Date date = new Date();
- System.out.println ( "Received : " + msg.trim() + " from " + dp.getAddress() + ":" + dp.getPort() + " at " + date );
- ds.send(dp);
- }
- }
- }
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 16
- Distributed Systems 2018-2019
- Output:
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 17
- Distributed Systems 2018-2019
- E) A multicast Socket program
- MulticastSender.java
- import java.net.*;
- class MulticastSender
- {
- public static void main( String args[] )throws Exception {
- Which port should we send to int port = 5000;
- Which address
- Create the socket but we don't bind it as we are only going to send data MulticastSocket s = new MulticastSocket();
- Note that we don't have to join the multicast group if we are only
- sending data and not receiving
- Fill the buffer with some data
- byte buf[] = new java.util.Date().toString().getBytes();
- // Create a DatagramPacket
- DatagramPacket pack = new DatagramPacket(buf, buf.length, InetAddress.getByName(group), port);
- Do a send. Note that send takes a byte for the ttl and not an int. s.send(pack);
- And when we have finished sending data close the socket s.close();
- }
- }
- MulticastReceiver.java
- import java.net.*;
- class MulticastReceiver
- {
- public static void main(String args[]) throws Exception {
- Which port should we listen to int port = 5000;
- Which address
- Create the socket and bind it to port 'port'. MulticastSocket s = new MulticastSocket(port);
- join the multicast group s.joinGroup(InetAddress.getByName(group)); int ctr=0;
- while(true)
- {
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 18
- Distributed Systems 2018-2019
- Now the socket is set up and we are ready to receive packets
- Create a DatagramPacket and do a receive
- byte buf[] = new byte[1024];
- DatagramPacket pack = new DatagramPacket(buf, buf.length); s.receive(pack);
- Finally, let us do something useful with the data we just received,
- like print it on stdout :-)
- System.out.println("Received data from: " + pack.getAddress() + ":" + pack.getPort() +"\t" );
- System.out.println(new String(pack.getData()).trim()); System.out.println();
- And when we have finished receiving data leave the multicast group if( ++ctr == 999) break;
- }
- close the socket s.leaveGroup(InetAddress.getByName(group)); s.close();
- }
- }
- Output:
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 19
- Distributed Systems 2018-2019
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 20
- Distributed Systems 2018-2019
- Practical No: 03
- Aim: Write a program to show the object communication using RMI.
- Code:
- Adder.java
- import java.rmi.*;
- public interface Adder extends Remote{
- public int add(int x,int y)throws RemoteException;
- }
- AdderRemote.java
- import java.rmi.*;
- import java.rmi.server.*;
- public class AdderRemote extends UnicastRemoteObject implements Adder {
- public AdderRemote()throws RemoteException { super();
- }
- public int add(int x,int y) {
- return x+y;
- }
- }
- MyClient.java
- import java.rmi.*;
- public class MyClient{
- public static void main(String args[]){
- try{
- Adder x=(Adder)Naming.lookup("rmi://localhost:5555/vsitadderservice"); System.out.println(x.add(34,4));
- }catch(Exception e){System.out.println(e);}
- }
- }
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 21
- Distributed Systems 2018-2019
- MyServer.java
- import java.rmi.*;
- import java.rmi.registry.*;
- public class MyServer{
- public static void main(String args[]){
- try{
- variable will be of Interface i.e. a1 and Object will be created
- of that class who has extended the interface i.e.AdderRemote()
- Adder a1=new AdderRemote(); Naming.rebind("rmi://localhost:5555/vsitadderservice",a1);
- }catch(Exception e){System.out.println(e);}
- }
- }
- Output:
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 22
- Distributed Systems 2018-2019
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 23
- Distributed Systems 2018-2019
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 24
- Distributed Systems 2018-2019
- Practical No: 04
- Aim: Show the implementation of Remote Procedure Call.
- Code:
- RPCClient.java
- import java.io.*;
- import java.net.*;
- class RPCClient
- {
- public static void main(String[] args)
- {
- try
- {InetAddress ia = InetAddress.getLocalHost(); DatagramSocket ds = new DatagramSocket(); DatagramSocket ds1 = new DatagramSocket(1300); System.out.println("\nRPC Client\n");
- System.out.println("Enter method name and parameter (eg. add 3 4)\n");
- while (true)
- {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String str = br.readLine();
- byte b[] = str.getBytes();
- DatagramPacket dp = new DatagramPacket(b,b.length,ia,1200); ds.send(dp);
- dp = new DatagramPacket(b,b.length);
- ds1.receive(dp);
- String s = new String(dp.getData(),0,dp.getLength()); System.out.println("\nResult = " + s + "\n");
- }
- } catch (Exception e){e.printStackTrace();}
- }
- }
- RPCServer.java
- import java.util.*;
- import java.net.*;
- class RPCServer
- {
- DatagramSocket ds;
- DatagramPacket dp;
- RPCServer()
- {
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 25
- Distributed Systems 2018-2019
- String str="",methodName="";
- int val1=0,val2=0;
- double result=0;
- try{
- ds=new DatagramSocket(1200);
- while(true)
- {
- byte b[]=new byte[1000];
- dp=new DatagramPacket(b,b.length);
- ds.receive(dp);
- str=new String(dp.getData(),0,dp.getLength());
- if(str.equalsIgnoreCase("q"))
- continue;
- StringTokenizer st = new StringTokenizer(str," ");
- int i=0;
- while(st.hasMoreTokens())
- {
- methodName = st.nextToken();
- val1 = Integer.parseInt(st.nextToken());
- val2 = Integer.parseInt(st.nextToken());
- }
- System.out.println(str);
- InetAddress ia = InetAddress.getLocalHost();
- if(methodName.equals("add")) result = add(val1,val2); else if(methodName.equals("sub")) result = sub(val1,val2); else if(methodName.equals("mul")) result = mul(val1,val2); else if(methodName.equals("div")) result = div(val1,val2);
- byte b1[]=new String(result+"").getBytes(); DatagramSocket ds1 = new DatagramSocket();
- DatagramPacket dp1 = new DatagramPacket (
- b1,b1.length,InetAddress.getLocalHost(), 1300); System.out.println("result : "+result+"\n"); ds1.send(dp1);
- }
- }catch (Exception e){ e.printStackTrace(); }
- }
- public double add(int val1, int val2)
- {
- return val1+val2;
- }
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 26
- Distributed Systems 2018-2019
- public double sub(int val3, int val4)
- {
- return val3-val4;
- }
- public double mul(int val3, int val4)
- {
- return val3*val4;
- }
- public double div(int val3, int val4)
- {
- return val3 / (double)val4;
- }
- public static void main(String[] args)
- {
- new RPCServer();
- }
- }
- Output:
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 27
- Distributed Systems 2018-2019
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 28
- Distributed Systems 2018-2019
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 29
- Distributed Systems 2018-2019
- Practical No. 05
- Aim: Show the implementation of web services.
- Implementing “Big” Web Service.
- 1) Creating a Web Service
- A. Choosing a Container:
- 1. Choose File > New Project. Select Web Application from the Java Web.
- 2. Name the project CalculatorWSApplication. Select a location for the project. Click Next.
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 30
- Distributed Systems 2018-2019
- 3. Select your server and Java EE version and click Finish
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 31
- Distributed Systems 2018-2019
- B. Creating a Web Service from a Java Class
- 1. Right-click the CalculatorWSApplication node and choose New > Web Service.
- Name the web service CalculatorWS and type org.me.calculator in Package. Leave Create Web Service from Scratch selected. If you are creating a Java EE 6 project on GlassFish or WebLogic, select Implement Web Service as a Stateless Session Bean.
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 32
- Distributed Systems 2018-2019
- Click Finish. The Projects window displays the structure of the new web service and the source code is shown in the editor area.
- 2) Adding an Operation to the Web Service
- The goal of this exercise is to add to the web service an operation that adds two numbers received from a client. The NetBeans IDE provides a dialog for adding an operation to a web service. You can open this dialog either in the web service visual designer or in the web service context menu.
- A. To add an operation to the web service:
- Change to the Design view in the editor.
- Click Add Operation in either the visual designer or the context menu. The Add Opera-tion dialog opens.
- In the upper part of the Add Operation dialog box, type add in Name and type int in the Return Type drop-down list.
- In the lower part of the Add Operation dialog box, click Add and create a parameter of type int named i.
- Click Add again and create a parameter of type int called j. You now see the following:
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 33
- Distributed Systems 2018-2019
- Click OK at the bottom of the Add Operation dialog box. You return to the editor.
- The visual designer now displays the following:
- Click Source. And code the following.
- @WebMethod(operationName = "add")
- public int add(@WebParam(name = "i") int i, @WebParam(name = "j") int j)
- {
- int k = i + j;
- return k;
- }
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 34
- Distributed Systems 2018-2019
- 3) Deploying and Testing the Web Service
- After you deploy a web service to a server, you can use the IDE to open the server's test client, if the server has a test client. The GlassFish and WebLogic servers provide test clients.
- A. To test successful deployment to a GlassFish or WebLogic server:
- Right-click the project and choose Deploy. The IDE starts the application server, builds the application, and deploys the application to the server.
- In the IDE's Projects tab, expand the Web Services node of the CalculatorWSApplication project. Right-click the CalculatorWS node, and choose Test Web Service.
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 35
- Distributed Systems 2018-2019
- The IDE opens the tester page in your browser, if you deployed a web application to the GlassFish server.
- If you deployed to the GlassFish server, type two numbers in the tester page, as shown below
- The sum of the two numbers is displayed:
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 36
- Distributed Systems 2018-2019
- 4) Consuming the Web Service
- Now that you have deployed the web service, you need to create a client to make use of the web service's add method.
- Client: Java Class in Java SE Application
- Choose File > New Project. Select Java Application from the Java category. Name the project CalculatorWS_Client_Application. Leave Create Main Class selected and accept all other default settings. Click Finish.
- Right-click the CalculatorWS_Client_Application node and choose New > Web Service Client. The New Web Service Client wizard opens.
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 37
- Distributed Systems 2018-2019
- Select Project as the WSDL source. Click Browse. Browse to the CalculatorWS web ser-vice in the CalculatorWSApplication project. When you have selected the web service, click OK.
- Do not select a package name. Leave this field empty.
- Leave the other settings at default and click Finish. The Projects window displays the new web service client, with a node for the add method that you created:
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 38
- Distributed Systems 2018-2019
- Double-click your main class so that it opens in the Source Editor. Drag the add node be-low the main() method.
- You now see the following:
- public static void main(String[] args)
- {
- TODO code application logic here
- }
- private static int add(int i, int j)
- {
- org.me.calculator.CalculatorWS_Service service = new org.me.calculator.CalculatorWS_Service(); org.me.calculator.CalculatorWS port = service.getCalculatorWSPort(); return port.add(i, j);
- }
- 7. In the main() method body, replace the TODO comment with code that initializes values for i and j, calls add(), and prints the result.
- Distributed Systems – Practical Manual – 2013-2014 M.Sc. – I.T. – Semester I Page 37
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 39
- Distributed Systems 2018-2019
- public static void main(String[] args)
- { int i = 3; int j = 4; int result = add(i, j); System.out.println("Result = " + result);
- }
- 8. Surround the main() method code with a try/catch block that prints an exception.
- public static void main(String[] args)
- { try
- { int i = 3; int j = 4; int result = add(i, j); System.out.println("Result = " + result); } catch (Excep-tion ex) { System.out.println("Exception: " + ex); }
- }
- Right-click the project node and choose Run.
- The Output window now shows the sum:
- compile:
- run:
- Result = 7
- BUILD SUCCESSFUL (total time: 1 second)
- Practical 5B: Implementing Web Service that connects to MySQL database.
- Creating MySQL DB Table create database bookshop;
- use bookshop;
- ✓ Create a table named Books that will store valid books information
- create table books(isbn varchar(20) primary key, bookname varchar(100), bookprice var-char(10));
- ✓ Insert valid records in the Books table
- insert into books values("111-222-333","Learn My SQL","250");
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 40
- Distributed Systems 2018-2019
- insert into books values("111-222-444","Java EE 6 for Beginners","850"); insert into books values("111-222-555","Programming with Android","500"); insert into books values("111-222-666","Oracle Database for you","400");
- insert into books values("111-222-777","Asp.Net for advanced programmers","1250");
- Creating a web service i. Choosing a container
- in a Web application.
- ii. Creating a web application
- - New Project.
- Web Application available under the Projects section. Click Next.
- Name textbox and select the option Use Dedicated Folder for Storing Libraries.
- ∙ Click Next. Server and Settings section of the New Web Application dialog box appears. Choose the default i.e. GlassFish v3 Domain as the Web server, the Java EE 6 Web as the Java
- version and the Context Path. -Finish
- iii. Creating a web service
- -click the BookWS project and select New -> Web Service as shown in diagram.
- textbox, webservice in the Package textbox, select the option Create Web Service from scratch and also select the option implement web service as a stateless session bean as shown in the dia-gram.
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 41
- Distributed Systems 2018-2019
- 3) Designing the web service
- Now add an operation which will accept the ISBN number from the client to the web service. i. Adding an operation to the web service
- below the name of the BookWS.java tab.
- ∙ Click Add Operation available in the design view of the web service.
- a-
- va.lang.String in the Return Type textbox as shown in the diagram.
- as shown in the diagram.
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 42
- Distributed Systems 2018-2019
- The code spec expands due to the operation added to the web service as shown
- in the diagram.
- ∙ Modify the code spec of the web service BookWS.java.
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 43
- Distributed Systems 2018-2019
- Code Spec
- package webservice;
- import java.sql.*;
- import javax.jws.WebMethod;
- import javax.jws.WebParam;
- import javax.jws.WebService;
- import javax.ejb.Stateless;
- @WebService()
- @Stateless()
- public class BookWS {
- /**
- Web service operation */
- @WebMethod(operationName = "getBookDetails")
- public String getBookDetails(@WebParam(name = "isbn") String isbn) {
- //TODO write your implementation code here: Connection dbcon = null;
- Statement stmt = null; ResultSet rs = null; String query = null; try
- {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- dbcon = DriverManager.getConnection("jdbc:mysql://localhost/bookshop","root","123"); stmt = dbcon.createStatement();
- query = "select * from books where isbn = '" +isbn+ "'"; rs = stmt.executeQuery(query);
- rs.next();
- String bookDetails = "<h1>The name of the book is <b>" +rs.getString("bookname") + "</b> and its cost is <b>" +rs.getString("bookprice") + "</b></h1>.";
- return bookDetails;
- }
- catch(Exception e)
- {
- System.out.println("Sorry failed to connect to the database.." + e.getMessage());
- }
- return null;
- }
- }
- Explanation
- n-
- tered by the user, the associated book name and price is retrieved and returned.
- 4) Adding the MySQL connector
- need to add a reference of MySQL connector to our web service. It is via this connector that our web service will be able to communicate with the database.
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 44
- Distributed Systems 2018-2019
- the location where mysql-coonector-java-5.1.10-bin is located, select it and click on open as shown.
- 5) Deploying and testing the web service
- a web service is deployed to a web container, the IDE allows testing the web service to see if it functions as expected.
- tester application provided by GlassFish, is integrated into the IDE for this purpose as it allows the developer to enter values and test them.
- e BookWS application, right click the BookWS project and select Deploy as shown in the diagram.
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 45
- Distributed Systems 2018-2019
- -deploy) and GlassFish v3 Domain
- tabs in the Output view.
- -click the BookWS Web
- service and select Test web service as shown in the diagram.
- GlassFish server as shown in the figure.
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 46
- Distributed Systems 2018-2019
- ∙ Enter the ISBN number as shown in the diagram.
- The book name and its cost are displayed as shown in the diagram.
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 47
- Distributed Systems 2018-2019
- 6) Consuming the web service
- the web service’s getBookDetails() method.
- i. Creating a web application
- -> New Project.
- application available under the projects section. Click Finish.
- New web application dialog box appears. Enter BookWSServletClient as the project name in the Project Name textbox and select the option Use Dedicated Folder for Storing Libraries.
- g box appears. Choose
- the default i.e. GlassFish v3 Domain as the web serevr, the Java EE 6 web as the Java EE version
- and the context path.
- ii. Adding the web service to the client application
- -click the BookWSServletClient project and select New -> Web Service Client as shown in the diagram.
- through the web service which needs to be consumed. Click ok. The name of the web service ap-pears in the New Web Service Client as shown in the diagram.
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 48
- Distributed Systems 2018-2019
- The Web Service Reference directory is added to the BookWSServletClient application as shown in the diagram. It displays the structure of the newly created client including the getBookDetails() method created earlier.
- iii. Creating a servlet
- -> Servlet.
- n-
- ter servlet in the package textbox.
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 49
- Distributed Systems 2018-2019
- Keep the defaults.
- eBookDetails.java is available with the default skeleton created by the NetBeans IDE which needs to be modified to hold the application logic.
- body of the processRequest() method.
- /*TODO output your page here*/
- out.println("<h1>Servlet retreiveBookDetails at " + request.getContextPath () + "</h1>"); With the code spec of the getBookDetails() operation of the web service by dragging and drop-ping the getBookDetails operation as shown in the diagram.
- change the following code spec:
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 50
- Distributed Systems 2018-2019
- java.lang.String isbn = “”;
- to
- java.lang.String isbn = request.getParameter(“isbn”);
- iv. Creating an HTML form
- the web service is added and the servlet is created, the form to accept ISBN from the user needs to be coded.
- n-
- dex.jsp file. Modify it to hold the following code spec.
- <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>SOAP Cleint - Get Book Details</title> </head>
- <body bgcolor="pink">
- <form name="frmgetBookDetails" method="post" action="retreiveBookDetails"> <h1>
- ISBN : <input type="text" name="isbn"/><br><br> </h1>
- <input type="submit" value="Submit"/>
- </form>
- </body>
- </html>
- v. Building the Web Application
- the Build menu item is clicked the details about the compilation and building of the BookWSServletClient Web application appears in the output – BookWSServletClient (dist) win-dow.
- vi. Running the Application
- web application is done run the application.
- and the BookWSServletCleint application is executed as shown in the diagram.
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 51
- Distributed Systems 2018-2019
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 52
- Distributed Systems 2018-2019
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 53
- Distributed Systems 2018-2019
- Practical No: 06
- Aim: Write a program to execute any one mutual exclusion algorithm.(Token Ring)
- Code:
- TokenClient1.java
- import java.io.*;
- import java.net.*;
- public class TokenClient1 {
- public static void main(String arg[]) throws Exception
- {
- InetAddress lclhost;
- BufferedReader br;
- String str="";
- TokenClient12 tkcl,tkser;
- //boolean hasToken;
- //boolean setSendData;
- while(true)
- {
- lclhost=InetAddress.getLocalHost();
- tkcl = new TokenClient12(lclhost);
- tkser = new TokenClient12(lclhost);
- //tkcl.setSendPort(9001);
- tkcl.setSendPort(9004);
- tkcl.setRecPort(8002);
- lclhost=InetAddress.getLocalHost();
- tkser.setSendPort(9000);
- if(tkcl.hasToken == true)
- {
- System.out.println("Do you want to enter the Data --> YES/NO");
- br=new BufferedReader(new InputStreamReader(System.in));
- str=br.readLine();
- if(str.equalsIgnoreCase("yes"))
- {
- System.out.println("ready to send");
- tkser.setSendData = true;
- tkser.sendData();
- tkser.setSendData = false;
- }
- else if(str.equalsIgnoreCase("no")) {
- System.out.println("i m in else");
- //tkcl.hasToken=false;
- tkcl.sendData();
- tkcl.recData();
- System.out.println("i m leaving else");
- }
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 54
- Distributed Systems 2018-2019
- }
- else {
- System.out.println("ENTERING RECEIVING MODE..."); tkcl.recData();
- } } } }
- class TokenClient12 {
- InetAddress lclhost;
- int sendport,recport;
- boolean hasToken = true;
- boolean setSendData = false;
- TokenClient12 tkcl,tkser;
- TokenClient12(InetAddress lclhost)
- this.lclhost = lclhost;
- }
- void setSendPort(int sendport) {
- this.sendport = sendport;
- } void setRecPort(int recport)
- this.recport = recport;
- }
- void sendData() throws Exception
- BufferedReader br;
- String str="Token";
- DatagramSocket ds;
- DatagramPacket dp;
- if(setSendData == true) {
- {
- {
- {
- System.out.println("sending ");
- System.out.println("Enter the Data");
- br=new BufferedReader(new InputStreamReader(System.in)); str = "ClientOne....." + br.readLine(); System.out.println("now sending");
- }
- ds = new DatagramSocket(sendport);
- dp = new DatagramPacket(str.getBytes(),str.length(),lclhost,sendport-1000);
- ds.send(dp);
- ds.close();
- setSendData = false;
- hasToken = false;
- }
- void recData()throws Exception
- {
- String msgstr;
- byte buffer[] = new byte[256];
- DatagramSocket ds;
- DatagramPacket dp;
- ds = new DatagramSocket(recport);
- dp = new DatagramPacket(buffer,buffer.length);
- ds.receive(dp);
- ds.close();
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 55
- Distributed Systems 2018-2019
- msgstr = new String(dp.getData(),0,dp.getLength()); System.out.println("The data is "+msgstr);
- if(msgstr.equals("Token")) {
- hasToken = true;
- } } }
- TokenClient2.java
- import java.io.*;
- import java.net.*;
- public class TokenClient2 {
- static boolean setSendData ;
- static boolean hasToken ;
- public static void main(String arg[]) throws Exception {
- InetAddress lclhost;
- BufferedReader br;
- String str1;
- TokenClient21 tkcl;
- TokenClient21 ser;
- while(true) {
- lclhost=InetAddress.getLocalHost();
- tkcl = new TokenClient21(lclhost);
- tkcl.setRecPort(8004);
- tkcl.setSendPort(9002);
- lclhost=InetAddress.getLocalHost();
- ser = new TokenClient21(lclhost);
- ser.setSendPort(9000);
- System.out.println("entering if");
- if(hasToken == true) {
- System.out.println("Do you want to enter the Data --> YES/NO");
- br=new BufferedReader(new InputStreamReader(System.in));
- str1=br.readLine();
- if(str1.equalsIgnoreCase("yes")) {
- System.out.println("ignorecase");
- ser.setSendData = true;
- ser.sendData(); }
- else if(str1.equalsIgnoreCase("no")) {
- tkcl.sendData();
- hasToken=false;
- }
- }
- else {
- System.out.println("entering recieving mode");
- tkcl.recData();
- hasToken=true;
- }
- }
- }
- M.Sc. IT PART-1
- Roll No: 18306A1035
- Page | 56
- Distributed Systems 2018-2019
- }
- class TokenClient21 {
- InetAddress lclhost;
- int sendport,recport;
- boolean setSendData = false;
- boolean hasToken = false;
- TokenClient21 tkcl;
- TokenClient21 ser;
- TokenClient21(InetAddress lclhost) {
- this.lclhost = lclhost; }
- void setSendPort(int sendport) {
- this.sendport = sendport;
- }
- void setRecPort(int recport) {
- this.recport = recport;
- }
- void sendData() throws Exception {
- System.out.println("case");
- BufferedReader br;
- String str="Token";
- DatagramSocket ds;
- DatagramPacket dp;
- if(setSendData == true) {
- System.out.println("Enter the Data");
- br=new BufferedReader(new InputStreamReader(System.in));
- str = "ClientTwo....." + br.readLine(); }
- ds = new DatagramSocket(sendport);
- dp = new DatagramPacket(str.getBytes(),str.length(),lclhost,sendport-1000);
- ds.send(dp);
- ds.close();
- System.out.println("Data Sent");
- setSendData = false;
- hasToken = false;
- }
- void recData()throws Exception {
- String msgstr;
- byte buffer[] = new byte[256];
- DatagramSocket ds;
- DatagramPacket dp;
- ds = new DatagramSocket(recport);
- //ds = new DatagramSocket(4000);
- dp = new DatagramPacket(buffer,buffer.length);
- ds.receive(dp);
- ds.close();
- msgstr = new String(dp.getData(),0,dp.getLength());
- System.out.println("The data is "+msgstr);
- if(msgstr.equals("Token"))
- {
- M.Sc. IT PART-1
- Roll No: 18306A1035
- Page | 57
- Distributed Systems 2018-2019
- hasToken = true;
- }
- }
- }
- TokenServer.java
- import java.net.*;
- public class TokenServer {
- public static void main(String agrs[])throws Exception
- while(true) {
- {
- Server sr=new Server();
- sr.recPort(8000);
- sr.recData();
- }
- }
- }
- class Server {
- boolean hasToken=false;
- boolean sendData=false;
- int recport;
- void recPort(int recport) {
- this.recport=recport;
- }
- void recData()throws Exception {
- byte buff[]=new byte[256];
- DatagramSocket ds;
- DatagramPacket dp;
- String str;
- ds=new DatagramSocket(recport);
- dp=new DatagramPacket(buff,buff.length);
- ds.receive(dp);
- ds.close();
- str=new String(dp.getData(),0,dp.getLength()); System.out.println("The message is "+str); }
- }
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 58
- Distributed Systems 2018-2019
- Output:
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 59
- Distributed Systems 2018-2019
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 60
- Distributed Systems 2018-2019
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 61
- Distributed Systems 2018-2019
- Practical No: 07
- Aim: Write a program to implement any one election algorithm.
- Code:
- Election.c
- #include<stdio.h>
- #include<conio.h>
- #include<process.h>
- struct proc
- {
- int live;
- int identifier;
- }
- process[10];
- int n,cordinator=1;
- /******************* DISPLAY PROCESSES **********************/
- void display()
- {
- int i;
- printf("\n PROCESSES ARE\n\n");
- printf("Processes ");
- for(i=1;i<=n;i++)
- {
- printf("P%d\t",i);
- }
- printf("\nlive ");
- for(i=1;i<=n;i++)
- {
- printf("%d\t",process[i].live);
- }
- printf("\nidentifier ");
- for(i=1;i<=n;i++)
- {
- printf("%d\t",process[i].identifier);
- }
- }
- /************ BULLY ALGORITHM ****************************/
- void bully()
- {
- int ch,c,id,i=0,cordinator,init,max=-99;
- cordinator=i;
- for(i=1;i<=n;i++)
- {
- if(process[cordinator].identifier<process[i].identifier&& process[i].live==1)
- cordinator=i;
- }
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 62
- Distributed Systems 2018-2019
- printf("\n\n CURRENT CO-ORDINATOR IS=P%d",cordinator);
- while(ch!=4)
- {
- printf("\n\n\n *** BULLY ALGORITHM ***");
- printf("\n1.Crash a Process\n2.Activate Process\n3.Display\n4.Exit");
- printf("\nENTER UR CHOICE");
- scanf("%d",&ch);
- switch(ch)
- {
- case 1:
- printf("\n Enter the process id to crash");
- scanf("%d",&id);
- if(process[id].live==0)
- {
- printf("\n Already crashed process");
- }
- else
- {
- process[id].live=0;
- printf("\n process P%d is crashed",id);
- if(id==cordinator)
- {
- while(1)
- {
- printf("\n Enter process id who intiates election");
- scanf("%d",&init);
- if(process[init].live==0)
- {
- printf("\n the selected process is crashed");
- }
- else
- {
- for(i=1;i<=n;i++)
- {
- if(i!=init&& process[i].identifier>process[init].identifier)
- printf("\n Election MSG sent from %d to %d",init,i);
- }
- for(i=1;i<=n;i++)
- {
- if(i!=init)
- {
- if(process[i].identifier>process[init].identifier&&process[i].live!=0)
- {
- printf("\n OK from %d to %d",i,init);
- }
- }
- }
- for(i=1;i<=n;i++)
- {
- if(max<process[i].identifier && process[i].live!=0)
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 63
- Distributed Systems 2018-2019
- {
- cordinator=i;
- max=process[i].identifier;
- }
- }
- printf("\n\n NEW CO-ORDINATOR IS=P%d",cordinator);
- break;
- }
- }
- }
- }
- break;
- case 2:
- printf("\n Enter process id to activate");
- scanf("%d",&id);
- if(process[id].live==1)
- {
- printf("\n Process %d is already active",id);
- }
- else
- {
- process[id].live=1;
- printf("\n Process %d activated",id);
- }
- if(process[id].identifier>process[cordinator].identifier)
- {
- cordinator=id;
- printf("\n NEW CO-ORDINATOR IS=P%d\n\n",id);
- }
- break;
- case 3:
- display();
- break;
- case 4:
- break;
- }
- }
- }
- /************ RING ALGORITHM ****************************/
- void ring()
- {
- int ch,c,id,i=0,init,max=-99,last;
- for(i=1;i<=n;i++)
- {
- if(process[cordinator].identifier<process[i].identifier&&process[i].live==1)
- cordinator=i;
- }
- printf("\n\n CURRENT CO-ORDINATOR IS=P%d",cordinator); while(ch!=4)
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 64
- Distributed Systems 2018-2019
- {
- printf("\n\n\n *** RING ALGORITHM ***");
- printf("\n1.Crash a Process\n2.Activate Process\n3.Display\n4.Exit");
- printf("\nENTER UR CHOICE");
- scanf("%d",&ch);
- switch(ch)
- {
- case 1:
- printf("\n Enter the process id to crash");
- scanf("%d",&id);
- if(process[id].live==0)
- {
- printf("\n Already crashed process");
- }
- else
- {
- process[id].live=0;
- printf("\n process P%d is crashed",id);
- if(id==cordinator)
- {
- while(1)
- {
- printf("\n Enter process id who intiates election");
- scanf("%d",&init);
- if(process[init].live==0)
- {
- printf("\n the selected process is crashed");
- }
- else
- {
- last=init;
- printf("\nElection MSG sent from =%d",last);
- for(i=init+1;i<=n;i++)
- {
- if(i!=init)
- printf(" ->%d",i);
- }
- for(i=1;i<init;i++)
- {
- if(i!=init)
- printf("->%d",i);
- last=i;
- }
- for(i=init+1;i<=n;i++)
- {
- if(max<process[i].identifier && process[i].live==1)
- {
- cordinator=i;
- max=process[i].identifier;
- }
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 65
- Distributed Systems 2018-2019
- }
- for(i=1;i<=init;i++)
- {
- if(max<process[i].identifier && process[i].live==1)
- {
- cordinator=i;
- max=process[i].identifier;
- }
- }
- printf("\n\n NEW COORDINATOR
- IS=P%d",cordinator);
- break;
- }
- }
- }
- }
- break;
- case 2:
- printf("\n Enter process id to activate");
- scanf("%d",&id);
- if(process[id].live==1)
- {
- printf("\n Process %d is already active",id);
- }
- else
- {
- process[id].live=1;
- printf("\n Process %d activated",id); if(process[id].identifier>process[cordinator].identifier) {
- printf("\n NEW CO-ORDINATOR
- IS=P%d\n\n",id);
- cordinator=id;
- }
- }
- break;
- case 3:
- display();
- break;
- case 4:
- break;
- }
- }
- }
- void main()
- {
- int ch,i,c;
- clrscr();
- printf("\n ENTER NO. OF PROCESSES");
- scanf("%d",&n);
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 66
- Distributed Systems 2018-2019
- for(i=1;i<=n;i++)
- {
- printf("\nEnter P%d process live or not(0/1)",i);
- scanf("%d",&process[i].live);
- printf("\nEnter P%d process identifier",i);
- scanf("%d",&process[i].identifier);
- }
- display();
- while(1)
- {
- printf("\n\n\n**** ELECTION ALGORITHM ****"); printf("\n1.BULLY ALGORITHM\n2.RING ALGORITHM\n3.EXIT"); printf("\n\n ENTER UR CHOICE"); scanf("%d",&ch);
- switch(ch)
- {
- case 1:
- bully();
- break;
- case 2:
- ring();
- break;
- case 3:
- exit(0);
- }
- }
- }
- Output:
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 67
- Distributed Systems 2018-2019
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 68
- Distributed Systems 2018-2019
- Practical No: 08
- Aim: Show the implementation of any one clock synchronization algorithm.
- Code:
- SCClient.java
- import java.io.*;
- import java.net.*;
- public class SCClient
- {
- public static void main(String args[])throws Exception
- {
- InetAddress lclhost;
- lclhost=InetAddress.getLocalHost();
- while(true)
- {
- Client cntl=new Client(lclhost);
- cntl.sendPort(9001);
- cntl.sendData();
- }}}
- class Client
- {
- InetAddress lclhost;
- int senport;
- Client(InetAddress lclhost)
- {
- this.lclhost=lclhost;
- }
- void sendPort(int senport)
- {
- this.senport=senport;
- }
- void sendData()throws Exception
- {
- DatagramPacket dp;
- DatagramSocket ds;
- BufferedReader br;
- br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the data"); String str=br.readLine();
- ds = new DatagramSocket(senport);
- dp = new DatagramPacket(str.getBytes(),str.length(),lclhost,senport-1000);
- ds.send(dp);
- ds.close();
- }}
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 69
- Distributed Systems 2018-2019
- SCServer.java
- import java.io.*;
- import java.net.*;
- import java.sql.*;
- public class SCServer
- {
- public static void main(String args[])throws Exception
- {
- InetAddress lclhost;
- lclhost=InetAddress.getLocalHost();
- long maxtime,skewtime,datatime;
- String maxtimestr,skewtimestr;
- BufferedReader br;
- ClntServer ser=new ClntServer(lclhost); System.out.println("Enter the maximum time");
- br = new BufferedReader(new InputStreamReader(System.in)); maxtimestr=br.readLine();
- System.out.println("Enter the maximum skew time");
- br = new BufferedReader(new InputStreamReader(System.in));
- skewtimestr=br.readLine();
- maxtime=Long.parseLong(maxtimestr);
- skewtime=Long.parseLong(skewtimestr);
- while(true)
- {
- datatime = System.currentTimeMillis();
- long G = datatime-maxtime-skewtime;
- System.out.println("G ="+G);
- ser.setTimeStamp(new Timestamp(G));
- ser.recPort(8001);
- ser.recData();
- }}}
- class ClntServer
- {
- InetAddress lclhost;
- int recport;
- Timestamp obtmp;
- ClntServer(InetAddress lclhost)
- {
- this.lclhost = lclhost;
- }
- void recPort(int recport)
- {
- this.recport = recport;
- }
- void setTimeStamp(Timestamp obtmp)
- {
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 70
- Distributed Systems 2018-2019
- this.obtmp = obtmp;
- }
- void recData()throws Exception
- {
- String msgstr="";
- DatagramSocket ds;
- DatagramPacket dp;
- BufferedReader br;
- byte buf[] = new byte[256];
- ds = new DatagramSocket(recport);
- dp = new DatagramPacket(buf,buf.length);
- ds.receive(dp);
- ds.close();
- msgstr = new String(dp.getData(),0,dp.getLength()); System.out.println(msgstr);
- Timestamp obtmp = new Timestamp(Long.parseLong(msgstr)); if(this.obtmp.before(obtmp) == true) {
- System.out.println("The Message is accepted");
- }
- else
- {
- System.out.println("The Message is rejected");
- }}}
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 71
- Distributed Systems 2018-2019
- Output:
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 72
- Distributed Systems 2018-2019
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- ________________________________________________________
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 73
- Distributed Systems 2018-2019
- Practical No: 9
- Aim: Implement the concept of distributed file system architecture.
- Network File System:
- Goto root mode.
- $ su
- password – fedora
- Install nfs-utils
- #yum install nfs-utils
- Specify what we want to share
- #/etc/exports
- Start the Services
- #systemctl start rpcbind.service
- #systemctl start nfs-lock.service
- #systemctl start nfs-service.service
- #systemctl start nfs-idmap.service
- #systemctl start mountd.service
- Enable the Services
- #systemctl enable rpcbind.service
- #systemctl enable nfs-lock.service
- #systemctl enable nfs-service.service
- #systemctl enable nfs-idmap.service
- #systemctl enable mountd.service
- Type the message
- cat>>vaibhav
- Hello World!
- Cat>>/nfstest/vaibhav
- Hello Universe!
- mkdir vsit
- mount –t nfs 127.0.0.1:/nfstest vsit
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 74
- Distributed Systems 2018-2019
- Output:
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 75
- Distributed Systems 2018-2019
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 76
- Distributed Systems 2018-2019
- M.Sc. IT PART-1 Roll No: 18306A1035 Page | 77
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement