Skip to content

Commit

Permalink
Added proper documentation and cleaned up the code.
Browse files Browse the repository at this point in the history
  • Loading branch information
pid1 committed Feb 8, 2015
1 parent 209cb12 commit 0ae5b98
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 82 deletions.
Binary file modified bin/networking/networking.class
Binary file not shown.
198 changes: 116 additions & 82 deletions src/networking/networking.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

}

}

0 comments on commit 0ae5b98

Please sign in to comment.