Skip to content

Commit

Permalink
Moving networking code and threading the server's listener.
Browse files Browse the repository at this point in the history
  • Loading branch information
pid1 committed Apr 27, 2015
1 parent 169fc44 commit 372841e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/networking/ServerNetworking.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

public class ServerNetworking {

String thermostatIP;
String thermostatIP;

// Listens on receiveCurrentTempPORT and outputs the received int to console
public void receiveCurrentTemp() throws Exception {
Expand Down
4 changes: 3 additions & 1 deletion src/networking/ThermostatNetworking.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void receiveDesiredTemp() throws Exception {
}

// Sends the current temperature on port sendCurrentTempPORT
public void sendInfo(int currentTemperature)
public void sendInfo(int currentTemperature, String serverIP)
throws Exception {
// Open a sending port
Socket sender = new Socket(serverIP, 7000);
Expand All @@ -57,6 +57,7 @@ public void sendInfo(int currentTemperature)
}
}

/*
// Send multicast messages to the server to update the database's IP entry
public void updateIP() throws Exception {
// Instantiate the DatagramPacket and buffer size
Expand All @@ -73,5 +74,6 @@ public void updateIP() throws Exception {
}
}
*/

}
65 changes: 55 additions & 10 deletions src/server/server.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package server;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Hashtable;

import InputOutput.*;
Expand All @@ -8,9 +12,13 @@
public class server extends ServerNetworking implements Thermostatable, Serializable {
private static server instance = null;

public static server getInstance(){
public static server getInstance() {
if (instance == null){
instance = new server();
try {
instance = new server();
} catch (Exception e) {
e.printStackTrace();
}
}
return instance;
}
Expand All @@ -21,18 +29,18 @@ public static server getInstance(){
String thermostatIP;
// Database object
Database dataStorage;
// Threaded network listener so we don't block other operations
Thread serverListenerThread;

public server() {
public server() throws Exception {
// Default admin user
String adminpassword = "admin";
String ip = "127.0.0.1";
String email = "admin@uconn.edu";
String serialnumber = "1.1";
Database dataStorage = new Database(this);
dataStorage.createUser(email, adminpassword, ip, null);
}

public void receiveCurrentTemp() throws Exception {
super.receiveCurrentTemp();
// this.startServerListenerThread();
}

public boolean createUser(String email, String password, String ip, String serialpermission) {
Expand Down Expand Up @@ -73,9 +81,46 @@ public void setDesiredTempForSerialNumber(String serialnumber, String desiredtem
public String getDevicesFromEmail(String email) {
return null;
}

public void datastructureLogEvent(String query) {
}

public void sendDesiredTemp(int desiredTemperature, String thermostatIP) {
this.sendDesiredTemp(desiredTemperature, thermostatIP);
}
// Threading
/*
public void startServerListenerThread() {
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);
} finally {
socket.close();
}
}
} finally {
listener.close();
}
}
} finally {
}
}
};
}
*/
public void datastructureLogEvent(String query) {
}


}
6 changes: 0 additions & 6 deletions src/thermostat/ThermostatDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,4 @@ public void actionPerformed(ActionEvent e) {
}
}

/*
public String getServerIP() {
return
}
*/

}

0 comments on commit 372841e

Please sign in to comment.