package anywheresoftware.b4a.sample;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.ShortName;
import java.io.IOException;
import java.net.*;
import java.util.Timer;
import java.util.TimerTask;
@Events(values={"packetreceived (result As Int)"})
@ShortName("UdpMulticastSocket")
public class UdpMulticastSocket {
// private String _eventName;
public int CurrentPort;
int len;
String eventName;
MulticastSocket serverSocket;
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
BA baevent;
public void initialize(BA ba,String _eventName,int newport ,String ipadresse) throws Exception
{
baevent = ba;
InetAddress groupeIP = InetAddress.getByName(ipadresse);
serverSocket = new MulticastSocket(newport);
serverSocket.joinGroup(groupeIP);
eventName = _eventName;
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run()
{
byte[] receiveData = new byte[1024];
try {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
// String sentence = new String( receivePacket.getData());
len = receivePacket.getLength();
if (len > 0)
{
int result;
result = 15;
baevent.raiseEventFromDifferentThread(this, null, 0, eventName + "_packetreceived(result As Int)", false, new Object[]{result});
// baevent.raiseEvent(this, eventName + "_packetreceived (result As Int)", new Object[]{result});
/*
if (len == 106-42)
sendstatustype1("239.255.36.93",receivePacket.getPort());
if (len == 91-42)
sendstatustype1("239.255.36.93",receivePacket.getPort());
else if (len == 90-42)
sendstatustype2("239.255.36.93",3694);
else if (len == 78-42)
sendstatustype1("239.255.36.93",receivePacket.getPort()); */
}
} catch (IOException e) {
}
}
}, 0, 1);
/*
while(true)
{
}*/
}
int compt = 0;
int comptid = 0x10;
public void send(String Message,String ipadresse,int port) throws IOException
{
InetAddress groupeIP = InetAddress.getByName(ipadresse);
DatagramPacket messagetosend = new DatagramPacket(Message.getBytes(), Message.length(),groupeIP , port);
serverSocket.send(messagetosend);
}
public void sendstatustype1(String ipadresse,int port) throws IOException
{
byte[] message = new byte[1024];
int compt = 0;
message[compt] = (byte)0x41; compt++;
message[compt] = (byte)0x44; compt++;
message[compt] = (byte)0x4e; compt++;
message[compt] = (byte)0x2d; compt++;
message[compt] = (byte)0x4e; compt++;
message[compt] = (byte)0x45; compt++;
message[compt] = (byte)0x54; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x02; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte) comptid++; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x08; compt++;
message[compt] = (byte)0x02; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte) 0xff; compt++;
message[compt] = (byte) 0x89; compt++;
message[compt] = (byte) 0xbc; compt++;
message[compt] = (byte)0x77; compt++;
message[compt] = (byte)0x37; compt++;
message[compt] = (byte) 0xdd; compt++;
message[compt] = (byte) 0xf7; compt++;
message[compt] = (byte)0x01; compt++;
message[compt] = (byte)0x45; compt++;
message[compt] = (byte)0x44; compt++;
message[compt] = (byte)0x54; compt++;
message[compt] = (byte)0x54; compt++;
message[compt] = (byte)0x36; compt++;
message[compt] = (byte)0x32; compt++;
message[compt] = (byte)0x30; compt++;
message[compt] = (byte)0x30; compt++;
message[compt] = (byte) 0xc0; compt++;
message[compt] = (byte) 0xa8; compt++;
message[compt] = (byte)0x01; compt++;
message[compt] = (byte)0x63; compt++;
message[compt] = (byte)0x0e; compt++;
message[compt] = (byte)0x6e; compt++;
message[compt] = (byte)0x75; compt++;
message[compt] = (byte)0x45; compt++;
message[compt] = (byte) 0x8b; compt++;
message[compt] = (byte)0x10; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte) 0x01; compt++;
message[compt] = (byte) 0x02; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x01; compt++;
message[compt] = (byte)0x16; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x01; compt++;
message[compt] = (byte)0x18; compt++;
message[compt] = (byte)0x02; compt++;
message[compt] =(byte) 0x06; compt++;
InetAddress groupeIP = InetAddress.getByName(ipadresse);
DatagramPacket messagetosend = new DatagramPacket(message, compt,groupeIP , port);
serverSocket.send(messagetosend);
}
public void RequestStatus() throws IOException
{
String ipadresse = "239.255.36.93";
int port = 3693;
byte[] message = new byte[1024];
int compt = 0;
message[compt] = (byte)0x41; compt++;
message[compt] = (byte)0x44; compt++;
message[compt] = (byte)0x4e; compt++;
message[compt] = (byte)0x2d; compt++;
message[compt] = (byte)0x4e; compt++;
message[compt] = (byte)0x45; compt++;
message[compt] = (byte)0x54; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x02; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte) 0xB6; compt++;
message[compt] = (byte)0x06; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0xff; compt++;
message[compt] = (byte)0x89; compt++;
message[compt] = (byte)0xbc; compt++;
message[compt] = (byte)0x77; compt++;
message[compt] = (byte)0x37; compt++;
message[compt] = (byte)0xdd; compt++;
message[compt] = (byte)0xf7; compt++;
message[compt] = (byte)0x01; compt++;
message[compt] = (byte) 0xff; compt++;
message[compt] = (byte) 0xff; compt++;
message[compt] = (byte) 0xff; compt++;
message[compt] = (byte)0xff; compt++;
message[compt] = (byte)0xff; compt++;
message[compt] = (byte) 0xff; compt++;
message[compt] = (byte) 0xff; compt++;
message[compt] = (byte)0xff; compt++;
message[compt] = (byte)0x13; compt++;
message[compt] = (byte)0xfd; compt++;
message[compt] = (byte)0x61; compt++;
message[compt] = (byte)0xf2; compt++;
InetAddress groupeIP2 = InetAddress.getByName(ipadresse);
DatagramPacket messagetosend2 = new DatagramPacket(message, compt,groupeIP2 , port);
serverSocket.send(messagetosend2);
}
public void sendstatustype2(String ipadresse,int port) throws IOException
{
byte[] message = new byte[1024];
int compt = 0;
compt = 0;
message[compt] = (byte)0x41; compt++;
message[compt] = (byte)0x44; compt++;
message[compt] = (byte)0x4e; compt++;
message[compt] = (byte)0x2d; compt++;
message[compt] = (byte)0x4e; compt++;
message[compt] = (byte)0x45; compt++;
message[compt] = (byte)0x54; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x02; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte) comptid++; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x08; compt++;
message[compt] = (byte)0x02; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte) 0xff; compt++;
message[compt] = (byte) 0x89; compt++;
message[compt] = (byte) 0xbc; compt++;
message[compt] = (byte)0x77; compt++;
message[compt] = (byte)0x37; compt++;
message[compt] = (byte) 0xdd; compt++;
message[compt] = (byte) 0xf7; compt++;
message[compt] = (byte)0x01; compt++;
message[compt] = (byte)0xFe; compt++;
message[compt] = (byte)0xfd; compt++;
message[compt] = (byte)0xfa; compt++;
message[compt] = (byte)0x01; compt++;
message[compt] = (byte)0x11; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x04; compt++;
message[compt] = (byte)0x55; compt++;
message[compt] = (byte) 0x00; compt++;
message[compt] = (byte) 0x04; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x07; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x14; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte) 0x0d; compt++;
message[compt] = (byte)0x36; compt++;
message[compt] = (byte)0x32; compt++;
message[compt] = (byte)0x30; compt++;
message[compt] = (byte)0x30; compt++;
message[compt] = (byte)0x45; compt++;
message[compt] = (byte) 0x44; compt++;
message[compt] = (byte) 054; compt++;
message[compt] = (byte)0x54; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x0; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0x00; compt++;
message[compt] = (byte)0xec; compt++;
message[compt] =(byte) 0x3a; compt++;
InetAddress groupeIP2 = InetAddress.getByName(ipadresse);
DatagramPacket messagetosend2 = new DatagramPacket(message, compt,groupeIP2 , port);
serverSocket.send(messagetosend2);
}
}