Skip to content

Commit

Permalink
Initial networking setup is complete. At the moment, received current…
Browse files Browse the repository at this point in the history
… and desired temperatures are just spat out to the console.
  • Loading branch information
pid1 committed Feb 7, 2015
1 parent 1263216 commit ae1494e
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 34 deletions.
Binary file removed bin/networking/Network.class
Binary file not shown.
Binary file added bin/networking/networking.class
Binary file not shown.
Binary file added bin/testing/Networking_Test.class
Binary file not shown.
34 changes: 0 additions & 34 deletions src/networking/Network.java

This file was deleted.

87 changes: 87 additions & 0 deletions src/networking/networking.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package networking;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public interface networking {
// Listen for the current temperature on port 7000
static final int receiveCurrentTempPORT = 7000;
// Send the current temperature on port 7000
static final int sendCurrentTempPORT = 7000;
// Listen for the desired temperature on port 7001
static final int receiveDesiredTempPORT = 7001;
// Send the desired temperature on port 7001
static final int sendDesiredTempPORT = 7001;

// 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:", currentTemperature);
} finally {
socket.close();
}
}
}
finally {
listener.close();
}
}

// Listens on receiveDesiredTempPORT and outputs the received string to console.
public static void receiveDesiredTemp() throws Exception {
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:", currentTemperature);
} finally {
socket.close();
}
}
}
finally {
listener.close();
}
}

// Sends the current temperature on port sendCurrentTempPORT.
public static void sendCurrentTemp(int currentTemperature, String thermostatIP) throws Exception {
Socket sender = new Socket(thermostatIP, sendCurrentTempPORT);
try {
while (true) {
PrintWriter out = new PrintWriter(sender.getOutputStream(), true);
out.println(currentTemperature);
}
} finally {
sender.close();
}
}

// Sends the desired temperature on port sendCurrentTempPORT.
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();
}
}
}
13 changes: 13 additions & 0 deletions src/testing/Networking_Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package testing;

import networking.networking;

public class Networking_Test implements networking {
public void Networking_test() {
this.serverCurrentTemperatureListener();
}

public void serverCurrentTemperatureListener() {
this.serverCurrentTemperatureListener();
}
}

0 comments on commit ae1494e

Please sign in to comment.