package test.protocol.musatel;
|
|
|
import art.library.model.devices.sos.Sos;
|
import java.io.ByteArrayInputStream;
|
import java.io.InputStream;
|
import java.io.OutputStream;
|
import java.net.DatagramPacket;
|
import java.net.DatagramSocket;
|
import java.net.InetAddress;
|
import java.net.InetSocketAddress;
|
import java.net.Socket;
|
import java.net.SocketAddress;
|
import java.util.StringTokenizer;
|
|
|
public class ControllerMusatel extends Thread
|
{
|
protected String address = null;
|
protected int port = 3000;
|
protected int timeout = 5000;
|
protected Socket socket;
|
protected InputStream is;
|
protected OutputStream os;
|
public MusatelInputStream input = null;
|
public MusatelOutputStream output = null;
|
|
|
|
public ControllerMusatel (String address)
|
{
|
this.address = address;
|
this.input = new MusatelInputStream(this);
|
this.output = new MusatelOutputStream(this);
|
}
|
|
|
|
|
public void run()
|
{
|
try
|
{
|
DatagramSocket socket = new DatagramSocket(9090);
|
|
byte[] receive = new byte[8192];
|
|
while (true)
|
{
|
DatagramPacket request = new DatagramPacket(receive, receive.length);
|
socket.receive(request);
|
|
if (request.getLength() > 0)
|
{
|
ByteArrayInputStream bis = new ByteArrayInputStream(request.getData(), 0, request.getLength());
|
input.response(bis);
|
bis.close();
|
}
|
}
|
}
|
catch (Exception e)
|
{
|
e.printStackTrace();
|
}
|
}
|
|
|
|
|
|
|
protected void connect () throws Exception
|
{
|
disconnect();
|
|
InetAddress inetAddress = InetAddress.getByName(address);
|
SocketAddress sockaddress = new InetSocketAddress(inetAddress, port);
|
socket = new Socket();
|
socket.connect(sockaddress, 1500);
|
socket.setSoLinger (true, 1);
|
socket.setSoTimeout(timeout);
|
is = socket.getInputStream();
|
os = socket.getOutputStream();
|
}
|
|
|
|
|
protected void disconnect ()
|
{
|
try { os.close(); } catch (Exception e) { }
|
try { socket.close(); } catch (Exception e) { }
|
try { is.close(); } catch (Exception e) { }
|
}
|
|
|
|
|
|
protected byte[] getIpArray (String ip) throws Exception
|
{
|
byte[] addr = new byte[4];
|
StringTokenizer strtok = new StringTokenizer(ip, ".");
|
String s0 = strtok.nextToken();
|
String s1 = strtok.nextToken();
|
String s2 = strtok.nextToken();
|
String s3 = strtok.nextToken();
|
addr[0] = (byte)Integer.parseInt(s0);
|
addr[1] = (byte)Integer.parseInt(s1);
|
addr[2] = (byte)Integer.parseInt(s2);
|
addr[3] = (byte)Integer.parseInt(s3);
|
return(addr);
|
}
|
|
|
|
|
|
|
|
}
|