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
Minor changes.
master
1 parent
b2a8b9f
commit
585d2980bc67f1277b615b668f593a1f1aea1c7c
Mark
authored
on 9 Aug 2013
Patch
Showing
4 changed files
src/clients/StudentClient.java
src/clients/TutorClient.java
src/server/RequestThread.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 request = new MenuItem("Request Help"); request.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { makeRequest(); } }); final MenuItem cancel = new MenuItem("Cancel Request"); cancel.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { cancelRequest(); } }); final MenuItem exit = new MenuItem("Exit"); exit.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { exit(); } }); this.trayPopopMenu.add(request); this.trayPopopMenu.add(cancel); this.trayPopopMenu.addSeparator(); this.trayPopopMenu.add(exit); SystemTray.getSystemTray().add(this.systemTrayIcon); } catch (Exception ex) { LOG.log(Level.SEVERE, null, ex); } } private void makeRequest() { 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 cancelRequest() { try { requestSender.sendRequest(generator.cancelRequest(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 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) { cancelRequest(); } // remove the tray icon from the system tray SystemTray.getSystemTray().remove(systemTrayIcon); } @SuppressWarnings("ResultOfObjectAllocationIgnored") public static void main(final String[] args) { // final String name = "SB318-15"; // final String name = "SB317-10"; // final String name = "SBEASTCAL1-30"; final String name = args.length > 0 ? args[0] : null; final ComputerNameResolver nameResolver = new OtagoComputerNameResolver(name, "COMPUTERNAME"); 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 request = new MenuItem("Request Help"); request.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { makeRequest(); } }); final MenuItem cancel = new MenuItem("Cancel Request"); cancel.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { cancelRequest(); } }); final MenuItem exit = new MenuItem("Exit"); exit.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { exit(); } }); this.trayPopopMenu.add(request); this.trayPopopMenu.add(cancel); this.trayPopopMenu.addSeparator(); this.trayPopopMenu.add(exit); SystemTray.getSystemTray().add(this.systemTrayIcon); } catch (Exception ex) { LOG.log(Level.SEVERE, null, ex); } } private void makeRequest() { 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 cancelRequest() { try { requestSender.sendRequest(generator.cancelRequest(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 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) { cancelRequest(); } // remove the tray icon from the system tray SystemTray.getSystemTray().remove(systemTrayIcon); } @SuppressWarnings("ResultOfObjectAllocationIgnored") public static void main(final String[] args) { final String name = "SB318-15"; // final String name = "SB317-10"; // final String name = "SBEASTCAL1-30"; // 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.BorderLayout; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.io.ObjectInputStream; import java.util.Collection; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import network.RequestSender; /** * * @author Mark */ public class TutorClient { private static final Logger LOG = Logger.getLogger(TutorClient.class.getName()); private String serverIp; @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(); final TutorLabelProcessor processor = new TutorLabelProcessor(serverIp); processor.processLabels(mapPanel); frame.setTitle(String.format("Democall %1s - Tutor Client (%1s)", Constants.VERSION, lab.getLabDescription())); frame.setLayout(new BorderLayout()); frame.add(BorderLayout.NORTH, new QueuePanel()); frame.add(BorderLayout.CENTER, mapPanel); frame.pack(); frame.setVisible(true); // final Timer timer = new Timer(true); // timer.scheduleAtFixedRate(new TimerTask() { // // @Override // public void run() { // final Collection<Integer> queue = new RequestSender(serverIp).requestQueue(); // processor.update(queue); // } // }, 0, Constants.TUTOR_CLIENT_POLL); final ObjectInputStream ois = new RequestSender(serverIp).registerTutorClient(); new Thread(new Runnable(){ public void run() { while(true) { try { @SuppressWarnings("unchecked") Collection<Integer> queue = (Collection<Integer>)ois.readObject(); // System.out.println(queue); processor.update(queue); } catch (Exception ex) { Logger.getLogger(TutorClient.class.getName()).log(Level.SEVERE, null, ex); break; } } } }).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); } }); } 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); } } @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 = args.length > 0 ? args[0] : null; final ComputerNameResolver nameResolver = new OtagoComputerNameResolver(name, "COMPUTERNAME"); 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.BorderLayout; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.io.ObjectInputStream; import java.util.Collection; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import network.RequestSender; /** * * @author Mark */ public class TutorClient { private static final Logger LOG = Logger.getLogger(TutorClient.class.getName()); private String serverIp; @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(); final TutorLabelProcessor processor = new TutorLabelProcessor(serverIp); processor.processLabels(mapPanel); frame.setTitle(String.format("Democall %1s - Tutor Client (%1s)", Constants.VERSION, lab.getLabDescription())); frame.setLayout(new BorderLayout()); frame.add(BorderLayout.NORTH, new QueuePanel()); frame.add(BorderLayout.CENTER, mapPanel); frame.pack(); frame.setVisible(true); // final Timer timer = new Timer(true); // timer.scheduleAtFixedRate(new TimerTask() { // // @Override // public void run() { // final Collection<Integer> queue = new RequestSender(serverIp).requestQueue(); // processor.update(queue); // } // }, 0, Constants.TUTOR_CLIENT_POLL); final ObjectInputStream ois = new RequestSender(serverIp).registerTutorClient(); new Thread(new Runnable(){ public void run() { while(true) { try { @SuppressWarnings("unchecked") Collection<Integer> queue = (Collection<Integer>)ois.readObject(); System.out.println(queue); processor.update(queue); } catch (Exception ex) { Logger.getLogger(TutorClient.class.getName()).log(Level.SEVERE, null, ex); break; } } } }).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); } }); } 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); } } @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 = args.length > 0 ? args[0] : null; final ComputerNameResolver nameResolver = new OtagoComputerNameResolver(name, "COMPUTERNAME"); new TutorClient(nameResolver); } }
Ignore Space
Show notes
View
src/server/RequestThread.java
package server; import constants.Constants; import gui.processors.AbstractLabelProcessor; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.ObjectOutputStream; import java.net.Socket; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Mark */ public class RequestThread extends Thread { private static final Logger LOG = Logger.getLogger(RequestThread.class.getName()); private final AbstractLabelProcessor processor; private final Socket socket; private boolean dontClose = false; public RequestThread(final AbstractLabelProcessor processor, final Socket socket) { super(); super.setDaemon(true); this.processor = processor; this.socket = socket; } @Override public void run() { try { final BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); final String message = reader.readLine(); if (message != null) { // readLine returns null if buffer is empty processMessage(message); } if(!dontClose) { socket.close(); } } catch (IOException ex) { LOG.log(Level.SEVERE, null, ex); } } public void processMessage(final String message) { // System.out.println(message); final String[] messageBits = message.split(" "); final String performative = messageBits[0]; final int machineId = Integer.parseInt(messageBits[1]); if ("request".equals(performative)) { processor.request(machineId); } else if ("cancel".equals(performative)) { processor.cancel(machineId); } else if ("queue".equals(performative)) { try { final ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); oos.writeObject(processor.getQueue()); oos.close(); } catch (IOException ex) { LOG.log(Level.SEVERE, null, ex); } } else if ("register".equals(performative)) { try { final ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); oos.writeObject(processor.getQueue()); oos.flush(); Constants.PERSISTENT_SOCKETS.add(oos); dontClose = true; } catch (IOException ex) { LOG.log(Level.SEVERE, null, ex); } } else { LOG.log(Level.SEVERE, "Unknown command: {0}", performative); } } }
package server; import constants.Constants; import gui.processors.AbstractLabelProcessor; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.ObjectOutputStream; import java.net.Socket; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Mark */ public class RequestThread extends Thread { private static final Logger LOG = Logger.getLogger(RequestThread.class.getName()); private final AbstractLabelProcessor processor; private final Socket socket; private boolean dontClose = false; public RequestThread(final AbstractLabelProcessor processor, final Socket socket) { super(); super.setDaemon(true); this.processor = processor; this.socket = socket; } @Override public void run() { try { final BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); final String message = reader.readLine(); if (message != null) { // readLine returns null if buffer is empty processMessage(message); } if(!dontClose) { socket.close(); } } catch (IOException ex) { LOG.log(Level.SEVERE, null, ex); } } public void processMessage(final String message) { final String[] messageBits = message.split(" "); final String performative = messageBits[0]; final int machineId = Integer.parseInt(messageBits[1]); if ("request".equals(performative)) { processor.request(machineId); } else if ("cancel".equals(performative)) { processor.cancel(machineId); } else if ("queue".equals(performative)) { try { final ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); oos.writeObject(processor.getQueue()); oos.close(); } catch (IOException ex) { LOG.log(Level.SEVERE, null, ex); } } else if ("register".equals(performative)) { try { final ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); oos.writeObject(processor.getQueue()); oos.flush(); Constants.PERSISTENT_SOCKETS.add(oos); dontClose = true; } catch (IOException ex) { LOG.log(Level.SEVERE, null, ex); } } else { LOG.log(Level.SEVERE, "Unknown command: {0}", performative); } } }
Ignore Space
Show notes
View
src/server/Server.java
package server; import constants.Constants; import discovery.computername.ComputerNameResolver; import discovery.computername.OtagoComputerNameResolver; import discovery.server.BroadcastResponder; import gui.Lab; import gui.LabRegistry; import gui.QueuePanel; import gui.processors.AbstractLabelProcessor; import gui.processors.ServerLabelProcessor; import java.awt.BorderLayout; import java.awt.event.*; import java.io.IOException; 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 = "SB317-1"; // final String name = "SB318-1"; final String name = args.length > 0 ? args[0] : null; final ComputerNameResolver nameResolver = new OtagoComputerNameResolver(name, "COMPUTERNAME"); 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); if (lab == null) { JOptionPane.showMessageDialog(frame, "There is no map for this lab yet.\nThe lab ID is : " + labName, "No map", JOptionPane.ERROR_MESSAGE); System.exit(Constants.EXIT_NO_MAP_FOUND); } final JPanel mapPanel = lab.getPanel(); final AbstractLabelProcessor processor = new ServerLabelProcessor(); processor.processLabels(mapPanel); frame.setTitle(String.format("Democall %1s - Server (%1s)", Constants.VERSION, lab.getLabDescription())); QueuePanel queuePanel = new QueuePanel(); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(frame.getContentPane()); frame.getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(queuePanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(mapPanel, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); layout.setVerticalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(queuePanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(mapPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); frame.pack(); frame.setVisible(true); // 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); } }); new ApplicationHandler(processor).start(); new BroadcastResponder(labName).start(); } }
package server; import constants.Constants; import discovery.computername.ComputerNameResolver; import discovery.computername.OtagoComputerNameResolver; import discovery.server.BroadcastResponder; import gui.Lab; import gui.LabRegistry; import gui.QueuePanel; import gui.processors.AbstractLabelProcessor; import gui.processors.ServerLabelProcessor; import java.awt.BorderLayout; import java.awt.event.*; import java.io.IOException; 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 = "SB317-1"; final String name = "SB318-1"; // final String name = args.length > 0 ? args[0] : null; final ComputerNameResolver nameResolver = new OtagoComputerNameResolver(name, "COMPUTERNAME"); 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); if (lab == null) { JOptionPane.showMessageDialog(frame, "There is no map for this lab yet.\nThe lab ID is : " + labName, "No map", JOptionPane.ERROR_MESSAGE); System.exit(Constants.EXIT_NO_MAP_FOUND); } final JPanel mapPanel = lab.getPanel(); final AbstractLabelProcessor processor = new ServerLabelProcessor(); processor.processLabels(mapPanel); frame.setTitle(String.format("Democall %1s - Server (%1s)", Constants.VERSION, lab.getLabDescription())); QueuePanel queuePanel = new QueuePanel(); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(frame.getContentPane()); frame.getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(queuePanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(mapPanel, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); layout.setVerticalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(queuePanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(mapPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); frame.pack(); frame.setVisible(true); // 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); } }); new ApplicationHandler(processor).start(); new BroadcastResponder(labName).start(); } }
Show line notes below