Ring Token Algorithm Implementation
Introduction These project is divided into two parts namely Server and Client. I refer client as NODE. As this is first prototype I have hard-wired the server side code. Lets dive into to java coading [Link]
package server; /** * @owner jalpAn * */ import [Link]; import [Link]; import [Link].*; import [Link].*; import [Link].*; import [Link]; import [Link]; import [Link]; import [Link]; public class ChatServer extends JFrame { public static final int PORT = 3990; ArrayList<HandleClient> clients = new ArrayList<HandleClient>(); public void process() throws Exception { ServerSocket server = new ServerSocket(PORT); [Link]("server is online"); while (true) { Socket client = [Link](); HandleClient c = new HandleClient(client); [Link](c); } } public static void main(String[] args) throws Exception { ChatServer c = new ChatServer(); [Link](); [Link](); } JFrame main; JTextArea iTxt;
Code is copyrighted by jalpAn randeRi
JTextArea oTxt; JLabel chek1 = new JLabel("Down"); JLabel chek2 = new JLabel("Down"); JLabel chek3 = new JLabel("Down"); JLabel chek4 = new JLabel("Down"); JLabel chek5 = new JLabel("Down"); public void buildInterface() { main = new JFrame("Server Dashboard"); [Link](true); BorderLayout borderManager = new BorderLayout(2, 2); [Link](borderManager); JPanel headerPanel = new JPanel(); JLabel label = new JLabel("Network Status"); [Link](label); [Link](headerPanel, [Link]); GridLayout gridManger = new GridLayout(5, 2); JPanel contents = new JPanel(); [Link](gridManger); JLabel node1 = new JLabel("Node 1"); JLabel node2 = new JLabel("Node 2"); JLabel node3 = new JLabel("Node 3"); JLabel node4 = new JLabel("Node 4"); JLabel node5 = new JLabel("Node 5"); [Link](node1); [Link](chek1); [Link](node2); [Link](chek2); [Link](node3); [Link](chek3); [Link](node4); [Link](chek4); [Link](node5); [Link](chek5); [Link](contents, [Link]); [Link](200, 200); [Link](true); [Link](EXIT_ON_CLOSE); pack(); }
String msg; protected class HandleClient implements Runnable {
Code is copyrighted by jalpAn randeRi
String name = ""; BufferedReader input; PrintWriter output; public HandleClient(Socket client) throws Exception { input = new BufferedReader(new InputStreamReader([Link]())); output = new PrintWriter([Link](), true); name = [Link](); Thread t = new Thread(this); [Link](); }
public String getUserName() { return name; } public void run() { [Link]("thread started"); String line; try { while (true) { line = [Link]();
switch (line) { case "Node1U":[Link]("Up");break; case "Node1D":[Link]("Down");break; case "Node1L":[Link]("Leader"); notifyLeader(1);break; case "Node2U":[Link]("Up");break; case "Node2D":[Link]("Down");break; case "Node2L":[Link]("Leader");notifyLeader(2); break; case "Node3U":[Link]("Up");break; case "Node3D":[Link]("Down");break; case "Node3L":[Link]("Leader"); notifyLeader(3); break; case "Node4U":[Link]("Up");break; case "Node4D":[Link]("Down");break; case "Node4L":[Link]("Leader"); notifyLeader(4); break; case "Node5U":[Link]("Up");break; case "Node5D":[Link]("Down");break; case "Node5L":[Link]("Leader"); notifyLeader(5); break; case "Nxt1":sendMsg("Node1",getNextNode("Node1")); break; case "Nxt2":sendMsg("Node2",getNextNode("Node2")); break; case "Nxt3":sendMsg("Node3",getNextNode("Node3")); break; case "Nxt4":sendMsg("Node4",getNextNode("Node4")); break; case "Nxt5":sendMsg("Node5",getNextNode("Node5")); break; default:
Code is copyrighted by jalpAn randeRi
if([Link](":")){ [Link]("in if"); String[] temp=[Link](":"); String msg=temp[0]; String node=temp[1]; [Link]("Destination node is "+node+" msg is "+msg); sendMsg(node, msg); }else{ [Link](line); } break; }
} } catch (Exception ex) { [Link]([Link]()); } } HandleClient c; public String getNextNode(String CurrentNode){ String nextNode=""; switch(CurrentNode){ case "Node1":if([Link]().equalsIgnoreCase("Up")){ nextNode="Node2"; }else if([Link]().equalsIgnoreCase("Up")){ nextNode="Node3"; }else if([Link]().equalsIgnoreCase("Up")){ nextNode="Node4"; }else if([Link]().equalsIgnoreCase("Up")){ nextNode="Node5"; } break; case "Node2":if([Link]().equalsIgnoreCase("Up")){ nextNode="Node3"; }else if([Link]().equalsIgnoreCase("Up")){ nextNode="Node4"; }else if([Link]().equalsIgnoreCase("Up")){ nextNode="Node5"; }else if([Link]().equalsIgnoreCase("Up")){ nextNode="Node1"; } break;
Code is copyrighted by jalpAn randeRi
case "Node3":if([Link]().equalsIgnoreCase("Up")){ nextNode="Node4"; }else if([Link]().equalsIgnoreCase("Up")){ nextNode="Node5"; }else if([Link]().equalsIgnoreCase("Up")){ nextNode="Node1"; }else if([Link]().equalsIgnoreCase("Up")){ nextNode="Node2"; } break; case "Node4":if([Link]().equalsIgnoreCase("Up")){ nextNode="Node5"; }else if([Link]().equalsIgnoreCase("Up")){ nextNode="Node1"; }else if([Link]().equalsIgnoreCase("Up")){ nextNode="Node2"; }else if([Link]().equalsIgnoreCase("Up")){ nextNode="Node3"; } break; case "Node5":if([Link]().equalsIgnoreCase("Up")){ nextNode="Node1"; }else if([Link]().equalsIgnoreCase("Up")){ nextNode="Node2"; }else if([Link]().equalsIgnoreCase("Up")){ nextNode="Node3"; }else if([Link]().equalsIgnoreCase("Up")){ nextNode="Node4"; } break; }
return nextNode ; } private void sendMsg(String CurrentNode, String nextNode) { [Link]("Current is "+CurrentNode+" nextNode is "+nextNode); HandleClient temp; for(HandleClient c1: clients){ if([Link]().equals(CurrentNode)){ temp=c1; [Link](nextNode); [Link]("OutMSG to c1 is "+[Link]()+" msg "+nextNode); return; } } }
Code is copyrighted by jalpAn randeRi
private void notifyLeader(int i) { [Link]("in notify"); switch(i){ case 1: if([Link]().equalsIgnoreCase("Leader")){ sendMsg("Node1","leader is 1"); } if([Link]().equalsIgnoreCase("UP")){ sendMsg("Node2","leader is 1"); } if([Link]().equalsIgnoreCase("UP")){ sendMsg("Node3","leader is 1"); } if([Link]().equalsIgnoreCase("UP")){ sendMsg("Node4","leader is 1"); } if([Link]().equalsIgnoreCase("UP")){ sendMsg("Node5","leader is 1"); } break; case 2: if([Link]().equalsIgnoreCase("UP")){ sendMsg("Node1","leader is 2"); } if([Link]().equalsIgnoreCase("Leader")){ sendMsg("Node2","leader is 2"); } if([Link]().equalsIgnoreCase("UP")){ sendMsg("Node3","leader is 2"); } if([Link]().equalsIgnoreCase("UP")){ sendMsg("Node4","leader is 2"); } if([Link]().equalsIgnoreCase("UP")){ sendMsg("Node5","leader is 2"); } break; case 3: if([Link]().equalsIgnoreCase("UP")){ sendMsg("Node1","leader is 3"); } if([Link]().equalsIgnoreCase("UP")){ sendMsg("Node2","leader is 3"); } if([Link]().equalsIgnoreCase("Leader")){ sendMsg("Node3","leader is 3"); } if([Link]().equalsIgnoreCase("UP")){ sendMsg("Node4","leader is 3"); }
Code is copyrighted by jalpAn randeRi
if([Link]().equalsIgnoreCase("UP")){ sendMsg("Node5","leader is 3"); } break; case 4: if([Link]().equalsIgnoreCase("UP")){ sendMsg("Node1","leader is 4"); } if([Link]().equalsIgnoreCase("UP")){ sendMsg("Node2","leader is 4"); } if([Link]().equalsIgnoreCase("UP")){ sendMsg("Node3","leader is 4"); } if([Link]().equalsIgnoreCase("Leader")){ sendMsg("Node4","leader is 4"); } if([Link]().equalsIgnoreCase("UP")){ sendMsg("Node5","leader is 4"); } break; case 5: if([Link]().equalsIgnoreCase("UP")){ sendMsg("Node1","leader is 5"); } if([Link]().equalsIgnoreCase("UP")){ sendMsg("Node2","leader is 5"); } if([Link]().equalsIgnoreCase("UP")){ sendMsg("Node3","leader is 5"); } if([Link]().equalsIgnoreCase("UP")){ sendMsg("Node4","leader is 5"); } if([Link]().equalsIgnoreCase("Leader")){ sendMsg("Node5","leader is 5"); } break; } } } }
Lets dive into NODE [Link]
Code is copyrighted by jalpAn randeRi
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package ringcorordinataralgo; import [Link]; import [Link]; import [Link]; /** * * @author jalpAn */ public class Node1 { public static void main(String[] args){ Node1 n=new Node1(); try { [Link](); } catch (Exception ex) { [Link]([Link]()).log([Link], null, ex); } } private void start() { try { ChatClient c=new ChatClient("Node1", "localhost", 1); [Link](); } catch (Exception ex) { [Link]([Link]()).log([Link], null, ex); } } }
As you can see here I have used the [Link] class. Lets examine this class [Link] package client; import [Link].*; import [Link].*; import [Link].*; import [Link].*; import [Link].*; public class ChatClient extends JFrame { int NO;
Code is copyrighted by jalpAn randeRi
String uname; PrintWriter pw; BufferedReader br; JTextArea incomingMsgsTxt; JTextField usrIpTxt; JButton btnSend, btnLogoff, btnLogin; Socket client; public ChatClient(String uname, String servername, int i) throws Exception { super(uname); [Link] = uname; [Link] = i; client = new Socket(servername, 3990); br = new BufferedReader(new InputStreamReader([Link]())); pw = new PrintWriter([Link](), true); [Link](uname); MessagesThread m = new MessagesThread(); Thread t = new Thread(m); [Link](); } JButton connect = new JButton("Connect"); public void buildInterface() { btnSend = new JButton("Election"); [Link](new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String cmd = [Link](); switch (cmd) { case "Connect": [Link]("Node" + NO + "U"); [Link]("Disconnect"); break; case "Disconnect": [Link]("Node" + NO + "D"); [Link]("Connect"); break; }
} }); incomingMsgsTxt = new JTextArea(); [Link](10); [Link](10);
Code is copyrighted by jalpAn randeRi
[Link](false); JScrollPane sp = new JScrollPane(incomingMsgsTxt, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); add(sp, "Center"); JPanel bp = new JPanel(new FlowLayout()); [Link](btnSend); [Link](connect); add(bp, "South"); [Link](new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // starting election set the flag true Init_flag=true; //ask for next node sendMsg("Nxt" + NO); //recving next node informatoin
} });
setSize(250, 200); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); setAlwaysOnTop(true); pack(); } public void sendMsg(String test) { [Link](test); } public void sendMsg(String msg, String node) { [Link](node + ":" + msg); } public static void main(String[] args) { try { ChatClient c = new ChatClient("Node", "localhost", 1); [Link](); } catch (Exception ex) { [Link]("Exception " + [Link]()); } } String line;
Code is copyrighted by jalpAn randeRi
String Msg="{"; boolean Init_flag=false; String saveMsg =""; public void reciveMsg() { try { while (true) { line = [Link](); [Link]("recvied > "+line); [Link](line + "\n"); if([Link]("Node")){ //recived next Node location if(Init_flag){ Msg="{"+NO+","; sendMsg(line,Msg); }else{ sendMsg(line,saveMsg); } }else if([Link]("{")){ // msg recivied form other node as a part of election if([Link](NO+"")){//determine who is leader determineLeader(line); }else{ //append own NO in the msg by saving it to temp saveMsg=line+NO+","; sendMsg("Nxt"+NO); } } } } catch (Exception ex) { } } private void determineLeader(String line) { [Link]("Determine Leader "+line); String[] temp=[Link](","); int[] nodes=new int[[Link]]; int i=0; for(String s: temp){ if([Link]("{")){ nodes[i]=[Link]([Link](1)); }else{ nodes[i]=[Link](s); } i++; } int leader=nodes[0];
Code is copyrighted by jalpAn randeRi
for(i=0;i<[Link];i++){ if(nodes[i]>leader){ leader=nodes[i]; } } [Link]("leader is "+leader); sendMsg("Node"+leader+"L");
} class MessagesThread implements Runnable { @Override public void run() { [Link]("thread is client "); reciveMsg(); } } }
//
HERE GOES THE OUTPUT
Initial State
Connect State
Code is copyrighted by jalpAn randeRi
Output State
Code is copyrighted by jalpAn randeRi