diff --git a/bin/networking/networking.class b/bin/networking/networking.class index c969b8e..1bcfda2 100644 Binary files a/bin/networking/networking.class and b/bin/networking/networking.class differ diff --git a/src/networking/networking.java b/src/networking/networking.java index c6e7523..2def963 100644 --- a/src/networking/networking.java +++ b/src/networking/networking.java @@ -7,101 +7,135 @@ import java.net.Socket; public interface networking { - // Listen for the current temperature on port 7000 - int receiveCurrentTempPORT = 7000; - // Send the current temperature on port 7000 - int sendCurrentTempPORT = 7000; - // Listen for the desired temperature on port 7001 - int receiveDesiredTempPORT = 7001; - // Send the desired temperature on port 7001 - int sendDesiredTempPORT = 7001; - - // Discover protocol receiving port - int receiveDiscoverPort=7777; - // Discover protocol sending port - int sendDiscoverPORT=7778; - - // Server IP object - String serverIP = new String(); - // Thermostat IP object - String thermostatIP = new String(); - - // Listens on receiveCurrentTempPORT and outputs the received string to console. - public static void receiveCurrentTemp() throws Exception { - ServerSocket listener = new ServerSocket(receiveCurrentTempPORT); - try { - // This stays running continuously once called. - while (true) { - Socket socket = listener.accept(); - try { - BufferedReader input = - new BufferedReader(new InputStreamReader(socket.getInputStream())); - int currentTemperature = Integer.parseInt(input.readLine()); - System.out.printf("The current temperature is: %d \n", currentTemperature); - } finally { - socket.close(); - } - } + + // Listen for the current temperature on port 7000 + int receiveCurrentTempPORT = 7000; + // Send the current temperature on port 7000 + int sendCurrentTempPORT = 7000; + // Listen for the desired temperature on port 7001 + int receiveDesiredTempPORT = 7001; + // Send the desired temperature on port 7001 + int sendDesiredTempPORT = 7001; + + // Discover protocol receiving port + int receiveDiscoverPort=7777; + // Discover protocol sending port + int sendDiscoverPORT=7778; + + // Server IP object + String serverIP = new String(); + // Thermostat IP object + String thermostatIP = new String(); + + // Listens on receiveCurrentTempPORT and outputs the received int to console + public static void receiveCurrentTemp() throws Exception { + // Open a receiver socket + ServerSocket listener = new ServerSocket(receiveCurrentTempPORT); + try { + // This stays running continuously once called + while (true) { + // Sets the socket to an accept state + Socket socket = listener.accept(); + try { + // Creates a new input stream reader + BufferedReader input = new BufferedReader(new InputStreamReader + (socket.getInputStream())); + // Parses the input stream + int currentTemperature = Integer.parseInt(input.readLine()); + // Outputs the current temperature to the console + System.out.printf("The current temperature is: %d \n", + currentTemperature); + } finally { + socket.close(); + } } + } finally { - listener.close(); + listener.close(); } - } - - // Listens on receiveDesiredTempPORT and outputs the received string to console. + } + + // Listens on receiveDesiredTempPORT and outputs the received int to console public static void receiveDesiredTemp() throws Exception { + // Open a receiver socket ServerSocket listener = new ServerSocket(receiveDesiredTempPORT); try { - while (true) { - Socket socket = listener.accept(); - try { - BufferedReader input = - new BufferedReader(new InputStreamReader(socket.getInputStream())); - int currentTemperature = Integer.parseInt(input.readLine()); - System.out.printf("The desired temperature is: %d \n", currentTemperature); - } finally { - socket.close(); - } - } + while (true) { + // Sets the socket to an accept state + Socket socket = listener.accept(); + try { + // Creates a new input stream reader + BufferedReader input = new BufferedReader(new InputStreamReader + (socket.getInputStream())); + // Parses the input stream + int currentTemperature = Integer.parseInt(input.readLine()); + // Outputs the desired temperature to the console + System.out.printf("The desired temperature is: %d \n", + currentTemperature); + } finally { + socket.close(); + } + } } finally { - listener.close(); + listener.close(); } } - - // Sends the current temperature on port sendCurrentTempPORT. - public static void sendCurrentTemp(int currentTemperature, String serverIP) throws Exception { - Socket sender = new Socket(serverIP, sendCurrentTempPORT); - try { - while (true) { - PrintWriter out = new PrintWriter(sender.getOutputStream(), true); - out.println(currentTemperature); - } - } finally { - sender.close(); - } + + // Sends the current temperature on port sendCurrentTempPORT + public static void sendCurrentTemp(int currentTemperature, String serverIP) + throws Exception { + // Open a sending port + Socket sender = new Socket(serverIP, sendCurrentTempPORT); + try { + while (true) { + // Instantiate a printwriter so we can write to the output stream + PrintWriter out = new PrintWriter(sender.getOutputStream(), true); + // Send out the value of currentTemperature + out.println(currentTemperature); + } + } finally { + // Immediately close the socket when finished. + sender.close(); + } } - - // Sends the desired temperature on port sendDesiredTempPORT. - public static void sendDesiredTemp(int desiredTemperature, String thermostatIP) throws Exception { - Socket sender = new Socket(thermostatIP, sendDesiredTempPORT); - try { - while (true) { - PrintWriter out = new PrintWriter(sender.getOutputStream(), true); - out.println(desiredTemperature); - } - } finally { - sender.close(); - } + + // Sends the desired temperature on port sendDesiredTempPORT + public static void sendDesiredTemp(int desiredTemperature, + String thermostatIP) + throws Exception { + // Opens a sending port + Socket sender = new Socket(thermostatIP, sendDesiredTempPORT); + try { + while (true) { + // Instantiate a printwriter so we can write to the output stream + PrintWriter out = new PrintWriter(sender.getOutputStream(), true); + // Send out the value of desiredTemperature + out.println(desiredTemperature); + } + } finally { + // Immediately close the socket when finished + sender.close(); + } } - // This discover protocol finds other instances of Thermocontroller running on the network, and updates the IP addresses accordingly for the server and the thermostat - public static void getServerIP() { - + // This discover protocol finds other instances of Thermocontroller running + // on the network, and updates the IP addresses accordingly + // for the server and the thermostat + public static void monitorServerIP() { + } - - public static void getThermostatIP() { - + + public static void monitorThermostatIP() { + } - + + public static void setDesiredTemp() { + + } + + public static void updateCurrentTemp() { + + } + } \ No newline at end of file