This commit is contained in:
xdrm-brackets 2017-04-13 20:02:06 +02:00
commit e270e47bc1
6 changed files with 41 additions and 91 deletions

BIN
central-manager/boot Executable file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -6,19 +6,13 @@
package commandterminal; package commandterminal;
import DatagramSocket.AsynchronousDatagramSocket; import java.io.IOException;
import java.net.DatagramPacket; import java.net.DatagramPacket;
import java.net.DatagramSocket; import java.net.DatagramSocket;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException; import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
/** /**
* *
@ -26,96 +20,52 @@ import java.util.logging.Logger;
*/ */
public class CommandTerminal { public class CommandTerminal {
private final static int SGCA_BROADCAST_PORT = 9999; private final static int SGCA_MULTICAST_PORT = 4446;
private final static int MAX_MESSAGE_SIZE = 300; private final static String SCGA_MULTICAST_ADDRESS = "224.0.0.3";
private static AsynchronousDatagramSocket socket;
/** /**
* @param args the command line arguments * @param args the command line arguments
*/ */
public static void main(String[] args){ public static void main(String[] args){
//the following code go through every interfaces to get broadcasts addresses in order to contact the sgca /*
HashSet<InetAddress> listOfBroadcasts = new HashSet<InetAddress>(); * Handshake
Enumeration list; */
try { try {
list = NetworkInterface.getNetworkInterfaces(); DatagramSocket socket = new DatagramSocket();
ByteBuffer buf = ByteBuffer.allocate(19);
buf.clear();
buf.put((byte)(0x01|0x02));
while(list.hasMoreElements()) { DatagramPacket payload = new DatagramPacket(buf.array(),buf.array().length,InetAddress.getByName(SCGA_MULTICAST_ADDRESS),SGCA_MULTICAST_PORT);
NetworkInterface iface = (NetworkInterface) list.nextElement(); socket.send(payload);
if(iface == null) continue; socket.receive(payload);
//if the interface is not loopback
if(!iface.isLoopback() && iface.isUp()) {
Iterator it = iface.getInterfaceAddresses().iterator(); buf = ByteBuffer.wrap(payload.getData());
while (it.hasNext()) {
InterfaceAddress address = (InterfaceAddress) it.next(); if(buf.get() == 3){
if(address == null) continue; System.out.println("--Connection request successful");
InetAddress broadcast = address.getBroadcast(); byte address[] = new byte[9];
if(broadcast != null) buf = buf.get(address,0,9);
{ String addressString = new String(address);
//we found it ! InetAddress sgcaAddress = InetAddress.getByName(addressString);
listOfBroadcasts.add(broadcast); buf = buf.get(address,0,7);
} short shortVal = buf.getShort();
} int port = shortVal >= 0 ? shortVal : 0x10000 + shortVal;
} System.out.println("----Address : "+sgcaAddress.getHostAddress());
} System.out.println("----Port : "+port);
} catch (SocketException ex) {
System.err.println("Error while getting network interfaces");
return;
} }
Integer sgcaPort;
InetAddress sgcaAddress;
DatagramPacket packet;
try {
//try to send a request to all the broadcasts (usually there is only one) and wait for the response of the sgca
DatagramSocket broadcastSocket = new DatagramSocket();
byte[] data;
for(InetAddress adr : listOfBroadcasts){
data = new byte[1];
Integer clientType = 1;
data[0] = clientType.byteValue();
packet = new DatagramPacket(data,data.length,adr,SGCA_BROADCAST_PORT);
broadcastSocket.send(packet);
} } catch ( IOException e) {
data = new byte[1]; // TODO Auto-generated catch block
packet = new DatagramPacket(data,data.length); e.printStackTrace();
broadcastSocket.receive(packet);
sgcaAddress = packet.getAddress();
Byte b = new Byte(packet.getData()[0]);
sgcaPort = b.intValue();
} catch (Exception ex) {
Logger.getLogger(CommandTerminal.class.getName()).log(Level.SEVERE, null, ex);
return;
} }
//from now we assume that the network discovery part went well and we have the port/address of the sgca
//send a ISALIVE to ensure the sgca is ready to receive commands
DatagramSocket comSock;
ByteBuffer buf = ByteBuffer.allocate(1);
try {
comSock = new DatagramSocket(sgcaPort);
buf.putInt(OpCode.ISALIVE.ordinal());
packet = new DatagramPacket(buf.array(),buf.arrayOffset(),sgcaAddress,sgcaPort);
comSock.send(packet);
//receive response
comSock.receive(packet);
Byte response = new Byte(packet.getData()[0]);
OpCode success = OpCode.values()[response.intValue()];
if(success != OpCode.SUCCESS){
System.out.println("SGCA Not ready, exiting");
return;
}
} catch (Exception ex) {
Logger.getLogger(CommandTerminal.class.getName()).log(Level.SEVERE, null, ex);
}
//now the sgca is connected and ready, let's start the real work
} }