Adding early version of IoT RMI system; for now: 1) Connects Java to Java; 2) Transla...
[iot2.git] / iotjava / iotrmi / Java / IoTSocketClient.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 IoTSocketClient is a communication class
11  *  that provides interfaces to connect to either
12  *  Java or C++ socket endpoint
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 class IoTSocketClient {
23
24         /**
25          * Class Properties
26          */
27         byte data[];
28         int port;
29         Socket sock;
30         BufferedInputStream input;
31         BufferedOutputStream output;
32
33         /**
34          * Class Constant
35          */
36         static int BUFFSIZE = 128000;   // how many bytes our incoming buffer can hold 
37
38         /**
39          * Default constructor
40          */
41         public IoTSocketClient(int _port, String _address, int rev) throws IOException
42         {
43                 port = _port;
44                 try {
45                         sock = new Socket( InetAddress.getByName(_address), port );
46                         input = new BufferedInputStream(sock.getInputStream(), BUFFSIZE);
47                         output = new BufferedOutputStream(sock.getOutputStream(),BUFFSIZE);
48                 }
49                 catch ( IOException e ) {
50                         e.printStackTrace();
51                 }
52                 data = new byte[BUFFSIZE];
53                 // now we want to tell the server if we want reversed bytes or not
54                 output.write(rev);
55                 output.flush();
56         }
57
58
59         /**
60          * sendBytes() sends an array of bytes
61          */
62         public void sendBytes(byte vals[]) throws IOException
63         {
64                 int len = vals.length;
65                 output.write(len);
66                 output.flush();
67                 output.write(vals, 0, len);
68                 output.flush();
69                 receiveAck();
70                 sendAck();
71         }
72
73
74         /**
75          * receiveBytes() receives an array of bytes
76          */
77         public byte[] receiveBytes(byte val[]) throws IOException
78         {
79                 int i;
80                 int totalbytes = 0;
81                 int numbytes;
82                 // Read the maxlen first
83                 int maxlen = (int)input.read();
84                 if (maxlen>BUFFSIZE)
85                         System.out.println("IoTSocketClient/Server: Sending more bytes then will fit in buffer!");
86                 val = new byte[maxlen];
87                 while (totalbytes < maxlen)
88                 {
89                         numbytes = input.read(data);
90                         // copy the bytes into the result buffer
91                         for (i=totalbytes; i<totalbytes+numbytes; i++)
92                                 val[i] = data[i-totalbytes];
93                         totalbytes += numbytes;
94                 }
95                 // we now send an acknowledgement to the server to let them
96                 // know we've got it
97                 sendAck();
98                 receiveAck();
99
100                 return val;
101         }
102
103
104         /**
105          * Close socket connection
106          */
107         public void close() throws IOException
108         {
109                 sock.close();
110         }
111
112
113         /**
114          * Send ACK
115          */
116         private void sendAck() throws IOException
117         {
118                 int ack;
119                 ack = 0;
120                 output.write(ack);
121                 output.flush();
122         }
123
124
125         /**
126          * Receive ACK
127          */
128         private void receiveAck() throws IOException
129         {
130                 int ack;
131                 ack = (int) input.read();
132         }
133 }