Skip to content

Instantly share code, notes, and snippets.

@SebastianOpiyo
Created July 8, 2022 15:36
Show Gist options
  • Save SebastianOpiyo/a484ba1cc2ba7e0226d2506ed702bf41 to your computer and use it in GitHub Desktop.
Save SebastianOpiyo/a484ba1cc2ba7e0226d2506ed702bf41 to your computer and use it in GitHub Desktop.

/*

Coordinator Cohorts QUERY TO COMMIT --------------------------------> VOTE YES/NO prepare/abort <------------------------------- commit/abort COMMIT/ROLLBACK --------------------------------> ACKNOWLEDGMENT commit/abort <--------------------------------
end

Two Phases :

1.Prepare and Vote Phase 2. Commit or Abort Phase

"Either All Commit Or All RollBack."

*/

// Two Phase Commit --> Client
//Two Phase Commit Protocol CLIENT
package twophase;
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=1111;
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);
}
}
}
@SuppressWarnings("deprecation")
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);
}
}
} //end client
// Two Phase Commit --> Server
package twophase;
import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
boolean closed = false, inputFromAll = false;
List<ClientThread> thread;
List<String> data;
Server() {
thread = new ArrayList<ClientThread>();
data = new ArrayList<String>();
}
public static void main(String args[])
{
Socket clientSocket = null;
ServerSocket serverSocket = null;
int port_number = 1111;
Server server = new Server();
try
{
serverSocket = new ServerSocket(port_number);
} catch (IOException e) {
System.out.println(e);
}
while (!server.closed)
{
try {
clientSocket = serverSocket.accept();
ClientThread clientThread = new ClientThread(server, clientSocket);
(server.thread).add(clientThread);
System.out.println("\nNow Total clients are : " + (server.thread).size());
(server.data).add("NOT_SENT");
clientThread.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 server;
public ClientThread(Server server, Socket clientSocket)
{
this.clientSocket = clientSocket;
this.server = server;
}
@SuppressWarnings("deprecation")
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 < (server.thread).size(); i++)
{
if ((server.thread).get(i) != this)
{
((server.thread).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 < (server.thread).size(); i++) {
((server.thread).get(i)).os.println("GLOBAL_ABORT");
((server.thread).get(i)).os.close();
((server.thread).get(i)).is.close();
}
break;
}
if (line.equalsIgnoreCase("COMMIT"))
{
System.out.println("\nFrom '" + clientIdentity + "' : COMMIT");
if ((server.thread).contains(this))
{
(server.data).set((server.thread).indexOf(this), "COMMIT");
for (int j = 0; j < (server.data).size(); j++)
{
if (!(((server.data).get(j)).equalsIgnoreCase("NOT_SENT")))
{
server.inputFromAll = true;
continue;
}
else{
server.inputFromAll = false;
System.out.println("\nWaiting for inputs from other clients.");
break;
}
}
if (server.inputFromAll)
{
System.out.println("\n\nCommited....");
for (int i = 0; i < (server.thread).size(); i++)
{
((server.thread).get(i)).os.println("GLOBAL_COMMIT");
((server.thread).get(i)).os.close();
((server.thread).get(i)).is.close();
}
break;
}
} // if thread.contains
} // commit
} // while
server.closed = true;
clientSocket.close();
} catch (IOException e) { }
}
}// end class thread
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment