Lamport


import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class common extends Panel implements Runnable
{
public Label l1,l2,l3,l4,l5;
public TextField t1,t2,t3;
public Button b1;
public int tick;
public java.awt.List list1;
public Thread th;
boolean flag;

public common(String str)
{
flag=true;
th=new Thread(this);
l1=new Label("time");
l2=new Label("message");
l3=new Label("to:");
l4=new Label("received");
l5=new Label(str);

t1=new TextField("0");
t2=new TextField();
t3=new TextField();

b1=new Button ("send");

list1=new java.awt.List();
this.setVisible(true);
this.setLayout(null);

l5.setBounds(100,15,100,30);
l1.setBounds(50,70,100,20);
l2.setBounds(50,120,100,20);
l3.setBounds(50,180,100,20);
l4.setBounds(50,240,100,20);

t1.setBounds(150,70,100,20);
t2.setBounds(150,120,100,20);
t3.setBounds(150,240,100,20);

list1.setBounds(150,180,100,40);

b1.setBounds(150,350,100,20);

t1.setEditable(false);

this.add(l5);
this.add(l1);
this.add(t1);
this.add(l2);
this.add(t2);
this.add(l3);
this.add(list1);
this.add(l4);
this.add(t3);
this.add(b1);
this.setSize(300,400);
this.setBackground(Color.GRAY);
}

public void run()
{
try{
while(flag)
{
t1.setText((Integer.parseInt(t1.getText()))+this.tick+"");
Thread.sleep(500);
}

}catch(Exception e) {;}

}

}
public class lamport implements ActionListener
{
Frame f;
common c1,c2,c3;
lamport()
{
f=new Frame(" lamport ");
c1=new common("Process: A");
c2=new common("Process: B");
c3=new common("Process: C");

f.setLayout(null);

c1.setBounds(30,100,300,400);
c2.setBounds(350,100,300,400);
c3.setBounds(670,100,300,400);

f.add(c1);
f.add(c2);
f.add(c3);

c1.b1.addActionListener(this);
c2.b1.addActionListener(this);
c3.b1.addActionListener(this);

c1.list1.add("Process B");
c1.list1.add("Process c");


c2.list1.add("Process A");
c2.list1.add("Process C");

c3.list1.add("Process A");
c3.list1.add("Process B");

c1.tick=6;
c2.tick=8;
c3.tick=10;

f.setVisible(true);
f.setSize(1000,600);
f.setBackground(Color.BLACK);

c1.th.start();
c2.th.start();
c3.th.start();

       }

     public static void main(String arg[])
{
lamport lp=new lamport();
}

public void actionPerformed(ActionEvent ae)
{
String temp1,temp2,temp3;
if(ae.getActionCommand().equals("send"))
{
if((""+ae.getSource()).substring(16,23).equals("button0"))
{
temp1=c1.t1.getText();
temp2=c1.t2.getText();

if(c1.list1.getSelectedItem().equals("Process B"))
{
temp3=c2.t1.getText();
c2.t3.setText(temp2+","+temp1);
}
      else if(c1.list1.getSelectedItem().equals("Process C"))
{
temp3=c3.t2.getText();
c3.t3.setText(temp2+","+temp1);
}
}

if((""+ae.getSource()).substring(16,23).equals("button1"))
{
temp1=c2.t1.getText();
temp2=c2.t2.getText();

if(c2.list1.getSelectedItem().equals("Process A"))
{
temp3=c1.t1.getText();

if(Integer.parseInt(temp1)>Integer.parseInt(temp3))
c1.t1.setText(temp1);
c1.t3.setText(temp2+","+temp1);
}

else if(c2.list1.getSelectedItem().equals("Process C"))
{

temp3=c3.t1.getText();

c3.t3.setText(temp2+","+temp1);
}

}

if((""+ae.getSource()).substring(16,23).equals("button2"))
{
temp1=c3.t1.getText();
temp2=c3.t2.getText();



if(c3.list1.getSelectedItem().equals("Process B"))
{
temp3=c2.t1.getText();

if(Integer.parseInt(temp1)>Integer.parseInt(temp3))
c2.t1.setText(temp1);
c2.t3.setText(temp2+","+temp1);
}

else if(c3.list1.getSelectedItem().equals("Process A"))
{

temp3=c1.t1.getText();

if(Integer.parseInt(temp1)>Integer.parseInt(temp3))
c1.t1.setText(temp1);
c1.t3.setText(temp2+","+temp1);
}
}
}
   }
}


Leave a Reply