import java.util.Random; import java.lang.Math; import java.awt.*; import java.net.*; import java.io.*; import java.applet.Applet; public class FlowControl extends Applet implements Runnable { int height=0; Thread thread=null; boolean connected=false; // ConnectionHandler ch; Graphics bufGr; Image im; float colR=(float)0; float colG=(float)0; float colB=(float)10; // Communications Socket soq = null; Socket sock = null; DataInputStream input = null; DataOutputStream output = null; int porth=5000; int port; String hostname="rice"; char DISC='d'; char NEG='n'; //control double a=Math.random(); RFs rfs=new RFs(); int nrf=1; //default RF is 1 public void init() { setLayout(new BorderLayout()); Panel controlPanel = new Panel(); TitleLabel title=new TitleLabel(); add("South", controlPanel); add("North", title); controlPanel.setLayout(new FlowLayout()); controlPanel.add(new Button("Connect")); controlPanel.add(new Button("Disconnect")); Choice c = new Choice(); c.addItem("Reaction Function 1"); c.addItem("Reaction Function 2"); c.addItem("Reaction Function 3"); c.addItem("Reaction Function 4"); controlPanel.add(c); this.resize(400,120); } /** * Start the applet by forking a connection thread. */ public void start() { if (thread == null) { thread = new Thread(this); thread.start(); } // initialize double buffering stuff for flicker-free display try { im = createImage(600,500); bufGr = im.getGraphics(); } catch (Exception e) { bufGr = null; } } /** * Stop the applet. The thread will exit because thread is set to null. */ public void stop() { if (thread != null) { thread.stop(); thread = null; } } public void mypaint(){ //height=(int) (200*Math.random()); bufGr.setColor(new Color(colR,colG,colB)); bufGr.clearRect(0,30,500,40); bufGr.fillRect(0,30,height,40); repaint(); } public void paint(Graphics g) { g.drawImage(im,0,0,this); } public void update(Graphics g) { paint(g); } /* * * redefinition of the event method which is part of the Component class * */ public boolean action(Event evt, Object arg) { //Thread.currentThread().setPriority(Thread.NORM_PRIORITY-1); if (evt.target instanceof Choice) { String st=((String)arg); int stl=st.length(); switch(st.charAt(stl-1)) { case '1': nrf=1; break; case '2': nrf=2; break; case '3': nrf=3; break; case '4': nrf=4; break; default: System.out.println("Unknown reaction function\n"); break; } System.out.println("You have selected reaction function: "+((String)arg)+"\n"); } if ("Connect".equals(arg)) { if (!connected) { // System.out.println("Connecting ...\n"); connect(); } else { System.out.println("You are already connected\n"); } } if ("Disconnect".equals(arg)) { connected=false; } return true; } public void connect() { System.out.print("Connecting to " + hostname + "..."); System.out.flush(); try { soq = new Socket(hostname, porth); DataInputStream in = new DataInputStream(soq.getInputStream()); port=in.readInt(); soq.close(); sock = new Socket(hostname, port); input = new DataInputStream(sock.getInputStream()); output = new DataOutputStream(sock.getOutputStream()); connected = true; } catch (IOException sex) { System.out.print("IOException\n"); connected = false; } System.out.println("Connection okay."); } public void run() { thread.setPriority(Thread.MIN_PRIORITY+1); while (thread!=null) { try { thread.sleep(100); } catch (InterruptedException e) { System.out.print("InterruptedException\n"); } if(connected) { try { output.writeChar(NEG); int avCap=input.readInt(); //rnd=Math.random(); //int demand=(int) ((height+avCap)*rnd); //int demand=(int) (a*(height+avCap)); // System.out.println("height="+height+"\n"); // System.out.println("avCap="+avCap+"\n"); int demand=rfs.rfs[nrf][height+avCap]; int demandInc=demand-height; output.writeInt(demandInc); height=demand; //System.out.println(new Integer(height).toString()+"\n"); mypaint(); } catch (IOException e) { break; } } if ((sock!=null)&&(!connected)) { try { output.writeChar(DISC); output.writeInt(-height); input.close(); output.close(); sock.close(); sock=null; } catch (IOException e) { System.out.println("Error"+e+"\n"); } System.out.println("You are disconnected\n"); height=0; mypaint(); } } } } class RFs { public int rfs[][]=new int[6][501]; int cap=500; public RFs() { //linear double a=Math.random(); for(int avCap=0;avCaprfs[4][avCap+1]) System.out.println("RF is not monotone"); } double dcapsq=(double) Math.sqrt((double) cap); for(int avCap=0;avCap