There three Distributed Commit Protocols :
1. Create a class name Server.java and paste the below code:
2. Create a class name Client.java and paste the below code:
CONSOLE OUTPUTS:
- One-phase Commit
- Two-phase Commit
- Three-phase Commit
- Eclipse IDE
- First run the Server.java
- Then run multiple times Client.java
- To see output Go to Console -> Client of Small Computer icon (Display Selected Console)
1. Create a class name Server.java and paste the below code:
//Professional Cipher (www.professionalcipher.com)
import java.io.*;
import java.net.*;
import java.util.*;
public class Server
{
boolean closed=false,inputFromAll=false;
List<clientThread> t;
List<String> data;
Server()
{
t= new ArrayList<clientThread>();
data= new ArrayList<String>();
}
public static void main(String args[])
{
Socket clientSocket = null;
ServerSocket serverSocket = null;
int port_number=8989;
Server ser=new Server();
try {
serverSocket = new ServerSocket(port_number);
}
catch (IOException e)
{System.out.println(e);}
while(!ser.closed)
{
try {
clientSocket = serverSocket.accept();
clientThread th=new clientThread(ser,clientSocket);
(ser.t).add(th);
System.out.println("\nNow Total clients are : "+(ser.t).size());
(ser.data).add("NOT_SENT");
th.start();
}
catch (IOException e) {}
}
try
{
serverSocket.close();
}catch(Exception e1){}
}
}
class clientThread extends Thread
{
DataInputStream is = null;
String line;
String destClient="";
String name;
PrintStream os = null;
Socket clientSocket = null;
String clientIdentity;
Server ser;
public clientThread(Server ser,Socket clientSocket)
{
this.clientSocket=clientSocket;
this.ser=ser;
}
public void run()
{
try
{
is = new DataInputStream(clientSocket.getInputStream());
os = new PrintStream(clientSocket.getOutputStream());
os.println("Enter your name.");
name = is.readLine();
clientIdentity=name;
os.println("Welcome "+name+" to this 2 Phase Application.\nYou will receive a vote Request now...");
os.println("VOTE_REQUEST\nPlease enter COMMIT or ABORT to proceed : ");
for(int i=0; i<(ser.t).size(); i++)
{
if((ser.t).get(i)!=this)
{
((ser.t).get(i)).os.println("---A new user "+name+" entered the Appilcation---");
}
}
while (true)
{
line = is.readLine();
if(line.equalsIgnoreCase("ABORT"))
{
System.out.println("\nFrom '"+clientIdentity+"' : ABORT\n\nSince aborted we will not wait for inputs from other clients.");
System.out.println("\nAborted....");
for(int i=0; i<(ser.t).size(); i++)
{
((ser.t).get(i)).os.println("GLOBAL_ABORT");
((ser.t).get(i)).os.close();
((ser.t).get(i)).is.close();
}
break;
}
if(line.equalsIgnoreCase("COMMIT"))
{
System.out.println("\nFrom '"+clientIdentity+"' : COMMIT");
if((ser.t).contains(this))
{
(ser.data).set((ser.t).indexOf(this), "COMMIT");
for(int j=0;j<(ser.data).size();j++)
{
if(!(((ser.data).get(j)).equalsIgnoreCase("NOT_SENT")))
{
ser.inputFromAll=true;
continue;
}
else
{
ser.inputFromAll=false;
System.out.println("\nWaiting for inputs from other clients.");
break;
}
}
if(ser.inputFromAll)
{
System.out.println("\n\nCommited....");
for(int i=0; i<(ser.t).size(); i++)
{
((ser.t).get(i)).os.println("GLOBAL_COMMIT");
((ser.t).get(i)).os.close();
((ser.t).get(i)).is.close();
}
break;
}
}//if t.contains
}//commit
}//while
ser.closed=true;
clientSocket.close();
} catch(IOException e){};
}
}
2. Create a class name Client.java and paste the below code:
//Professional Cipher (www.professionalcipher.com)
import java.io.*;
import java.net.*;
public class Client implements Runnable{
static Socket clientSocket = null;
static PrintStream os = null;
static DataInputStream is = null;
static BufferedReader inputLine = null;
static boolean closed = false;
public static void main(String[] args)
{
int port_number=8989;
String host="localhost";
try {
clientSocket = new Socket(host, port_number);
inputLine = new BufferedReader(new InputStreamReader(System.in));
os = new PrintStream(clientSocket.getOutputStream());
is = new DataInputStream(clientSocket.getInputStream());
} catch (Exception e) {
System.out.println("Exception occurred : "+e.getMessage());
}
if (clientSocket != null && os != null && is != null)
{
try
{
new Thread(new Client()).start();
while (!closed)
{
os.println(inputLine.readLine());
}
os.close();
is.close();
clientSocket.close();
} catch (IOException e)
{
System.err.println("IOException: " + e);
}
}
}
public void run()
{
String responseLine;
try
{
while ((responseLine = is.readLine()) != null)
{
System.out.println("\n"+responseLine);
if (responseLine.equalsIgnoreCase("GLOBAL_COMMIT")==true || responseLine.equalsIgnoreCase("GLOBAL_ABORT")==true )
{
break;
}
}
closed=true;
} catch (IOException e)
{
System.err.println("IOException: " + e);
}
}
}
CONSOLE OUTPUTS:
*************SERVER************ Now Total clients are : 1 Now Total clients are : 2 Now Total clients are : 3 From 'Client 1' : COMMIT Waiting for inputs from other clients. From 'Client 2' : COMMIT Waiting for inputs from other clients. From 'Client 3' : ABORT Since aborted we will not wait for inputs from other clients. Aborted....
************CLIENT 1************ Enter your name. Client 1 Welcome Client 1 to this 2 Phase Application. You will receive a vote Request now... VOTE_REQUEST Please enter COMMIT or ABORT to proceed : ---A new user Client 2 entered the Appilcation--- ---A new user Client 3 entered the Appilcation--- COMMIT GLOBAL_ABORT
************CLIENT 2************ Enter your name. Client 2 Welcome Client 2 to this 2 Phase Application. You will receive a vote Request now... VOTE_REQUEST Please enter COMMIT or ABORT to proceed : ---A new user Client 3 entered the Appilcation--- COMMIT GLOBAL_ABORT
************CLIENT 3************ Enter your name. Client 3 Welcome Client 3 to this 2 Phase Application. You will receive a vote Request now... VOTE_REQUEST Please enter COMMIT or ABORT to proceed : ABORT
Comments
Post a Comment