Updating the first benchmark with new stubs/skeletons; somehow motion detection is...
[iot2.git] / iotjava / iotrmi / Java / IoTSocket.java
1 package iotrmi.Java;
2
3 // Java libraries
4 import java.io.*;
5 import java.net.*;
6 import java.awt.*;
7 import java.util.*;
8 import java.nio.ByteBuffer;
9
10 import java.util.concurrent.Semaphore;
11
12
13 /** Class IoTSocket is the basic class for IoT RMI
14  *  socket communication. This class will be extended
15  *  by both IoTSocketServer and IoTSocketClient
16  *  <p>
17  *  Adapted from Java/C++ socket implementation
18  *  by Keith Vertanen
19  *  @see        <a href="https://www.keithv.com/software/socket/</a>
20  *
21  * @author      Rahmadi Trimananda <rtrimana @ uci.edu>
22  * @version     1.0
23  * @since       2016-08-17
24  */
25 public abstract class IoTSocket {
26
27         /**
28          * Class Properties
29          */
30         protected byte data[];
31         protected int localPort;
32         protected int port;
33         protected Socket sock;
34         protected BufferedInputStream input;
35         protected BufferedOutputStream output;
36
37         //protected static Semaphore sendRecvMutex = new Semaphore(1);
38
39         /**
40          * Class Constant
41          */
42         protected static int BUFFSIZE = 128000; // how many bytes our incoming buffer can hold (original)
43         //protected static int BUFFSIZE = 8388608;      // 8388608 = 2^23 bytes of memory (8MB) - this is required by our IHome speaker driver
44         protected static int MSG_LEN_SIZE = 4;  // send length in the size of integer (4 bytes)
45
46         /**
47          * Default constructor
48          */
49         protected IoTSocket(int _port) throws IOException
50         {
51                 localPort = 0;
52                 port = _port;
53                 data = new byte[BUFFSIZE];
54         }
55         
56          
57         protected IoTSocket(int _localPort, int _port) throws IOException
58         {
59                 localPort = _localPort;
60                 port = _port;
61                 data = new byte[BUFFSIZE];
62         }
63
64
65         /**
66          * sendBytes() sends an array of bytes
67          */
68         public synchronized void sendBytes(byte vals[]) throws IOException
69         {
70                 int len = vals.length;
71                 // Write the length first - convert to array of 4 bytes
72                 ByteBuffer bb = ByteBuffer.allocate(MSG_LEN_SIZE);
73                 bb.putInt(len);
74                 output.write(bb.array(), 0, MSG_LEN_SIZE);
75                 //System.out.println("Sender about to send: " + Arrays.toString(bb.array()));
76                 output.flush();
77                 // Write the byte array
78                 output.write(vals, 0, len);
79                 //System.out.println("Sender sending: " + len);
80                 output.flush();
81                 //System.out.println("Sender about to receive ACK!");
82                 receiveAck();
83                 //System.out.println("Sender about to send ACK!\n\n");
84                 sendAck();
85         }
86
87
88         /**
89          * receiveBytes() receives an array of bytes
90          */
91         public synchronized byte[] receiveBytes(byte val[]) throws IOException
92         {
93                 int i;
94                 int totalbytes = 0;
95                 int numbytes;
96
97                 // Wait until input is available
98                 if (input.available() == 0) {
99                         return null;
100                 }
101
102                 //System.out.println("Receiver about to receive: " + input.available());
103                 // Read the maxlen first - read 4 bytes here
104                 byte[] lenBytes = new byte[MSG_LEN_SIZE];
105                 input.read(lenBytes, 0, MSG_LEN_SIZE);
106                 //System.out.println("Receiver lenBytes: " + Arrays.toString(lenBytes));
107                 int maxlen = ByteBuffer.wrap(lenBytes).getInt();
108                 //System.out.println("Receiver received length: " + maxlen);
109                 // Receive until maxlen
110                 if (maxlen>BUFFSIZE) {
111                         System.out.println("IoTSocketClient/Server: Sending more bytes then will fit in buffer! Number of bytes: " + maxlen);
112                         // Allocate a bigger array when needed
113                         int newLen = 2;
114                         while (newLen < maxlen) // Shift until we get a new buffer size that's bigger than maxLen (basically power of 2)
115                                 newLen = newLen << 1;
116                         System.out.println("IoTSocketClient/Server: Allocating a bigger buffer now with size: " + newLen);
117                         BUFFSIZE = newLen;
118                         data = new byte[BUFFSIZE];
119                 }
120                 val = new byte[maxlen];
121                 while (totalbytes < maxlen)
122                 {
123                         numbytes = input.read(data);
124                         // copy the bytes into the result buffer
125                         for (i=totalbytes; i<totalbytes+numbytes; i++)
126                                 val[i] = data[i-totalbytes];
127                         totalbytes += numbytes;
128                 }
129                 // we now send an acknowledgement to the server to let them
130                 // know we've got it
131                 //System.out.println("Receiver about to send ACK!");
132                 sendAck();
133                 //System.out.println("Receiver about to receive ACK!\n\n");
134                 receiveAck();
135
136                 return val;
137         }
138
139
140         /**
141          * Close socket connection
142          */
143         public void close() throws IOException
144         {
145                 sock.close();
146         }
147
148
149         /**
150          * Send ACK
151          */
152         public synchronized void sendAck() throws IOException
153         {
154                 int ack;
155                 ack = 0;
156                 output.write(ack);
157                 output.flush();
158         }
159
160
161         /**
162          * Receive ACK
163          */
164         public synchronized void receiveAck() throws IOException
165         {
166                 int ack;
167                 ack = (int) input.read();
168         }
169
170
171         /**
172          * Set SO TIMEOUT
173          */
174         public void setSoTimeout(int timeout) throws SocketException {
175
176                 sock.setSoTimeout(timeout);
177         }
178 }