123 lines
4.4 KiB
Java
123 lines
4.4 KiB
Java
|
/*
|
||
|
* To change this license header, choose License Headers in Project Properties.
|
||
|
* To change this template file, choose Tools | Templates
|
||
|
* and open the template in the editor.
|
||
|
*/
|
||
|
|
||
|
package commandterminal;
|
||
|
|
||
|
import DatagramSocket.AsynchronousDatagramSocket;
|
||
|
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.nio.ByteBuffer;
|
||
|
import java.util.Enumeration;
|
||
|
import java.util.HashSet;
|
||
|
import java.util.Iterator;
|
||
|
import java.util.logging.Level;
|
||
|
import java.util.logging.Logger;
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @author lmascaro
|
||
|
*/
|
||
|
public class CommandTerminal {
|
||
|
|
||
|
private final static int SGCA_BROADCAST_PORT = 9999;
|
||
|
private final static int MAX_MESSAGE_SIZE = 300;
|
||
|
private static AsynchronousDatagramSocket socket;
|
||
|
|
||
|
/**
|
||
|
* @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();
|
||
|
|
||
|
while(list.hasMoreElements()) {
|
||
|
NetworkInterface iface = (NetworkInterface) list.nextElement();
|
||
|
|
||
|
if(iface == null) continue;
|
||
|
//if the interface is not loopback
|
||
|
if(!iface.isLoopback() && iface.isUp()) {
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
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);
|
||
|
|
||
|
}
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
//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
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|