848a2e97265cfd2dedaf5bbdbc76e64c7d2c7238
[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
11 /** Class IoTSocket is the basic class for IoT RMI
12  *  socket communication. This class will be extended
13  *  by both IoTSocketServer and IoTSocketClient
14  *  <p>
15  *  Adapted from Java/C++ socket implementation
16  *  by Keith Vertanen
17  *  @see        <a href="https://www.keithv.com/software/socket/</a>
18  *
19  * @author      Rahmadi Trimananda <rtrimana @ uci.edu>
20  * @version     1.0
21  * @since       2016-08-17
22  */
23 public abstract class IoTSocket {
24
25         /**
26          * Class Properties
27          */
28         protected byte data[];
29         protected int port;
30         protected Socket sock;
31         protected BufferedInputStream input;
32         protected BufferedOutputStream output;
33
34         /**
35          * Class Constant
36          */
37         protected static int BUFFSIZE = 128000; // how many bytes our incoming buffer can hold
38         protected static int MSG_LEN_SIZE = 4;  // send length in the size of integer (4 bytes)
39
40         /**
41          * Default constructor
42          */
43         protected IoTSocket(int _port) throws IOException
44         {
45                 port = _port;
46                 data = new byte[BUFFSIZE];
47         }
48
49
50         /**
51          * sendBytes() sends an array of bytes
52          */
53         public synchronized void sendBytes(byte vals[]) throws IOException
54         {
55                 int len = vals.length;
56                 // Write the length first - convert to array of 4 bytes
57                 ByteBuffer bb = ByteBuffer.allocate(MSG_LEN_SIZE);
58                 bb.putInt(len);
59                 output.write(bb.array(), 0, MSG_LEN_SIZE);
60                 output.flush();
61                 // Write the byte array
62                 output.write(vals, 0, len);
63                 output.flush();
64                 receiveAck();
65                 sendAck();
66         }
67
68
69         /**
70          * receiveBytes() receives an array of bytes
71          */
72         public synchronized byte[] receiveBytes(byte val[]) throws IOException
73         {
74                 int i;
75                 int totalbytes = 0;
76                 int numbytes;
77
78                 // Wait until input is available
79                 while(input.available() == 0);
80                 // Read the maxlen first - read 4 bytes here
81                 byte[] lenBytes = new byte[MSG_LEN_SIZE];
82                 input.read(lenBytes, 0, MSG_LEN_SIZE);
83                 int maxlen = ByteBuffer.wrap(lenBytes).getInt();
84                 // Receive until maxlen
85                 if (maxlen>BUFFSIZE)
86                         System.out.println("IoTSocketClient/Server: Sending more bytes then will fit in buffer!");
87                 val = new byte[maxlen];
88                 while (totalbytes < maxlen)
89                 {
90                         numbytes = input.read(data);
91                         // copy the bytes into the result buffer
92                         for (i=totalbytes; i<totalbytes+numbytes; i++)
93                                 val[i] = data[i-totalbytes];
94                         totalbytes += numbytes;
95                 }
96                 // we now send an acknowledgement to the server to let them
97                 // know we've got it
98                 sendAck();
99                 receiveAck();
100
101                 return val;
102         }
103
104
105         /**
106          * Close socket connection
107          */
108         public synchronized void close() throws IOException
109         {
110                 sock.close();
111         }
112
113
114         /**
115          * Send ACK
116          */
117         public synchronized void sendAck() throws IOException
118         {
119                 int ack;
120                 ack = 0;
121                 output.write(ack);
122                 output.flush();
123         }
124
125
126         /**
127          * Receive ACK
128          */
129         public synchronized void receiveAck() throws IOException
130         {
131                 int ack;
132                 ack = (int) input.read();
133         }
134 }