From e3631502b68d8c6ce824153a7f7576f251f5f9ad Mon Sep 17 00:00:00 2001 From: hama Date: Sun, 3 Jul 2016 23:07:44 +0200 Subject: [PATCH] blink() works --- src/main/java/de/republib/net/TcpClient.java | 39 +++++++++++++------ src/main/java/de/republib/pinet/Client.java | 13 +++---- .../java/de/republib/pinet/GpioClient.java | 4 +- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/main/java/de/republib/net/TcpClient.java b/src/main/java/de/republib/net/TcpClient.java index 87c7130..7056797 100644 --- a/src/main/java/de/republib/net/TcpClient.java +++ b/src/main/java/de/republib/net/TcpClient.java @@ -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 buffer (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 buffer 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
+ * OUT: the answer + * @return buffer (for chaining) + */ + public DynBytes sendAndReceive(DynBytes buffer) { + send(buffer); + final DynBytes rc = receive(buffer); + return rc; + } } diff --git a/src/main/java/de/republib/pinet/Client.java b/src/main/java/de/republib/pinet/Client.java index 55030c9..12456d1 100644 --- a/src/main/java/de/republib/pinet/Client.java +++ b/src/main/java/de/republib/pinet/Client.java @@ -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; diff --git a/src/main/java/de/republib/pinet/GpioClient.java b/src/main/java/de/republib/pinet/GpioClient.java index 010fd7f..7583176 100644 --- a/src/main/java/de/republib/pinet/GpioClient.java +++ b/src/main/java/de/republib/pinet/GpioClient.java @@ -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; } } -- 2.39.5