Skip to content

Commit

Permalink
Began multicast client discovery implementation, and started linking …
Browse files Browse the repository at this point in the history
…network communication to database update code.
  • Loading branch information
pid1 committed Apr 20, 2015
1 parent c78539f commit 2ca88b0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 26 deletions.
41 changes: 19 additions & 22 deletions src/networking/ServerNetworking.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.ServerSocket;
import java.net.Socket;
Expand Down Expand Up @@ -56,32 +57,28 @@ public void sendDesiredTemp(int sendDesiredTempPORT, int desiredTemperature,
}
}

/*
// Receive multicast messages from the thermostats
public void receiveMutlticast(int receiveMulticastPORT) throws Exception {
// Opens the receiving port
MulticastSocket multicastReceiver = new MulticastSocket(receiveMulticastPORT);
// Join the All Hosts network segment, as defined by RFC 5771

multicastReceiver.joinGroup(InetAddress.getByName("224.0.0.1"));
// Instantiate the DatagramPacket and buffer size
DatagramPacket packet;
byte[] buf = new byte[256];
DatagramPacket currentTemperature = new DatagramPacket(buf, buf.length);
try {
while (true) {
// Sets the socket to an accept state
multicastReceiver.receive(currentTemperature);
try {
// Creates a new input stream reader
String received = new String(currentTemperature.getData(), 0, currentTemperature.getLength());
// Parses the input stream
String currentTemperature = String.parseInt(input.readLine());
// Outputs the current temperature to the console
System.out.printf("My current IP is: %d \n",
currentTemperature);
} finally {
socket.close();
}
}
}
finally {
multicastReceiver.close();
while (true) {
// Receive and decode to string
packet = new DatagramPacket(buf, buf.length);
multicastReceiver.receive(packet);
String received = new String(packet.getData(), 0, packet.getLength());
packet.setLength(buf.length);
}
} finally {
// Immediately leave the group and close the socket when finished
multicastReceiver.leaveGroup(InetAddress.getByName("224.0.0.1"));
multicastReceiver.close();
}
} */

}
}
22 changes: 20 additions & 2 deletions src/networking/ThermostatNetworking.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.ServerSocket;
import java.net.Socket;

public class ThermostatNetworking {



// Listens on receiveDesiredTempPORT and outputs the received int to console
public void receiveDesiredTemp(int receiveDesiredTempPORT) throws Exception {
// Open a receiver socket
Expand Down Expand Up @@ -53,5 +54,22 @@ public void sendInfo(int sendCurrentTempPORT, int currentTemperature, String ser
sender.close();
}
}

// Send multicast messages to the server to update the database's IP entry
public void updateIP(int sendMulticastPORT) throws Exception {
// Instantiate the DatagramPacket and buffer size
byte[] buf = new byte[256];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
// Join the All Hosts network segment, as defined by RFC 5771
MulticastSocket socket = new MulticastSocket(sendMulticastPORT);
socket.joinGroup(InetAddress.getByName("224.0.0.1"));
try {
while (true) {
socket.send(packet);
}
} finally {

}
}

}
4 changes: 2 additions & 2 deletions src/server/server.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public String getDevicesFromEmail(String email) {
return null;
}

public void datastructureLogEvent(String query) {
}
public void datastructureLogEvent(String query) {
}


}

0 comments on commit 2ca88b0

Please sign in to comment.