Skip to content

Commit

Permalink
Completed multithreading for the receivers in ThermostatDevice and Se…
Browse files Browse the repository at this point in the history
…rver. ThermostatDevice's sendInfo method now correctly takes a String as a parameter, and then converts to an int for sending (Thanks David). Both the server and thermostat start receiver threads when instantiated.
  • Loading branch information
pid1 committed Apr 27, 2015
1 parent 372841e commit c4177b8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 38 deletions.
2 changes: 2 additions & 0 deletions src/networking/ServerNetworking.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void sendDesiredTemp(int desiredTemperature, String thermostatIP)
}
}

/*
// Receive multicast messages from the thermostats
public void receiveMutlticast() throws Exception {
// Opens the receiving port
Expand All @@ -80,4 +81,5 @@ public void receiveMutlticast() throws Exception {
multicastReceiver.close();
}
}
*/
}
34 changes: 3 additions & 31 deletions src/networking/ThermostatNetworking.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,18 @@
import java.net.Socket;

public class ThermostatNetworking {

String serverIP;

// Listens on receiveDesiredTempPORT and outputs the received int to console
public void receiveDesiredTemp() throws Exception {
// Open a receiver socket
ServerSocket listener = new ServerSocket(7001);
try {
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();
}
}

// Sends the current temperature on port sendCurrentTempPORT
public void sendInfo(int currentTemperature, String serverIP)
throws Exception {
public void sendInfo(String currentTemperature, String serverIP) throws Exception {
int currentTempInt = Integer.parseInt(currentTemperature);
// Open a sending port
Socket sender = new Socket(serverIP, 7000);
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);
out.println(currentTempInt);
}
} finally {
// Immediately close the socket when finished.
Expand Down
21 changes: 14 additions & 7 deletions src/server/server.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.ServerSocket;
Expand Down Expand Up @@ -40,7 +41,7 @@ public server() throws Exception {
String serialnumber = "1.1";
Database dataStorage = new Database(this);
dataStorage.createUser(email, adminpassword, ip, null);
// this.startServerListenerThread();
this.startServerListenerThread();
}

public boolean createUser(String email, String password, String ip, String serialpermission) {
Expand Down Expand Up @@ -85,8 +86,9 @@ public String getDevicesFromEmail(String email) {
public void sendDesiredTemp(int desiredTemperature, String thermostatIP) {
this.sendDesiredTemp(desiredTemperature, thermostatIP);
}
// Threading
/*

// Threaded receiver function
// Listens on 7000 and outputs the received int to console
public void startServerListenerThread() {
Runnable runnable = new Runnable() {
public void run() {
Expand All @@ -102,8 +104,10 @@ public void run() {
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 and writes to disk
System.out.printf("The desired temperature is: %d \n", currentTemperature);
// Outputs the current temperature to the console and writes to disk
System.out.printf("The current temperature is: %d \n", currentTemperature);
String currentTempString = Integer.toString(currentTemperature);
//this.setCurrentTempForSerialNumber();
} finally {
socket.close();
}
Expand All @@ -112,13 +116,16 @@ public void run() {
listener.close();
}
}
} finally {
} catch (IOException e) {
e.printStackTrace();
}
finally {

}
}
};
}
*/

public void datastructureLogEvent(String query) {
}

Expand Down
44 changes: 44 additions & 0 deletions src/thermostat/ThermostatDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -37,6 +41,7 @@ public ThermostatDevice(Integer updateIntervalInSeconds){

this.clock = new Timer(updateIntervalInSeconds*1000, this);
this.clock.start();
this.startThermostatListenerThread();

}

Expand All @@ -61,5 +66,44 @@ public void actionPerformed(ActionEvent e) {
sensor.requestNewDataPointReading();
}
}

// Threaded receiver function
// Listens on 7001 and outputs the received int to console
public void startThermostatListenerThread() {
Runnable runnable = new Runnable() {
public void run() {
try {
ServerSocket listener = new ServerSocket(7001);
while (true) {
try {
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 and writes to disk
System.out.printf("The desired temperature is: %d \n", currentTemperature);
String currentTempString = Integer.toString(currentTemperature);
//this.setCurrentTempForSerialNumber();
} finally {
socket.close();
}
}
} finally {
listener.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
finally {

}
}
};
}

}

0 comments on commit c4177b8

Please sign in to comment.