Tested C++ RPS/RMI libraries for arbitrary objects and arbitrary remote calls
[iot2.git] / iotjava / iotruntime / IoTUDP.java
1 package iotruntime;
2
3 // Java packages
4 import java.io.IOException;
5 import java.net.DatagramPacket;
6 import java.net.DatagramSocket;
7 import java.net.InetAddress;
8 import java.net.SocketException;
9 import java.net.UnknownHostException;
10
11 import iotruntime.slave.IoTDeviceAddress;
12
13 /** Class IoTUDP is a wrapper class that provides
14  *  minimum interfaces for user to interact with IoT
15  *  devices in our system - adapted from my colleague's
16  *  work (Ali Younis - ayounis @ uci.edu)
17  *
18  * @author      Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
19  * @version     1.0
20  * @since       2016-02-20
21  */
22 public class IoTUDP {
23
24         /**
25          * IoTUDP class properties
26          */
27         private final String strHostAddress;
28         private final int iSrcPort;
29         private final int iDstPort;
30         private DatagramSocket socket;  // the socket interface that we are guarding
31         private boolean didClose;               // make sure that the clean up was done correctly
32
33         /**
34          * Class constructor
35          */
36         public IoTUDP(IoTDeviceAddress iotDevAdd) throws SocketException, IOException {
37
38                 strHostAddress = iotDevAdd.getHostAddress();
39                 iSrcPort = iotDevAdd.getSourcePortNumber();
40                 iDstPort = iotDevAdd.getDestinationPortNumber();
41
42                 socket = new DatagramSocket(iSrcPort);
43                 didClose = false;
44         }
45
46         /**
47          * sendData() method
48          *
49          * @param  bData     Byte type that passes the data to be sent
50          * @return void
51          */
52         public void sendData(byte[] bData) throws UnknownHostException, IOException {
53
54                 DatagramPacket dpSendPacket = new DatagramPacket(bData, bData.length, InetAddress.getByName(strHostAddress), iDstPort);
55                 socket.send(dpSendPacket);
56         }
57
58         /**
59          * recieveData() method
60          *
61          * @param  iMaxDataLength  Integer maximum data length as reference
62          * @return byte[]
63          */
64         public byte[] recieveData(int iMaxDataLength) throws IOException {
65
66                 byte[] bReceiveData = new byte[iMaxDataLength];
67                 DatagramPacket dpReceivePacket = new DatagramPacket(bReceiveData, bReceiveData.length);
68                 socket.receive(dpReceivePacket);
69
70                 return dpReceivePacket.getData();
71         }
72
73         /**
74          * setSoTimeout() method
75          *
76          * @param  iTimeout  Integer timeout time
77          */
78         public void setSoTimeout(int iTimeout) throws SocketException {
79
80                 socket.setSoTimeout(iTimeout);
81
82         }
83
84         /**
85          * setSendBufferSize() method
86          *
87          * @param  iSize  Integer buffer size
88          */
89         public void setSendBufferSize(int iSize) throws SocketException {
90
91                 socket.setSendBufferSize(iSize);
92
93         }
94
95         /**
96          * setReceiveBufferSize() method
97          *
98          * @param  iSize  Integer buffer size
99          */
100         public void setReceiveBufferSize(int iSize) throws SocketException {
101
102                 socket.setReceiveBufferSize(iSize);
103
104         }
105
106
107         /**
108          * close() method
109          */
110         public void close() {
111
112                 socket.close();
113                 didClose = true;
114
115         }
116
117         /**
118          * close() called by the garbage collector right before trashing object
119          */
120         public void finalize() throws SocketException {
121
122                 if (!didClose) {
123                         close();
124                         throw new SocketException("Socket not closed before object destruction, must call close method.");
125                 }
126
127         }
128 }