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 {
list = NetworkInterface.getNetworkInterfaces();
while(list.hasMoreElements()) { try {
NetworkInterface iface = (NetworkInterface) list.nextElement(); DatagramSocket socket = new DatagramSocket();
ByteBuffer buf = ByteBuffer.allocate(19);
buf.clear();
buf.put((byte)(0x01|0x02));
if(iface == null) continue; DatagramPacket payload = new DatagramPacket(buf.array(),buf.array().length,InetAddress.getByName(SCGA_MULTICAST_ADDRESS),SGCA_MULTICAST_PORT);
//if the interface is not loopback socket.send(payload);
if(!iface.isLoopback() && iface.isUp()) {
Iterator it = iface.getInterfaceAddresses().iterator(); socket.receive(payload);
while (it.hasNext()) {
InterfaceAddress address = (InterfaceAddress) it.next();
if(address == null) continue;
InetAddress broadcast = address.getBroadcast();
if(broadcast != null)
{
//we found it !
listOfBroadcasts.add(broadcast);
}
}
}
}
} catch (SocketException ex) {
System.err.println("Error while getting network interfaces");
return;
}
Integer sgcaPort; buf = ByteBuffer.wrap(payload.getData());
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);
} if(buf.get() == 3){
data = new byte[1]; System.out.println("--Connection request successful");
packet = new DatagramPacket(data,data.length); byte address[] = new byte[9];
broadcastSocket.receive(packet); buf = buf.get(address,0,9);
sgcaAddress = packet.getAddress(); String addressString = new String(address);
Byte b = new Byte(packet.getData()[0]); InetAddress sgcaAddress = InetAddress.getByName(addressString);
sgcaPort = b.intValue(); buf = buf.get(address,0,7);
} catch (Exception ex) { short shortVal = buf.getShort();
Logger.getLogger(CommandTerminal.class.getName()).log(Level.SEVERE, null, ex); int port = shortVal >= 0 ? shortVal : 0x10000 + shortVal;
return; System.out.println("----Address : "+sgcaAddress.getHostAddress());
} System.out.println("----Port : "+port);
}
//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 } catch ( IOException e) {
comSock.receive(packet); // TODO Auto-generated catch block
Byte response = new Byte(packet.getData()[0]); e.printStackTrace();
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
} }