import java.awt.*; import corejava.*; import java.net.*; import java.io.*; import java.util.*; public class Survey extends Frame { public Survey(String[] args) { if (args.length < 1 || args.length > 2) { System.out.println("If you want to start a server process type 'java Survey {time}'.\n"); System.out.println("To start a client process type 'java Survey {host} {port}'.\n"); System.exit(0); } if (args.length == 1) { //Create Server Interface setTitle("Server Survey"); timer = Integer.parseInt(args[0]); display.setEditable(true); add("North", display); Panel p = new Panel(); p.setLayout(new GridLayout(4,2)); p.add(new Label("# of clients connected")); num_clients = new TextField("",3); num_clients.setEditable(false); p.add(num_clients); p.add(new Label("# of min left")); time_left = new TextField(Integer.toString(timer),3); time_left.setEditable(false); p.add(time_left); p.add(new Label("# of yes responces")); yeses = new TextField("",3); yeses.setEditable(false); p.add(yeses); p.add(new Label("# of no responces")); nos = new TextField("",3); nos.setEditable(false); p.add(nos); add("Center", p); clock = new ClockCanvas(this, "Time Left", 0, 0, timer); add("West", clock); Panel q = new Panel(); q.setLayout(new FlowLayout(FlowLayout.CENTER)); server_send_button = new Button("Send"); server_send_button.setBackground(Color.green); q.add(server_send_button); quit_button = new Button("Quit"); quit_button.setBackground(Color.red); q.add(quit_button); add("South", q); Server = new StartServer(0); System.out.println("Server running on " + Server.Address()); System.out.flush(); new GetClients(this, Server.getserverSocket()).start(); } // Create Client Interface else { setTitle("Client Survey"); host = args[0]; port = Integer.parseInt(args[1]); IsClient = 1; Client = new StartClient(this, host, port); display.setEditable(false); add("North", display); Panel p = new Panel(); p.setLayout(new GridLayout(4,2)); CheckboxGroup g = new CheckboxGroup(); yes_checkbox = new Checkbox("Yes", g, false); p.add(yes_checkbox); no_checkbox = new Checkbox("No", g, false); p.add(no_checkbox); p.add(new Label("# of min left")); time_left = new TextField(Integer.toString(timer),3); time_left.setEditable(false); p.add(time_left); p.add(new Label("# of yes responces")); yeses = new TextField("",3); yeses.setEditable(false); p.add(yeses); p.add(new Label("# of no responces")); nos = new TextField("",3); nos.setEditable(false); p.add(nos); add("Center", p); clock = new ClockCanvas(this, "Time Left", 0, 0, timer); add("West", clock); Panel q = new Panel(); q.setLayout(new FlowLayout(FlowLayout.CENTER)); client_send_button = new Button("Send"); client_send_button.setBackground(Color.green); q.add(client_send_button); quit_button = new Button("Quit"); quit_button.setBackground(Color.red); q.add(quit_button); add("South", q); Stopwatch = new Timer(clock,1000); Stopwatch.start(); } } public boolean handleEvent(Event evt) { if (evt.id == Event.WINDOW_DESTROY) System.exit(0); return super.handleEvent(evt); } public boolean action(Event evt, Object arg) { if (evt.target.equals(client_send_button)) { if (sent == 0) { if (answer == 1) { sent = 1; Client.AnswerServer(1); } else if (answer == 2) { sent = 1; Client.AnswerServer(2); } else { AlertDialog dialog = new AlertDialog(this, "ERROR", "You must answer the question before sending."); dialog.show(); } } else { AlertDialog dialog1 = new AlertDialog(this, "ERROR", "You have already sent your answer."); dialog1.show(); } } else if (evt.target.equals(server_send_button)) { if (sent == 0) { Stopwatch = new Timer(clock, 1000); Stopwatch.start(); sent = 1; } else { AlertDialog dialog2 = new AlertDialog(this, "ERROR", "You have already sent the question."); dialog2.show(); } } else if (evt.target.equals(quit_button)) { if (IsClient == 1) { try { PrintStream out = new PrintStream(Client.getSocket().getOutputStream()); out.println("finished"); Client.getSocket().close(); } catch (Exception e) { System.out.println(e); } System.exit(0); } else { if (this.clock.IsDone() == 1) { System.exit(0); } else { AlertDialog dialog3 = new AlertDialog(this, "ERROR", "Time is not up yet."); dialog3.show(); } } } else if (evt.target.equals(yes_checkbox)) answer = 1; else if (evt.target.equals(no_checkbox)) answer = 2; else return super.action(evt,arg); return true; } public void increment_num_connected(int c) { if (IsClient == 0) { num_connected = c; num_clients.setText("" + num_connected); } } public void RemoveFromconnected (Socket s) { SocketItem temp = new SocketItem(s); connected.removeElement(temp); } public void IncrementYesOrNo (int c) { if (c == 1) { num_yeses +=1; yeses.setText("" + num_yeses); } else { num_nos +=1; nos.setText("" + num_nos); } } public static void main(String[] args) { Frame f = new Survey(args); f.resize(650,250); f.show(); } public void stopclock() { int i, j; Stopwatch.stop(); if (IsClient == 0) { connected.trimToSize(); j = connected.size(); for (i = 0; i < j; i++) { SocketItem csock = (SocketItem)connected.elementAt(i); Socket s = csock.getSocket(); try { PrintStream out = new PrintStream(s.getOutputStream()); out.println("" + num_yeses); out.println("" + num_nos); } catch (Exception e) { System.out.println(e); } } } else { Client.FinishClient(); } } public void SendToClient (Socket s, int i) { connected.insertElementAt(new SocketItem(s), i); try { PrintStream out = new PrintStream(s.getOutputStream()); query = display.getText(); out.println("begin"); out.println(query); out.println("" + clock.getseconds()); out.println("" + clock.getminutes()); out.println("" + timer); } catch (Exception e) { System.out.println(e); } } public void clientstart(String q, String s, String m, String t) { display = new TextField(); display.setText(q); seconds = Integer.parseInt(s); seconds += 5; minutes = Integer.parseInt(m); timer = Integer.parseInt(t); clock = new ClockCanvas(this, "Time Left", seconds, minutes, timer); } public void sendResults(String y, String n) { yeses.setText(y); nos.setText(n); } private TextField display = new TextField("", 50); private TextField num_clients, time_left, yeses, nos; private Button server_send_button, client_send_button, quit_button; private Checkbox yes_checkbox, no_checkbox; private String host, query, time; private int port, timer, num_yeses, num_nos, seconds, minutes; private Vector connected = new Vector(); private int IsClient = 0; private StartClient Client; private StartServer Server; private int sent = 0; private int answer = 0; private int num_connected = 0; private int number_clients = 0; private ClockCanvas clock; private Timer Stopwatch; } class SocketItem { public SocketItem(Socket sock) { clientSocket = sock; } public Socket getSocket() { return clientSocket; } private Socket clientSocket; } interface Timed { public void tick(Timer t); } class Timer extends Thread { public Timer(Timed t, int i) { target = t; interval = i; setDaemon(true); } public void run() { while (true) { try { sleep(interval); } catch(InterruptedException e) {} target.tick(this); } } private Timed target; private int interval; } class ClockCanvas extends Canvas implements Timed { public ClockCanvas(Survey S, String c, int s, int m, int total) { tag = c; sec = s; min = m; startfrom = total; parent = S; Full_time = "" + startfrom; twelve_position = 125 - (Full_time.length()*4); if ((startfrom % 2) != 0) { Half_time =new Format("%7.2f").form((startfrom/2.00)); while (Half_time.substring(0,1).equals(" ")) Half_time = Half_time.substring(1,Half_time.length()); Three_quarter_time =new Format("%7.2f").form((startfrom/4.00)); while (Three_quarter_time.substring(0,1).equals(" ")) Three_quarter_time = Three_quarter_time.substring(1,Three_quarter_time.length()); One_quarter_time =new Format("%7.2f").form((3*startfrom/4.00)); while (One_quarter_time.substring(0,1).equals(" ")) One_quarter_time = One_quarter_time.substring(1,One_quarter_time.length()); nine_position = 62 - (Three_quarter_time.length()*4); } else { Half_time = "" + (startfrom/2); if (startfrom % 4 != 0) { Three_quarter_time =new Format("%7.2f").form((startfrom/4.00)); while (Three_quarter_time.substring(0,1).equals(" ")) Three_quarter_time = Three_quarter_time.substring(1,Three_quarter_time.length()); One_quarter_time =new Format("%7.2f").form((3*startfrom/4.00)); while (One_quarter_time.substring(0,1).equals(" ")) One_quarter_time = One_quarter_time.substring(1,One_quarter_time.length()); nine_position = 62 - (Three_quarter_time.length()*4); } else { Three_quarter_time = "" + (startfrom/4); One_quarter_time = "" + (3*startfrom/4); nine_position = 67 - (Three_quarter_time.length()*4); } } six_position = 125 - (Half_time.length()*4); resize(250, 180); repaint(); } public int getseconds() { return sec; } public int getminutes() { return min; } public void paint(Graphics g) { g.drawOval(75, 40, 100, 100); double minuteAngle = 2 * Math.PI * ((min*60) - (startfrom * 15)) / (startfrom * 60); double secondAngle = 2 * Math.PI * (sec - 15) / 60; g.setColor(Color.black); g.drawLine(125, 90, 125 + (int)(35 * Math.cos(minuteAngle)), 90 + (int)(35 * Math.sin(minuteAngle))); g.drawLine(125, 90, 125 + (int)(45 * Math.cos(secondAngle)), 90 + (int)(45 * Math.sin(secondAngle))); g.drawString(Full_time, twelve_position, 37); g.drawString(One_quarter_time, 178, 96); g.drawString(Three_quarter_time, nine_position, 96); g.drawString(Half_time, six_position, 152); g.drawString(tag, 80, 170); if (min == startfrom && doit == 0) { doit = 1; parent.stopclock(); } } public void tick(Timer t) { sec++; min = (sec/60); angle = (int)Math.ceil((360/startfrom)*min); repaint(); if (min == startfrom) { t.stop(); done = 1; } else done = 0; } public int IsDone() { return done; } private Survey parent; private int twelve_position; private int six_position; private int nine_position; private int angle; private int sec; private int min; private int done; private String tag; private String Full_time; private String Half_time; private String Three_quarter_time; private String One_quarter_time; private int startfrom; private int doit = 0; } class AlertDialog extends Dialog { public AlertDialog(Survey parent, String title, String message) { super(parent, title, true); Panel top = new Panel(); top.add(new Label(message)); add("Center", top); Panel bottom = new Panel(); bottom.add(new Button("OK")); add("South", bottom); resize(300, 150); } public boolean action(Event evt, Object arg) { if (arg.equals("OK")) { dispose(); return true; } return false; } public boolean handleEvent(Event evt) { if (evt.id == Event.WINDOW_DESTROY && evt.target == this) System.exit(0); return super.handleEvent(evt); } } class StartServer { public StartServer(int port) { try { S = new ServerSocket(port); } catch (IOException e) { System.err.println(e); System.exit(1); } } public ServerSocket getserverSocket() { return S; } public String Address() { String Answer = new String (""); try { Answer = InetAddress.getLocalHost() + " " + S.getLocalPort(); } catch (UnknownHostException e) { System.err.println(e); System.exit(1); } return Answer; } private ServerSocket S; } class GetClients extends Thread { Survey parent; ServerSocket Sock; GetClients(Survey p, ServerSocket s) { parent = p; Sock = s; } public void run() { int i = 0; try { for (;;) { Socket incoming = Sock.accept(); DataInputStream in = new DataInputStream(incoming.getInputStream()); PrintStream out = new PrintStream(incoming.getOutputStream()); parent.SendToClient(incoming, i); parent.increment_num_connected(i+1); new ThreadHandler(parent, incoming, i).start(); i++; } } catch (Exception e) { System.out.println(e); } } } class ThreadHandler extends Thread { ThreadHandler(Survey p, Socket s, int c) { incoming = s; parent = p; counter = c; } public void run() { try { DataInputStream in = new DataInputStream(incoming.getInputStream()); PrintStream out = new PrintStream(incoming.getOutputStream()); boolean finito = false; while (!finito) { String first = in.readLine(); if (first == null) finito = true; else if (first.equals("yes")) parent.IncrementYesOrNo(1); else if (first.equals("no")) parent.IncrementYesOrNo(0); else if (first.equals("finished")) finito = true; } parent.RemoveFromconnected(incoming); incoming.close(); this.stop(); } catch (Exception e) { System.out.println(e); } } private Socket incoming; private int counter; private Survey parent; } class StartClient { public StartClient(Survey f, String g, int h) { parent = f; try { socks = new Socket(g, h); ins = new DataInputStream(socks.getInputStream()); outs = new PrintStream(socks.getOutputStream()); first = ins.readLine(); query = ins.readLine(); secs = ins.readLine(); mins = ins.readLine(); time = ins.readLine(); parent.clientstart(query, secs, mins, time); } catch (Exception e) { System.out.println(e); } } public void AnswerServer (int o) { try { outs = new PrintStream(socks.getOutputStream()); if (o == 1) outs.println("yes"); else outs.println("no"); } catch (Exception e) { System.out.println(e); } } public void FinishClient() { try { ins = new DataInputStream(socks.getInputStream()); String yes = ins.readLine(); String no = ins.readLine(); parent.sendResults(yes,no); socks.close(); } catch (Exception e) { System.out.println(e); } } public DataInputStream getInputStream() { return ins; } public PrintStream getOutputStream() { return outs; } public Socket getSocket() { return socks; } public String getTime() { return time; } private Survey parent; private DataInputStream ins; private PrintStream outs; private Socket socks; private String first, query, secs, mins, time; }