le chie sur celui qui a eu l idee de ne pas gerer les unsigned en java
This commit is contained in:
parent
e190ac3008
commit
d9556c876f
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -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();
|
try {
|
||||||
|
DatagramSocket socket = new DatagramSocket();
|
||||||
while(list.hasMoreElements()) {
|
ByteBuffer buf = ByteBuffer.allocate(19);
|
||||||
NetworkInterface iface = (NetworkInterface) list.nextElement();
|
buf.clear();
|
||||||
|
buf.put((byte)(0x01|0x02));
|
||||||
if(iface == null) continue;
|
|
||||||
//if the interface is not loopback
|
DatagramPacket payload = new DatagramPacket(buf.array(),buf.array().length,InetAddress.getByName(SCGA_MULTICAST_ADDRESS),SGCA_MULTICAST_PORT);
|
||||||
if(!iface.isLoopback() && iface.isUp()) {
|
socket.send(payload);
|
||||||
|
|
||||||
Iterator it = iface.getInterfaceAddresses().iterator();
|
socket.receive(payload);
|
||||||
while (it.hasNext()) {
|
|
||||||
InterfaceAddress address = (InterfaceAddress) it.next();
|
buf = ByteBuffer.wrap(payload.getData());
|
||||||
if(address == null) continue;
|
|
||||||
InetAddress broadcast = address.getBroadcast();
|
if(buf.get() == 3){
|
||||||
if(broadcast != null)
|
System.out.println("--Connection request successful");
|
||||||
{
|
byte address[] = new byte[9];
|
||||||
//we found it !
|
buf = buf.get(address,0,9);
|
||||||
listOfBroadcasts.add(broadcast);
|
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;
|
||||||
} catch (SocketException ex) {
|
System.out.println("----Address : "+sgcaAddress.getHostAddress());
|
||||||
System.err.println("Error while getting network interfaces");
|
System.out.println("----Port : "+port);
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
} catch ( IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue