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;
import DatagramSocket.AsynchronousDatagramSocket;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
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 {
private final static int SGCA_BROADCAST_PORT = 9999;
private final static int MAX_MESSAGE_SIZE = 300;
private static AsynchronousDatagramSocket socket;
private final static int SGCA_MULTICAST_PORT = 4446;
private final static String SCGA_MULTICAST_ADDRESS = "224.0.0.3";
/**
* @param args the command line arguments
*/
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>();
Enumeration list;
try {
list = NetworkInterface.getNetworkInterfaces();
/*
* Handshake
*/
while(list.hasMoreElements()) {
NetworkInterface iface = (NetworkInterface) list.nextElement();
try {
DatagramSocket socket = new DatagramSocket();
ByteBuffer buf = ByteBuffer.allocate(19);
buf.clear();
buf.put((byte)(0x01|0x02));
if(iface == null) continue;
//if the interface is not loopback
if(!iface.isLoopback() && iface.isUp()) {
DatagramPacket payload = new DatagramPacket(buf.array(),buf.array().length,InetAddress.getByName(SCGA_MULTICAST_ADDRESS),SGCA_MULTICAST_PORT);
socket.send(payload);
Iterator it = iface.getInterfaceAddresses().iterator();
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;
}
socket.receive(payload);
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);
buf = ByteBuffer.wrap(payload.getData());
}
data = new byte[1];
packet = new DatagramPacket(data,data.length);
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;
}
if(buf.get() == 3){
System.out.println("--Connection request successful");
byte address[] = new byte[9];
buf = buf.get(address,0,9);
String addressString = new String(address);
InetAddress sgcaAddress = InetAddress.getByName(addressString);
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);
}
//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()];
} catch ( IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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
}