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