GitBucket
4.21.2
Toggle navigation
Snippets
Sign in
Files
Branches
1
Releases
Issues
Pull requests
Labels
Priorities
Milestones
Wiki
Forks
mark.george
/
democall3
Browse code
Updated environment variable to match new desktop environment.
master
1 parent
f6956b6
commit
5e899fcd7d99f4af81fce0a52eb77cd26bc96e20
Mark
authored
on 4 Mar 2015
Patch
Showing
3 changed files
src/clients/StudentClient.java
src/clients/TutorClient.java
src/server/Server.java
Ignore Space
Show notes
View
src/clients/StudentClient.java
package clients; import constants.Constants; import discovery.computername.ComputerNameResolver; import discovery.computername.InvalidComputerNameException; import discovery.computername.OtagoComputerNameResolver; import discovery.server.ServiceLocator; import discovery.server.ServiceNotFoundException; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.logging.Level; import java.util.logging.Logger; import network.MessageGenerator; import network.RequestSender; /* * The student's client for requesting help. * * It adds a system tray icon that has a pop-up menu that the student can use to * request help or cancel their request. Requests are automatically canceled * when the student clicks the "exit" menu item, or the client is terminated by * the OS (via a shutdown hook) meaning there should never be requests in the * queue for students who have gone home. * * This aims to be both simple and robust, therefor the StudentClient stores no * state relating to requests, uses no persistent connections to the server, and * receives no information from the server. * * The client uses a multi-cast broadcast to discover the IP of the server, * which it then uses to send requests to the server. * * The wire protocol is very simple. A string in the form "request 23" is a * request for from the machine with ID 23, and "cancel 23" will cancel that * request. Sending multiple "request" commands or multiple "cancel" commands to * the server should cause no duplication or problems on the server and are * effectively ignored. * * @author Mark */ public class StudentClient { private static final Logger LOG = Logger.getLogger(StudentClient.class.getName()); private final PopupMenu trayPopopMenu = new PopupMenu(); private final Image trayIconImage = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/icon.png")); private final TrayIcon systemTrayIcon = new TrayIcon(trayIconImage, "Demo Call " + Constants.VERSION, trayPopopMenu); private final MessageGenerator generator = new MessageGenerator(); private String machineId; private RequestSender requestSender; private boolean foundServer = false; private final Thread shutDownHook = new Thread() { @Override public void run() { cleanUp(); } }; public StudentClient(final ComputerNameResolver nameResolver) { try { createTrayMenu(); // not sure this is necessary but Windows is currently not exiting // when the shutdown hook kicks in... this.shutDownHook.setDaemon(true); Runtime.getRuntime().addShutdownHook(this.shutDownHook); try { nameResolver.resolve(); } catch (InvalidComputerNameException ex) { this.systemTrayIcon.displayMessage("Whoops", ex.getMessage(), TrayIcon.MessageType.ERROR); // give user a chance to read message Thread.sleep(5000); // shutdown exit(); } final String lab = nameResolver.getLabName(); this.machineId = nameResolver.getMachineId(); // find server IP (uses network multicast) final String serverIp = new ServiceLocator().locateServer(lab); this.foundServer = true; // this.systemTrayIcon.displayMessage("Yay", "Server found: " +serverIp, TrayIcon.MessageType.INFO); this.requestSender = new RequestSender(serverIp); } catch (ServiceNotFoundException ex) { this.systemTrayIcon.displayMessage("Whoops", "Could not connect to server. Please try again and let the supervisor know if it continues to happen.", TrayIcon.MessageType.ERROR); } catch (Exception ex) { LOG.log(Level.SEVERE, null, ex); } } private void createTrayMenu() { try { this.systemTrayIcon.setImageAutoSize(true); final MenuItem requestHelp = new MenuItem("Request Help"); requestHelp.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { helpRequest(); } }); final MenuItem requestMarking = new MenuItem("Request Marking"); requestMarking.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { markingRequest(); } }); final MenuItem cancelHelp = new MenuItem("Cancel Help"); cancelHelp.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { cancelHelp(); } }); final MenuItem cancelMarking = new MenuItem("Cancel Marking"); cancelMarking.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { cancelMarking(); } }); final MenuItem cancelAll = new MenuItem("Cancel All Requests"); cancelAll.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { cancelAll(); } }); final MenuItem exit = new MenuItem("Exit"); exit.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { exit(); } }); this.trayPopopMenu.add(requestHelp); this.trayPopopMenu.add(cancelHelp); this.trayPopopMenu.addSeparator(); this.trayPopopMenu.add(requestMarking); this.trayPopopMenu.add(cancelMarking); this.trayPopopMenu.addSeparator(); this.trayPopopMenu.add(cancelAll); this.trayPopopMenu.addSeparator(); this.trayPopopMenu.add(exit); SystemTray.getSystemTray().add(this.systemTrayIcon); } catch (Exception ex) { LOG.log(Level.SEVERE, null, ex); } } private void helpRequest() { try { requestSender.sendRequest(generator.requestHelp(machineId)); systemTrayIcon.displayMessage("Help is on the way", "Your request is now in the queue.", TrayIcon.MessageType.INFO); } catch (Exception ex) { systemTrayIcon.displayMessage("Whoops", "Failed to make request", TrayIcon.MessageType.ERROR); LOG.log(Level.SEVERE, "Error making request", ex); } } private void markingRequest() { try { requestSender.sendRequest(generator.requestMarking(machineId)); systemTrayIcon.displayMessage("Help is on the way", "Your request is now in the queue.", TrayIcon.MessageType.INFO); } catch (Exception ex) { systemTrayIcon.displayMessage("Whoops", "Failed to make request", TrayIcon.MessageType.ERROR); LOG.log(Level.SEVERE, "Error making request", ex); } } private void cancelHelp() { try { requestSender.sendRequest(generator.cancelHelp(machineId)); systemTrayIcon.displayMessage("Request removed", "Your request has been removed from the queue.", TrayIcon.MessageType.INFO); } catch (Exception ex) { systemTrayIcon.displayMessage("Whoops", "Failed to make request", TrayIcon.MessageType.ERROR); LOG.log(Level.SEVERE, "Error making request", ex); } } private void cancelMarking() { try { requestSender.sendRequest(generator.cancelMarking(machineId)); systemTrayIcon.displayMessage("Request removed", "Your request has been removed from the queue.", TrayIcon.MessageType.INFO); } catch (Exception ex) { systemTrayIcon.displayMessage("Whoops", "Failed to cancel request", TrayIcon.MessageType.ERROR); LOG.log(Level.SEVERE, "Error cancelling request", ex); } } private void cancelAll() { try { requestSender.sendRequest(generator.cancelHelp(machineId)); requestSender.sendRequest(generator.cancelMarking(machineId)); systemTrayIcon.displayMessage("All requests removeds", "All of your request have been removed from the queues.", TrayIcon.MessageType.INFO); } catch (Exception ex) { systemTrayIcon.displayMessage("Whoops", "Failed to cancel request", TrayIcon.MessageType.ERROR); LOG.log(Level.SEVERE, "Error cancelling request", ex); } } private void exit() { this.cleanUp(); // force an exit since they are several threads lurking around that could keep the process running System.exit(0); } private void cleanUp() { // cancel any requests the student has made if (this.foundServer) { cancelAll(); } // remove the tray icon from the system tray SystemTray.getSystemTray().remove(systemTrayIcon); } @SuppressWarnings("ResultOfObjectAllocationIgnored") public static void main(final String[] args) { // System.out.println("Student Client"); // final String name = "SB318-15"; // final String name = "SB317-10"; // final String name = "SBEASTCAL1-30"; // final String name = "SB316-15"; final String name = args.length > 0 ? args[0] : null; final ComputerNameResolver nameResolver = new OtagoComputerNameResolver(name, "CLIENTNAME"); new StudentClient(nameResolver); } }
package clients; import constants.Constants; import discovery.computername.ComputerNameResolver; import discovery.computername.InvalidComputerNameException; import discovery.computername.OtagoComputerNameResolver; import discovery.server.ServiceLocator; import discovery.server.ServiceNotFoundException; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.logging.Level; import java.util.logging.Logger; import network.MessageGenerator; import network.RequestSender; /* * The student's client for requesting help. * * It adds a system tray icon that has a pop-up menu that the student can use to * request help or cancel their request. Requests are automatically canceled * when the student clicks the "exit" menu item, or the client is terminated by * the OS (via a shutdown hook) meaning there should never be requests in the * queue for students who have gone home. * * This aims to be both simple and robust, therefor the StudentClient stores no * state relating to requests, uses no persistent connections to the server, and * receives no information from the server. * * The client uses a multi-cast broadcast to discover the IP of the server, * which it then uses to send requests to the server. * * The wire protocol is very simple. A string in the form "request 23" is a * request for from the machine with ID 23, and "cancel 23" will cancel that * request. Sending multiple "request" commands or multiple "cancel" commands to * the server should cause no duplication or problems on the server and are * effectively ignored. * * @author Mark */ public class StudentClient { private static final Logger LOG = Logger.getLogger(StudentClient.class.getName()); private final PopupMenu trayPopopMenu = new PopupMenu(); private final Image trayIconImage = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/icon.png")); private final TrayIcon systemTrayIcon = new TrayIcon(trayIconImage, "Demo Call " + Constants.VERSION, trayPopopMenu); private final MessageGenerator generator = new MessageGenerator(); private String machineId; private RequestSender requestSender; private boolean foundServer = false; private final Thread shutDownHook = new Thread() { @Override public void run() { cleanUp(); } }; public StudentClient(final ComputerNameResolver nameResolver) { try { createTrayMenu(); // not sure this is necessary but Windows is currently not exiting // when the shutdown hook kicks in... this.shutDownHook.setDaemon(true); Runtime.getRuntime().addShutdownHook(this.shutDownHook); try { nameResolver.resolve(); } catch (InvalidComputerNameException ex) { this.systemTrayIcon.displayMessage("Whoops", ex.getMessage(), TrayIcon.MessageType.ERROR); // give user a chance to read message Thread.sleep(5000); // shutdown exit(); } final String lab = nameResolver.getLabName(); this.machineId = nameResolver.getMachineId(); // find server IP (uses network multicast) final String serverIp = new ServiceLocator().locateServer(lab); this.foundServer = true; // this.systemTrayIcon.displayMessage("Yay", "Server found: " +serverIp, TrayIcon.MessageType.INFO); this.requestSender = new RequestSender(serverIp); } catch (ServiceNotFoundException ex) { this.systemTrayIcon.displayMessage("Whoops", "Could not connect to server. Please try again and let the supervisor know if it continues to happen.", TrayIcon.MessageType.ERROR); } catch (Exception ex) { LOG.log(Level.SEVERE, null, ex); } } private void createTrayMenu() { try { this.systemTrayIcon.setImageAutoSize(true); final MenuItem requestHelp = new MenuItem("Request Help"); requestHelp.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { helpRequest(); } }); final MenuItem requestMarking = new MenuItem("Request Marking"); requestMarking.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { markingRequest(); } }); final MenuItem cancelHelp = new MenuItem("Cancel Help"); cancelHelp.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { cancelHelp(); } }); final MenuItem cancelMarking = new MenuItem("Cancel Marking"); cancelMarking.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { cancelMarking(); } }); final MenuItem cancelAll = new MenuItem("Cancel All Requests"); cancelAll.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { cancelAll(); } }); final MenuItem exit = new MenuItem("Exit"); exit.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { exit(); } }); this.trayPopopMenu.add(requestHelp); this.trayPopopMenu.add(cancelHelp); this.trayPopopMenu.addSeparator(); this.trayPopopMenu.add(requestMarking); this.trayPopopMenu.add(cancelMarking); this.trayPopopMenu.addSeparator(); this.trayPopopMenu.add(cancelAll); this.trayPopopMenu.addSeparator(); this.trayPopopMenu.add(exit); SystemTray.getSystemTray().add(this.systemTrayIcon); } catch (Exception ex) { LOG.log(Level.SEVERE, null, ex); } } private void helpRequest() { try { requestSender.sendRequest(generator.requestHelp(machineId)); systemTrayIcon.displayMessage("Help is on the way", "Your request is now in the queue.", TrayIcon.MessageType.INFO); } catch (Exception ex) { systemTrayIcon.displayMessage("Whoops", "Failed to make request", TrayIcon.MessageType.ERROR); LOG.log(Level.SEVERE, "Error making request", ex); } } private void markingRequest() { try { requestSender.sendRequest(generator.requestMarking(machineId)); systemTrayIcon.displayMessage("Help is on the way", "Your request is now in the queue.", TrayIcon.MessageType.INFO); } catch (Exception ex) { systemTrayIcon.displayMessage("Whoops", "Failed to make request", TrayIcon.MessageType.ERROR); LOG.log(Level.SEVERE, "Error making request", ex); } } private void cancelHelp() { try { requestSender.sendRequest(generator.cancelHelp(machineId)); systemTrayIcon.displayMessage("Request removed", "Your request has been removed from the queue.", TrayIcon.MessageType.INFO); } catch (Exception ex) { systemTrayIcon.displayMessage("Whoops", "Failed to make request", TrayIcon.MessageType.ERROR); LOG.log(Level.SEVERE, "Error making request", ex); } } private void cancelMarking() { try { requestSender.sendRequest(generator.cancelMarking(machineId)); systemTrayIcon.displayMessage("Request removed", "Your request has been removed from the queue.", TrayIcon.MessageType.INFO); } catch (Exception ex) { systemTrayIcon.displayMessage("Whoops", "Failed to cancel request", TrayIcon.MessageType.ERROR); LOG.log(Level.SEVERE, "Error cancelling request", ex); } } private void cancelAll() { try { requestSender.sendRequest(generator.cancelHelp(machineId)); requestSender.sendRequest(generator.cancelMarking(machineId)); systemTrayIcon.displayMessage("All requests removeds", "All of your request have been removed from the queues.", TrayIcon.MessageType.INFO); } catch (Exception ex) { systemTrayIcon.displayMessage("Whoops", "Failed to cancel request", TrayIcon.MessageType.ERROR); LOG.log(Level.SEVERE, "Error cancelling request", ex); } } private void exit() { this.cleanUp(); // force an exit since they are several threads lurking around that could keep the process running System.exit(0); } private void cleanUp() { // cancel any requests the student has made if (this.foundServer) { cancelAll(); } // remove the tray icon from the system tray SystemTray.getSystemTray().remove(systemTrayIcon); } @SuppressWarnings("ResultOfObjectAllocationIgnored") public static void main(final String[] args) { // System.out.println("Student Client"); // final String name = "SB318-15"; // final String name = "SB317-10"; // final String name = "SBEASTCAL1-30"; // final String name = "SB316-15"; final String name = args.length > 0 ? args[0] : null; final ComputerNameResolver nameResolver = new OtagoComputerNameResolver(name, "COMPUTERNAME"); new StudentClient(nameResolver); } }
Ignore Space
Show notes
View
src/clients/TutorClient.java
package clients; import constants.Constants; import discovery.computername.ComputerNameResolver; import discovery.computername.InvalidComputerNameException; import discovery.computername.OtagoComputerNameResolver; import discovery.server.ServiceLocator; import discovery.server.ServiceNotFoundException; import gui.Lab; import gui.LabRegistry; import gui.QueuePanel; import gui.processors.TutorLabelProcessor; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; import java.util.LinkedHashSet; import java.util.logging.Level; import java.util.logging.Logger; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import javax.swing.GroupLayout; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.LayoutStyle; import network.RequestSender; /** * * @author Mark */ public class TutorClient { private static final Logger LOG = Logger.getLogger(TutorClient.class.getName()); private TutorLabelProcessor processor; private String serverIp; private Clip beep; @SuppressWarnings("rawtypes") public TutorClient(final ComputerNameResolver nameResolver) { JFrame frame = null; try { nameResolver.resolve(); String labName = nameResolver.getLabName(); final LabRegistry registry = new LabRegistry(); final Lab[] labs = registry.getLabs().toArray(new Lab[registry.getLabs().size()]); final Lab currentLab = registry.getLab(labName); final Lab lab = (Lab) JOptionPane.showInputDialog(null, "Which lab?", "Which lab?", JOptionPane.QUESTION_MESSAGE, null, labs, currentLab); if (lab == null) { System.exit(0); } else { labName = lab.getLabName(); } serverIp = new ServiceLocator().locateServer(labName); frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setAlwaysOnTop(true); final JPanel mapPanel = lab.getPanel(); QueuePanel helpPanel = new QueuePanel(new LinkedHashSet()); QueuePanel markingPanel = new QueuePanel(new LinkedHashSet()); processor = new TutorLabelProcessor(helpPanel, markingPanel, serverIp); processor.processLabels(mapPanel); frame.setTitle(String.format("Democall %1s - Tutor Client (%1s)", Constants.VERSION, lab.getLabDescription())); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(frame.getContentPane()); frame.getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(helpPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE) .addComponent(markingPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE) .addComponent(mapPanel, GroupLayout.Alignment.CENTER, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); layout.setVerticalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(helpPanel, Constants.QUEUE_MIN_HEIGHT, GroupLayout.DEFAULT_SIZE, Constants.QUEUE_MAX_HEIGHT) .addComponent(markingPanel, Constants.QUEUE_MIN_HEIGHT, GroupLayout.DEFAULT_SIZE, Constants.QUEUE_MAX_HEIGHT) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(mapPanel, 100, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED))); frame.pack(); frame.setVisible(true); final Socket socket = new RequestSender(serverIp).registerTutorClient(); new Thread(new Runnable() { @Override public void run() { try { BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); while (true) { final String message = reader.readLine(); if (message != null) { // readLine returns null if buffer is empty processMessage(message); } } } catch (IOException ex) { Logger.getLogger(TutorClient.class.getName()).log(Level.SEVERE, null, ex); } } } ).start(); // dynamic font resize based on window size final int defaultSize = Constants.DEFAULT_MAP_FONT_SIZE; final int defaultWidth = mapPanel.getWidth(); frame.addComponentListener( new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { int newWidth = mapPanel.getWidth(); float scaleFactor = (float) newWidth / (float) defaultWidth; int newFontSize = Math.round(defaultSize * scaleFactor); processor.resizeFonts(newFontSize); } } ); // load the beep audio clip into the audio system sample bank AudioInputStream is = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("/blip.wav")); beep = AudioSystem.getClip(); beep.open(is); } catch (InvalidComputerNameException ex) { JOptionPane.showMessageDialog(frame, ex.getMessage(), "Error getting computer name.", JOptionPane.ERROR_MESSAGE); } catch (ServiceNotFoundException ex) { JOptionPane.showMessageDialog(frame, "Could not connect to server.", "Connection error", JOptionPane.ERROR_MESSAGE); } catch (Exception ex) { LOG.log(Level.SEVERE, null, ex); } } public void processMessage(final String message) { // System.out.println("processMessage: " + message); final String[] messageBits = message.split(" "); final String performative = messageBits[0]; final int machineId = Integer.parseInt(messageBits[1]); if ("help".equals(performative)) { processor.requestHelp(machineId); beep(); } else if ("marking".equals(performative)) { processor.requestMarking(machineId); beep(); } else if ("cancelhelp".equals(performative)) { processor.cancelHelp(machineId); } else if ("cancelmarking".equals(performative)) { processor.cancelMarking(machineId); } } private void beep() { try { if(beep !=null && beep.isRunning()) beep.stop(); beep.setFramePosition(0); // rewind clip beep.start(); } catch (Exception ex) { Logger.getLogger(TutorClient.class.getName()).log(Level.SEVERE, null, ex); } } @SuppressWarnings("ResultOfObjectAllocationIgnored") public static void main(final String[] args) { // System.out.println("Tutor Client"); // final String name = "SBEASTCAL1-31"; // final String name = "SB318-10"; // final String name = "SB316-10"; final String name = args.length > 0 ? args[0] : null; final ComputerNameResolver nameResolver = new OtagoComputerNameResolver(name, "CLIENTNNAME"); new TutorClient(nameResolver); } }
package clients; import constants.Constants; import discovery.computername.ComputerNameResolver; import discovery.computername.InvalidComputerNameException; import discovery.computername.OtagoComputerNameResolver; import discovery.server.ServiceLocator; import discovery.server.ServiceNotFoundException; import gui.Lab; import gui.LabRegistry; import gui.QueuePanel; import gui.processors.TutorLabelProcessor; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; import java.util.LinkedHashSet; import java.util.logging.Level; import java.util.logging.Logger; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import javax.swing.GroupLayout; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.LayoutStyle; import network.RequestSender; /** * * @author Mark */ public class TutorClient { private static final Logger LOG = Logger.getLogger(TutorClient.class.getName()); private TutorLabelProcessor processor; private String serverIp; private Clip beep; @SuppressWarnings("rawtypes") public TutorClient(final ComputerNameResolver nameResolver) { JFrame frame = null; try { nameResolver.resolve(); String labName = nameResolver.getLabName(); final LabRegistry registry = new LabRegistry(); final Lab[] labs = registry.getLabs().toArray(new Lab[registry.getLabs().size()]); final Lab currentLab = registry.getLab(labName); final Lab lab = (Lab) JOptionPane.showInputDialog(null, "Which lab?", "Which lab?", JOptionPane.QUESTION_MESSAGE, null, labs, currentLab); if (lab == null) { System.exit(0); } else { labName = lab.getLabName(); } serverIp = new ServiceLocator().locateServer(labName); frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setAlwaysOnTop(true); final JPanel mapPanel = lab.getPanel(); QueuePanel helpPanel = new QueuePanel(new LinkedHashSet()); QueuePanel markingPanel = new QueuePanel(new LinkedHashSet()); processor = new TutorLabelProcessor(helpPanel, markingPanel, serverIp); processor.processLabels(mapPanel); frame.setTitle(String.format("Democall %1s - Tutor Client (%1s)", Constants.VERSION, lab.getLabDescription())); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(frame.getContentPane()); frame.getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(helpPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE) .addComponent(markingPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE) .addComponent(mapPanel, GroupLayout.Alignment.CENTER, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); layout.setVerticalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(helpPanel, Constants.QUEUE_MIN_HEIGHT, GroupLayout.DEFAULT_SIZE, Constants.QUEUE_MAX_HEIGHT) .addComponent(markingPanel, Constants.QUEUE_MIN_HEIGHT, GroupLayout.DEFAULT_SIZE, Constants.QUEUE_MAX_HEIGHT) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(mapPanel, 100, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED))); frame.pack(); frame.setVisible(true); final Socket socket = new RequestSender(serverIp).registerTutorClient(); new Thread(new Runnable() { @Override public void run() { try { BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); while (true) { final String message = reader.readLine(); if (message != null) { // readLine returns null if buffer is empty processMessage(message); } } } catch (IOException ex) { Logger.getLogger(TutorClient.class.getName()).log(Level.SEVERE, null, ex); } } } ).start(); // dynamic font resize based on window size final int defaultSize = Constants.DEFAULT_MAP_FONT_SIZE; final int defaultWidth = mapPanel.getWidth(); frame.addComponentListener( new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { int newWidth = mapPanel.getWidth(); float scaleFactor = (float) newWidth / (float) defaultWidth; int newFontSize = Math.round(defaultSize * scaleFactor); processor.resizeFonts(newFontSize); } } ); // load the beep audio clip into the audio system sample bank AudioInputStream is = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("/blip.wav")); beep = AudioSystem.getClip(); beep.open(is); } catch (InvalidComputerNameException ex) { JOptionPane.showMessageDialog(frame, ex.getMessage(), "Error getting computer name.", JOptionPane.ERROR_MESSAGE); } catch (ServiceNotFoundException ex) { JOptionPane.showMessageDialog(frame, "Could not connect to server.", "Connection error", JOptionPane.ERROR_MESSAGE); } catch (Exception ex) { LOG.log(Level.SEVERE, null, ex); } } public void processMessage(final String message) { // System.out.println("processMessage: " + message); final String[] messageBits = message.split(" "); final String performative = messageBits[0]; final int machineId = Integer.parseInt(messageBits[1]); if ("help".equals(performative)) { processor.requestHelp(machineId); beep(); } else if ("marking".equals(performative)) { processor.requestMarking(machineId); beep(); } else if ("cancelhelp".equals(performative)) { processor.cancelHelp(machineId); } else if ("cancelmarking".equals(performative)) { processor.cancelMarking(machineId); } } private void beep() { try { if(beep !=null && beep.isRunning()) beep.stop(); beep.setFramePosition(0); // rewind clip beep.start(); } catch (Exception ex) { Logger.getLogger(TutorClient.class.getName()).log(Level.SEVERE, null, ex); } } @SuppressWarnings("ResultOfObjectAllocationIgnored") public static void main(final String[] args) { // System.out.println("Tutor Client"); // final String name = "SBEASTCAL1-31"; // final String name = "SB318-10"; // final String name = "SB316-10"; final String name = args.length > 0 ? args[0] : null; final ComputerNameResolver nameResolver = new OtagoComputerNameResolver(name, "COMPUTERNAME"); new TutorClient(nameResolver); } }
Ignore Space
Show notes
View
src/server/Server.java
package server; import constants.Constants; import discovery.computername.ComputerNameResolver; import discovery.computername.InvalidComputerNameException; import discovery.computername.OtagoComputerNameResolver; import discovery.server.BroadcastResponder; import gui.Lab; import gui.LabRegistry; import gui.QueuePanel; import gui.processors.RequestProcessor; import gui.processors.ServerLabelProcessor; import java.awt.BorderLayout; import java.awt.event.*; import java.io.IOException; import java.util.Collection; import java.util.LinkedHashSet; import javax.swing.GroupLayout; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.LayoutStyle; /** * * @author Mark */ public final class Server { private Server() { } public static void main(final String[] args) throws IOException { // System.out.println("Server"); // final String name = "SBEASTCAL1-01"; // final String name = "SB316-01"; // final String name = "SB317-1"; // final String name = "SB318-1"; final String name = args.length > 0 ? args[0] : null; ComputerNameResolver nameResolver = null; try { nameResolver = new OtagoComputerNameResolver(name, "CLIENTNAME"); } catch (InvalidComputerNameException ex) { JOptionPane.showMessageDialog(null, "There is no map for this lab yet.\nThe machine ID is : " + name, "No map", JOptionPane.ERROR_MESSAGE); System.exit(Constants.EXIT_NO_MAP_FOUND); } nameResolver.resolve(); final String labName = nameResolver.getLabName(); final JFrame frame = new JFrame(); frame.setLayout(new BorderLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final LabRegistry registry = new LabRegistry(); final Lab lab = registry.getLab(labName); final JPanel mapPanel = lab.getPanel(); final Collection<Integer> helpQueue = new LinkedHashSet<Integer>(); final Collection<Integer> markingQueue = new LinkedHashSet<Integer>(); QueuePanel helpPanel = new QueuePanel(helpQueue); QueuePanel markingPanel = new QueuePanel(markingQueue); final RequestProcessor processor = new ServerLabelProcessor(helpPanel, markingPanel); processor.processLabels(mapPanel); frame.setTitle(String.format("Democall %1s - Server (%1s)", Constants.VERSION, lab.getLabDescription())); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(frame.getContentPane()); frame.getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(helpPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE) .addComponent(markingPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE) .addComponent(mapPanel, GroupLayout.Alignment.CENTER, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); layout.setVerticalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(helpPanel, Constants.QUEUE_MIN_HEIGHT, GroupLayout.DEFAULT_SIZE, Constants.QUEUE_MAX_HEIGHT) .addComponent(markingPanel, Constants.QUEUE_MIN_HEIGHT, GroupLayout.DEFAULT_SIZE, Constants.QUEUE_MAX_HEIGHT) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(mapPanel, 100, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED))); frame.pack(); frame.setVisible(true); // dynamic font resize based on window size final int defaultFontSize = Constants.DEFAULT_MAP_FONT_SIZE; final int currentHeight = mapPanel.getHeight(); frame.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { int newHeight = mapPanel.getHeight(); float scaleFactor = (float) newHeight / (float) currentHeight * 0.8f; int newFontSize = Math.round(defaultFontSize * scaleFactor); processor.resizeFonts(newFontSize); } }); new ApplicationHandler(processor).start(); new BroadcastResponder(labName).start(); } }
package server; import constants.Constants; import discovery.computername.ComputerNameResolver; import discovery.computername.InvalidComputerNameException; import discovery.computername.OtagoComputerNameResolver; import discovery.server.BroadcastResponder; import gui.Lab; import gui.LabRegistry; import gui.QueuePanel; import gui.processors.RequestProcessor; import gui.processors.ServerLabelProcessor; import java.awt.BorderLayout; import java.awt.event.*; import java.io.IOException; import java.util.Collection; import java.util.LinkedHashSet; import javax.swing.GroupLayout; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.LayoutStyle; /** * * @author Mark */ public final class Server { private Server() { } public static void main(final String[] args) throws IOException { // System.out.println("Server"); // final String name = "SBEASTCAL1-01"; // final String name = "SB316-01"; // final String name = "SB317-1"; // final String name = "SB318-1"; final String name = args.length > 0 ? args[0] : null; ComputerNameResolver nameResolver = null; try { nameResolver = new OtagoComputerNameResolver(name, "COMPUTERNAME"); } catch (InvalidComputerNameException ex) { JOptionPane.showMessageDialog(null, "There is no map for this lab yet.\nThe machine ID is : " + name, "No map", JOptionPane.ERROR_MESSAGE); System.exit(Constants.EXIT_NO_MAP_FOUND); } nameResolver.resolve(); final String labName = nameResolver.getLabName(); final JFrame frame = new JFrame(); frame.setLayout(new BorderLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final LabRegistry registry = new LabRegistry(); final Lab lab = registry.getLab(labName); final JPanel mapPanel = lab.getPanel(); final Collection<Integer> helpQueue = new LinkedHashSet<Integer>(); final Collection<Integer> markingQueue = new LinkedHashSet<Integer>(); QueuePanel helpPanel = new QueuePanel(helpQueue); QueuePanel markingPanel = new QueuePanel(markingQueue); final RequestProcessor processor = new ServerLabelProcessor(helpPanel, markingPanel); processor.processLabels(mapPanel); frame.setTitle(String.format("Democall %1s - Server (%1s)", Constants.VERSION, lab.getLabDescription())); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(frame.getContentPane()); frame.getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(helpPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE) .addComponent(markingPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE) .addComponent(mapPanel, GroupLayout.Alignment.CENTER, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); layout.setVerticalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(helpPanel, Constants.QUEUE_MIN_HEIGHT, GroupLayout.DEFAULT_SIZE, Constants.QUEUE_MAX_HEIGHT) .addComponent(markingPanel, Constants.QUEUE_MIN_HEIGHT, GroupLayout.DEFAULT_SIZE, Constants.QUEUE_MAX_HEIGHT) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(mapPanel, 100, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED))); frame.pack(); frame.setVisible(true); // dynamic font resize based on window size final int defaultFontSize = Constants.DEFAULT_MAP_FONT_SIZE; final int currentHeight = mapPanel.getHeight(); frame.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { int newHeight = mapPanel.getHeight(); float scaleFactor = (float) newHeight / (float) currentHeight * 0.8f; int newFontSize = Math.round(defaultFontSize * scaleFactor); processor.resizeFonts(newFontSize); } }); new ApplicationHandler(processor).start(); new BroadcastResponder(labName).start(); } }
Show line notes below