]> gitweb.hamatoma.de Git - jpinet/commitdiff
blink() works
authorhama <hama@siduction.net>
Sun, 3 Jul 2016 21:07:44 +0000 (23:07 +0200)
committerhama <hama@siduction.net>
Sun, 3 Jul 2016 21:07:44 +0000 (23:07 +0200)
src/main/java/de/republib/net/TcpClient.java
src/main/java/de/republib/pinet/Client.java
src/main/java/de/republib/pinet/GpioClient.java

index 87c71306ba51059ef50edc339c53c653f7416380..705679729e92c0a60e2717a9402c0e880e8c4c79 100644 (file)
@@ -1,7 +1,5 @@
 package de.republib.net;
 
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
 import java.io.IOException;
@@ -40,8 +38,9 @@ public class TcpClient {
                this.active = false;
                try {
                        this.socket = new Socket(host, port);
-                       this.outStream = new DataOutputStream(new BufferedOutputStream(this.socket.getOutputStream()));
-                       this.inStream = new DataInputStream(new BufferedInputStream(this.socket.getInputStream()));
+                       // we send always whole messages, we need no buffering:
+                       this.outStream = new DataOutputStream(this.socket.getOutputStream());
+                       this.inStream = new DataInputStream(this.socket.getInputStream());
                        this.active = true;
                } catch (final UnknownHostException e) {
                        this.logger.error(String.format("unknown server: %s:%d", host, port));
@@ -50,15 +49,22 @@ public class TcpClient {
                }
        }
 
+       /**
+        * Receives a message.
+        *
+        * @param buffer
+        *            OUT: the buffer for the content
+        * @return <i>buffer</i> (for chaining)
+        */
        public DynBytes receive(DynBytes buffer) {
                int length;
                try {
-                       buffer.setLength(0);
-                       length = this.inStream.read(buffer.getBuffer(), 0, buffer.getLength());
+                       buffer.setLength(0 + 0);
+                       length = this.inStream.read(buffer.getBuffer(), 0, buffer.getCapacity());
+                       buffer.setLength(length);
                        if (this.logger.isDebugEnabled()) {
-                               this.logger.debug(String.format("received: %s[%d]", buffer.toUtf8(4), buffer.getLength()));
+                               this.logger.debug(String.format("received: %s[%d]", buffer.toUtf8(4), length));
                        }
-                       buffer.setLength(length);
                } catch (final IOException e) {
                        this.logger.error("TcpClient.receive():", e);
                }
@@ -69,10 +75,6 @@ public class TcpClient {
         *
         * @param buffer
         *            bytes to send
-        * @param offset
-        *            first index in <i>buffer</i> to send
-        * @param length
-        *            number of bytes to send
         */
        public void send(DynBytes message) {
                if (this.active) {
@@ -86,4 +88,17 @@ public class TcpClient {
                        }
                }
        }
+
+       /**
+        *
+        * @param buffer
+        *            IN: bytes to send<br>
+        *            OUT: the answer
+        * @return <i>buffer</i> (for chaining)
+        */
+       public DynBytes sendAndReceive(DynBytes buffer) {
+               send(buffer);
+               final DynBytes rc = receive(buffer);
+               return rc;
+       }
 }
index 55030c9c7ae71c687a3744da8539d7d183758438..12456d1f35c67de802c159c432717bcc79411c81 100644 (file)
@@ -1,16 +1,13 @@
-package de.republlb;
+package de.republib.pinet;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import de.republib.pinet.GpioClient;
-import de.republib.pinet.PinNumber;
-
 /**
- * A Camel Application
+ * A TCP client to use services from a GPIO server.
  */
-public class MainApp {
-       static Logger logger = LoggerFactory.getLogger(MainApp.class);
+public class Client {
+       static Logger logger = LoggerFactory.getLogger(Client.class);
 
        /**
         * Evaluates the options and start the program.
@@ -19,7 +16,7 @@ public class MainApp {
         *            program arguments
         */
        public static void main(String... args) throws Exception {
-               MainApp.logger.info("start");
+               Client.logger.info("start");
                int port = 15000;
                final String host = "127.0.0.1";
                final int ix = 0;
index 010fd7fc67d48cfcb222de594cfb91c71d5b9098..75831763f77ef0f05f8c87466250a49f0715295b 100644 (file)
@@ -44,7 +44,7 @@ public class GpioClient extends TcpClient {
         * @return the answer from the server
         */
        public DynBytes blink(PinNumber pin, int count, int high, int low) {
-               final DynBytes rc = null;
+               DynBytes rc = null;
                if (this.logger.isDebugEnabled()) {
                        this.logger.debug(
                                        String.format("blink: pin: %d count: %d high: %d low: %d", pin.getNumber(), count, high, low));
@@ -54,7 +54,7 @@ public class GpioClient extends TcpClient {
                this.buffer.appendLittleEndian(count, 4);
                this.buffer.appendLittleEndian(high * 1000, 4);
                this.buffer.appendLittleEndian(low * 1000, 4);
-               send(this.buffer);
+               rc = sendAndReceive(this.buffer);
                return rc;
        }
 }