bck
Alejandro Acuña
2024-11-11 f1cb4443aede6d4657bdc3396c8914d3a9f4fa93
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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);
     }
     
     
    
 
     
 
     
}