package de.republib.net;
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
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));
}
}
+ /**
+ * 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);
}
*
* @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) {
}
}
}
+
+ /**
+ *
+ * @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;
+ }
}
-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.
* 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;
* @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));
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;
}
}