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