diff options
-rw-r--r-- | build.sbt | 10 | ||||
-rw-r--r-- | core/build.sbt | 2 | ||||
-rw-r--r-- | core/src/main/scala/ch/jodersky/akka/serial/Serial.scala | 2 | ||||
-rw-r--r-- | core/src/main/scala/ch/jodersky/akka/serial/SerialManager.scala | 1 | ||||
-rw-r--r-- | core/src/main/scala/ch/jodersky/akka/serial/SerialOperator.scala | 5 | ||||
-rw-r--r-- | core/src/test/scala/ch/jodersky/akka/serial/SerialManagerSpec.scala (renamed from core/src/test/scala/ch/jodersky/flow/SerialManagerSpec.scala) | 0 | ||||
-rw-r--r-- | core/src/test/scala/ch/jodersky/akka/serial/SerialOperatorSpec.scala (renamed from core/src/test/scala/ch/jodersky/flow/SerialOperatorSpec.scala) | 1 | ||||
-rw-r--r-- | native/src/CMakeLists.txt | 4 | ||||
-rw-r--r-- | native/src/akka_serial_jni.c (renamed from native/src/flow_jni.c) | 42 | ||||
-rw-r--r-- | native/src/include/akka_serial.h (renamed from native/src/include/flow.h) | 6 | ||||
-rw-r--r-- | native/src/include/ch_jodersky_akka_serial_sync_UnsafeSerial.h | 45 | ||||
-rw-r--r-- | native/src/include/ch_jodersky_akka_serial_sync_UnsafeSerial__.h | 29 | ||||
-rw-r--r-- | native/src/include/ch_jodersky_flow_UnsafeSerial.h | 45 | ||||
-rw-r--r-- | native/src/include/ch_jodersky_flow_UnsafeSerial__.h | 29 | ||||
-rw-r--r-- | native/src/platform/posix/akka_serial.c (renamed from native/src/platform/posix/flow.c) | 2 | ||||
-rw-r--r-- | native/src/platform/windows/README | 2 | ||||
-rw-r--r-- | native/src/platform/windows/akka_serial.c.disabled (renamed from native/src/platform/windows/flow.c.disabled) | 0 | ||||
-rw-r--r-- | native/src/readme.md | 2 | ||||
-rw-r--r-- | stream/src/main/scala/ch/jodersky/akka/serial/stream/Serial.scala (renamed from stream/src/main/scala/ch/jodersky/flow/stream/Serial.scala) | 3 | ||||
-rw-r--r-- | stream/src/main/scala/ch/jodersky/akka/serial/stream/StreamSerialException.scala (renamed from stream/src/main/scala/ch/jodersky/flow/stream/StreamSerialException.scala) | 0 | ||||
-rw-r--r-- | stream/src/main/scala/ch/jodersky/akka/serial/stream/StreamWatcherException.scala (renamed from stream/src/main/scala/ch/jodersky/flow/stream/StreamWatcherException.scala) | 0 | ||||
-rw-r--r-- | stream/src/main/scala/ch/jodersky/akka/serial/stream/impl/SerialConnectionLogic.scala (renamed from stream/src/main/scala/ch/jodersky/flow/stream/impl/SerialConnectionLogic.scala) | 0 | ||||
-rw-r--r-- | stream/src/main/scala/ch/jodersky/akka/serial/stream/impl/SerialConnectionStage.scala (renamed from stream/src/main/scala/ch/jodersky/flow/stream/impl/SerialConnectionStage.scala) | 0 | ||||
-rw-r--r-- | stream/src/main/scala/ch/jodersky/akka/serial/stream/impl/WatcherLogic.scala (renamed from stream/src/main/scala/ch/jodersky/flow/stream/impl/WatcherLogic.scala) | 0 | ||||
-rw-r--r-- | stream/src/main/scala/ch/jodersky/akka/serial/stream/impl/WatcherStage.scala (renamed from stream/src/main/scala/ch/jodersky/flow/stream/impl/WatcherStage.scala) | 0 | ||||
-rw-r--r-- | stream/src/test/scala/ch/jodersky/akka/serial/stream/SerialSpec.scala (renamed from stream/src/test/scala/ch/jodersky/flow/stream/SerialSpec.scala) | 0 | ||||
-rw-r--r-- | sync/build.sbt | 5 | ||||
-rw-r--r-- | sync/src/main/scala/ch/jodersky/akka/serial/Parity.scala (renamed from core/src/main/scala/ch/jodersky/akka/serial/Parity.scala) | 0 | ||||
-rw-r--r-- | sync/src/main/scala/ch/jodersky/akka/serial/SerialSettings.scala (renamed from core/src/main/scala/ch/jodersky/akka/serial/SerialSettings.scala) | 0 | ||||
-rw-r--r-- | sync/src/main/scala/ch/jodersky/akka/serial/exceptions.scala (renamed from core/src/main/scala/ch/jodersky/akka/serial/exceptions.scala) | 0 | ||||
-rw-r--r-- | sync/src/main/scala/ch/jodersky/akka/serial/sync/SerialConnection.scala (renamed from core/src/main/scala/ch/jodersky/akka/serial/SerialConnection.scala) | 20 | ||||
-rw-r--r-- | sync/src/main/scala/ch/jodersky/akka/serial/sync/UnsafeSerial.scala (renamed from core/src/main/scala/ch/jodersky/akka/serial/UnsafeSerial.scala) | 3 | ||||
-rw-r--r-- | sync/src/test/scala/ch/jodersky/akka/serial/PseudoTerminal.scala (renamed from core/src/test/scala/ch/jodersky/flow/PseudoTerminal.scala) | 0 | ||||
-rw-r--r-- | sync/src/test/scala/ch/jodersky/akka/serial/sync/SerialConnectionSpec.scala | 101 |
34 files changed, 237 insertions, 122 deletions
@@ -34,18 +34,22 @@ pomExtra in ThisBuild := { // Project structure lazy val root = (project in file(".")) - .aggregate(core, native, stream) + .aggregate(core, native, stream, sync) lazy val core = (project in file("core")) .settings(name := "akka-serial-core") - .dependsOn(native % "test->runtime") + .dependsOn(sync, sync % "test->test") lazy val native = (project in file("native")) .settings(name := "akka-serial-native") lazy val stream = (project in file("stream")) .settings(name := "akka-serial-stream") - .dependsOn(core, core % "test->test", native % "test->runtime") + .dependsOn(core, sync % "test->test", native % "test->runtime") + +lazy val sync = (project in file("sync")) + .settings(name := "akka-serial-sync") + .dependsOn(native % "test->runtime") lazy val samplesTerminal = (project in file("samples") / "terminal") .dependsOn(core, native % Runtime) diff --git a/core/build.sbt b/core/build.sbt index fdfcbab..d1f1dc6 100644 --- a/core/build.sbt +++ b/core/build.sbt @@ -3,5 +3,3 @@ import flow.Dependencies libraryDependencies += Dependencies.akkaActor libraryDependencies += Dependencies.akkaTestKit % "test" libraryDependencies += Dependencies.scalatest % "test" - -target in javah := (baseDirectory in ThisBuild).value / "flow-native" / "src" / "include" diff --git a/core/src/main/scala/ch/jodersky/akka/serial/Serial.scala b/core/src/main/scala/ch/jodersky/akka/serial/Serial.scala index 889d3b0..f0c9602 100644 --- a/core/src/main/scala/ch/jodersky/akka/serial/Serial.scala +++ b/core/src/main/scala/ch/jodersky/akka/serial/Serial.scala @@ -127,6 +127,6 @@ object Serial extends ExtensionKey[SerialExt] { * * @param value set to enable debugging */ - def debug(value: Boolean) = UnsafeSerial.debug(value) + def debug(value: Boolean) = sync.UnsafeSerial.debug(value) } diff --git a/core/src/main/scala/ch/jodersky/akka/serial/SerialManager.scala b/core/src/main/scala/ch/jodersky/akka/serial/SerialManager.scala index a6647cd..1f0a82d 100644 --- a/core/src/main/scala/ch/jodersky/akka/serial/SerialManager.scala +++ b/core/src/main/scala/ch/jodersky/akka/serial/SerialManager.scala @@ -3,6 +3,7 @@ package ch.jodersky.akka.serial import akka.actor.{ Actor, ActorLogging, OneForOneStrategy } import akka.actor.SupervisorStrategy.{ Escalate, Stop } import scala.util.{ Failure, Success, Try } +import sync.SerialConnection /** * Entry point to the serial API. Actor that manages serial port creation. Once opened, a serial port is handed over to diff --git a/core/src/main/scala/ch/jodersky/akka/serial/SerialOperator.scala b/core/src/main/scala/ch/jodersky/akka/serial/SerialOperator.scala index 1cfc703..e099e80 100644 --- a/core/src/main/scala/ch/jodersky/akka/serial/SerialOperator.scala +++ b/core/src/main/scala/ch/jodersky/akka/serial/SerialOperator.scala @@ -4,6 +4,8 @@ import akka.actor.{ Actor, ActorLogging, ActorRef, Props, Terminated } import akka.util.ByteString import java.nio.ByteBuffer +import sync.SerialConnection + /** * Operator associated to an open serial port. All communication with a port is done via an operator. Operators are created though the serial manager. * @see SerialManager @@ -21,8 +23,7 @@ private[serial] class SerialOperator(connection: SerialConnection, bufferSize: I while (!connection.isClosed && !stop) { try { buffer.clear() - val length = connection.read(buffer) - buffer.limit(length) + connection.read(buffer) val data = ByteString.fromByteBuffer(buffer) client.tell(Serial.Received(data), self) } catch { diff --git a/core/src/test/scala/ch/jodersky/flow/SerialManagerSpec.scala b/core/src/test/scala/ch/jodersky/akka/serial/SerialManagerSpec.scala index cac07fa..cac07fa 100644 --- a/core/src/test/scala/ch/jodersky/flow/SerialManagerSpec.scala +++ b/core/src/test/scala/ch/jodersky/akka/serial/SerialManagerSpec.scala diff --git a/core/src/test/scala/ch/jodersky/flow/SerialOperatorSpec.scala b/core/src/test/scala/ch/jodersky/akka/serial/SerialOperatorSpec.scala index 5c9ca49..587fb15 100644 --- a/core/src/test/scala/ch/jodersky/flow/SerialOperatorSpec.scala +++ b/core/src/test/scala/ch/jodersky/akka/serial/SerialOperatorSpec.scala @@ -6,6 +6,7 @@ import akka.actor.{ActorRef, ActorSystem} import akka.testkit.{ImplicitSender, TestKit} import akka.util.ByteString import org.scalatest._ +import sync._ case class Ack(n: Int) extends Serial.Event diff --git a/native/src/CMakeLists.txt b/native/src/CMakeLists.txt index a57451c..ca60ffb 100644 --- a/native/src/CMakeLists.txt +++ b/native/src/CMakeLists.txt @@ -11,8 +11,8 @@ set(ignoreMe "${SBT}") # sbt-jni defines -DSBT # Define project and related variables # (required by sbt-jni) please use semantic versioning # -project (flow) -set(PROJECT_VERSION_MAJOR 4) +project (akkaserial) +set(PROJECT_VERSION_MAJOR 5) set(PROJECT_VERSION_MINOR 0) set(PROJECT_VERSION_PATCH 0) diff --git a/native/src/flow_jni.c b/native/src/akka_serial_jni.c index 75bffff..4eec999 100644 --- a/native/src/flow_jni.c +++ b/native/src/akka_serial_jni.c @@ -1,9 +1,9 @@ #include <stdint.h> -#include "flow.h" +#include "akka_serial.h" -#include "ch_jodersky_flow_UnsafeSerial.h" -#include "ch_jodersky_flow_UnsafeSerial__.h" +#include "ch_jodersky_akka_serial_sync_UnsafeSerial.h" +#include "ch_jodersky_akka_serial_sync_UnsafeSerial__.h" // suppress unused parameter warnings #define UNUSED_ARG(x) (void)(x) @@ -18,11 +18,11 @@ static void check(JNIEnv* env, int ret) { switch (ret) { case -E_IO: throwException(env, "java/io/IOException", ""); break; - case -E_BUSY: throwException(env, "ch/jodersky/flow/PortInUseException", ""); break; - case -E_ACCESS_DENIED: throwException(env, "ch/jodersky/flow/AccessDeniedException", ""); break; - case -E_INVALID_SETTINGS: throwException(env, "ch/jodersky/flow/InvalidSettingsException", ""); break; - case -E_INTERRUPT: throwException(env, "ch/jodersky/flow/PortInterruptedException", ""); break; - case -E_NO_PORT: throwException(env, "ch/jodersky/flow/NoSuchPortException", ""); break; + case -E_BUSY: throwException(env, "ch/jodersky/akka/serial/PortInUseException", ""); break; + case -E_ACCESS_DENIED: throwException(env, "ch/jodersky/akka/serial/AccessDeniedException", ""); break; + case -E_INVALID_SETTINGS: throwException(env, "ch/jodersky/akka/serial/InvalidSettingsException", ""); break; + case -E_INTERRUPT: throwException(env, "ch/jodersky/akka/serial/PortInterruptedException", ""); break; + case -E_NO_PORT: throwException(env, "ch/jodersky/akka/serial/NoSuchPortException", ""); break; default: return; } } @@ -30,18 +30,18 @@ static void check(JNIEnv* env, int ret) /** Get pointer to serial config associated to an UnsafeSerial instance. */ static struct serial_config* get_config(JNIEnv* env, jobject unsafe_serial) { - jclass clazz = (*env)->FindClass(env, "ch/jodersky/flow/UnsafeSerial"); + jclass clazz = (*env)->FindClass(env, "ch/jodersky/akka/serial/sync/UnsafeSerial"); jfieldID field = (*env)->GetFieldID(env, clazz, "serialAddr", "J"); jlong addr = (*env)->GetLongField(env, unsafe_serial, field); return (struct serial_config*) (intptr_t) addr; } /* - * Class: ch_jodersky_flow_UnsafeSerial__ + * Class: ch_jodersky_akka_serial_sync_UnsafeSerial__ * Method: open * Signature: (Ljava/lang/String;IIZI)J */ -JNIEXPORT jlong JNICALL Java_ch_jodersky_flow_UnsafeSerial_00024_open +JNIEXPORT jlong JNICALL Java_ch_jodersky_akka_serial_sync_UnsafeSerial_00024_open (JNIEnv *env, jobject instance, jstring port_name, jint baud, jint char_size, jboolean two_stop_bits, jint parity) { UNUSED_ARG(instance); @@ -61,11 +61,11 @@ JNIEXPORT jlong JNICALL Java_ch_jodersky_flow_UnsafeSerial_00024_open } /* - * Class: ch_jodersky_flow_UnsafeSerial + * Class: ch_jodersky_akka_serial_sync_UnsafeSerial * Method: read * Signature: (Ljava/nio/ByteBuffer;)I */ -JNIEXPORT jint JNICALL Java_ch_jodersky_flow_UnsafeSerial_read +JNIEXPORT jint JNICALL Java_ch_jodersky_akka_serial_sync_UnsafeSerial_read (JNIEnv *env, jobject instance, jobject buffer) { char* local_buffer = (char*) (*env)->GetDirectBufferAddress(env, buffer); @@ -85,11 +85,11 @@ JNIEXPORT jint JNICALL Java_ch_jodersky_flow_UnsafeSerial_read } /* - * Class: ch_jodersky_flow_UnsafeSerial + * Class: ch_jodersky_akka_serial_sync_UnsafeSerial * Method: cancelRead * Signature: ()V */ -JNIEXPORT void JNICALL Java_ch_jodersky_flow_UnsafeSerial_cancelRead +JNIEXPORT void JNICALL Java_ch_jodersky_akka_serial_sync_UnsafeSerial_cancelRead (JNIEnv *env, jobject instance) { int r = serial_cancel_read(get_config(env, instance)); @@ -99,11 +99,11 @@ JNIEXPORT void JNICALL Java_ch_jodersky_flow_UnsafeSerial_cancelRead } /* - * Class: ch_jodersky_flow_UnsafeSerial + * Class: ch_jodersky_akka_serial_sync_UnsafeSerial * Method: write * Signature: (Ljava/nio/ByteBuffer;I)I */ -JNIEXPORT jint JNICALL Java_ch_jodersky_flow_UnsafeSerial_write +JNIEXPORT jint JNICALL Java_ch_jodersky_akka_serial_sync_UnsafeSerial_write (JNIEnv *env, jobject instance, jobject buffer, jint size) { @@ -122,11 +122,11 @@ JNIEXPORT jint JNICALL Java_ch_jodersky_flow_UnsafeSerial_write } /* - * Class: ch_jodersky_flow_UnsafeSerial + * Class: ch_jodersky_akka_serial_sync_UnsafeSerial * Method: close * Signature: ()V */ -JNIEXPORT void JNICALL Java_ch_jodersky_flow_UnsafeSerial_close +JNIEXPORT void JNICALL Java_ch_jodersky_akka_serial_sync_UnsafeSerial_close (JNIEnv *env, jobject instance) { int r = serial_close(get_config(env, instance)); @@ -136,11 +136,11 @@ JNIEXPORT void JNICALL Java_ch_jodersky_flow_UnsafeSerial_close } /* - * Class: ch_jodersky_flow_UnsafeSerial__ + * Class: ch_jodersky_akka_serial_sync_UnsafeSerial__ * Method: debug * Signature: (Z)V */ -JNIEXPORT void JNICALL Java_ch_jodersky_flow_UnsafeSerial_00024_debug +JNIEXPORT void JNICALL Java_ch_jodersky_akka_serial_sync_UnsafeSerial_00024_debug (JNIEnv *env, jobject instance, jboolean value) { UNUSED_ARG(env); diff --git a/native/src/include/flow.h b/native/src/include/akka_serial.h index e3f33b9..23507a9 100644 --- a/native/src/include/flow.h +++ b/native/src/include/akka_serial.h @@ -1,5 +1,5 @@ -#ifndef FLOW_H -#define FLOW_H +#ifndef AKKA_SERIAL_H +#define AKKA_SERIAL_H #ifdef __cplusplus extern "C" { @@ -100,4 +100,4 @@ void serial_debug(bool value); } #endif -#endif /* FLOW_H */ +#endif /* AKKA_SERIAL_H */ diff --git a/native/src/include/ch_jodersky_akka_serial_sync_UnsafeSerial.h b/native/src/include/ch_jodersky_akka_serial_sync_UnsafeSerial.h new file mode 100644 index 0000000..897287b --- /dev/null +++ b/native/src/include/ch_jodersky_akka_serial_sync_UnsafeSerial.h @@ -0,0 +1,45 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include <jni.h> +/* Header for class ch_jodersky_akka_serial_sync_UnsafeSerial */ + +#ifndef _Included_ch_jodersky_akka_serial_sync_UnsafeSerial +#define _Included_ch_jodersky_akka_serial_sync_UnsafeSerial +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: ch_jodersky_akka_serial_sync_UnsafeSerial + * Method: read + * Signature: (Ljava/nio/ByteBuffer;)I + */ +JNIEXPORT jint JNICALL Java_ch_jodersky_akka_serial_sync_UnsafeSerial_read + (JNIEnv *, jobject, jobject); + +/* + * Class: ch_jodersky_akka_serial_sync_UnsafeSerial + * Method: cancelRead + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_ch_jodersky_akka_serial_sync_UnsafeSerial_cancelRead + (JNIEnv *, jobject); + +/* + * Class: ch_jodersky_akka_serial_sync_UnsafeSerial + * Method: write + * Signature: (Ljava/nio/ByteBuffer;I)I + */ +JNIEXPORT jint JNICALL Java_ch_jodersky_akka_serial_sync_UnsafeSerial_write + (JNIEnv *, jobject, jobject, jint); + +/* + * Class: ch_jodersky_akka_serial_sync_UnsafeSerial + * Method: close + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_ch_jodersky_akka_serial_sync_UnsafeSerial_close + (JNIEnv *, jobject); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/native/src/include/ch_jodersky_akka_serial_sync_UnsafeSerial__.h b/native/src/include/ch_jodersky_akka_serial_sync_UnsafeSerial__.h new file mode 100644 index 0000000..cc9b460 --- /dev/null +++ b/native/src/include/ch_jodersky_akka_serial_sync_UnsafeSerial__.h @@ -0,0 +1,29 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include <jni.h> +/* Header for class ch_jodersky_akka_serial_sync_UnsafeSerial__ */ + +#ifndef _Included_ch_jodersky_akka_serial_sync_UnsafeSerial__ +#define _Included_ch_jodersky_akka_serial_sync_UnsafeSerial__ +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: ch_jodersky_akka_serial_sync_UnsafeSerial__ + * Method: open + * Signature: (Ljava/lang/String;IIZI)J + */ +JNIEXPORT jlong JNICALL Java_ch_jodersky_akka_serial_sync_UnsafeSerial_00024_open + (JNIEnv *, jobject, jstring, jint, jint, jboolean, jint); + +/* + * Class: ch_jodersky_akka_serial_sync_UnsafeSerial__ + * Method: debug + * Signature: (Z)V + */ +JNIEXPORT void JNICALL Java_ch_jodersky_akka_serial_sync_UnsafeSerial_00024_debug + (JNIEnv *, jobject, jboolean); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/native/src/include/ch_jodersky_flow_UnsafeSerial.h b/native/src/include/ch_jodersky_flow_UnsafeSerial.h deleted file mode 100644 index f80ada0..0000000 --- a/native/src/include/ch_jodersky_flow_UnsafeSerial.h +++ /dev/null @@ -1,45 +0,0 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include <jni.h> -/* Header for class ch_jodersky_flow_UnsafeSerial */ - -#ifndef _Included_ch_jodersky_flow_UnsafeSerial -#define _Included_ch_jodersky_flow_UnsafeSerial -#ifdef __cplusplus -extern "C" { -#endif -/* - * Class: ch_jodersky_flow_UnsafeSerial - * Method: read - * Signature: (Ljava/nio/ByteBuffer;)I - */ -JNIEXPORT jint JNICALL Java_ch_jodersky_flow_UnsafeSerial_read - (JNIEnv *, jobject, jobject); - -/* - * Class: ch_jodersky_flow_UnsafeSerial - * Method: cancelRead - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_ch_jodersky_flow_UnsafeSerial_cancelRead - (JNIEnv *, jobject); - -/* - * Class: ch_jodersky_flow_UnsafeSerial - * Method: write - * Signature: (Ljava/nio/ByteBuffer;I)I - */ -JNIEXPORT jint JNICALL Java_ch_jodersky_flow_UnsafeSerial_write - (JNIEnv *, jobject, jobject, jint); - -/* - * Class: ch_jodersky_flow_UnsafeSerial - * Method: close - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_ch_jodersky_flow_UnsafeSerial_close - (JNIEnv *, jobject); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/native/src/include/ch_jodersky_flow_UnsafeSerial__.h b/native/src/include/ch_jodersky_flow_UnsafeSerial__.h deleted file mode 100644 index 617875f..0000000 --- a/native/src/include/ch_jodersky_flow_UnsafeSerial__.h +++ /dev/null @@ -1,29 +0,0 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include <jni.h> -/* Header for class ch_jodersky_flow_UnsafeSerial__ */ - -#ifndef _Included_ch_jodersky_flow_UnsafeSerial__ -#define _Included_ch_jodersky_flow_UnsafeSerial__ -#ifdef __cplusplus -extern "C" { -#endif -/* - * Class: ch_jodersky_flow_UnsafeSerial__ - * Method: open - * Signature: (Ljava/lang/String;IIZI)J - */ -JNIEXPORT jlong JNICALL Java_ch_jodersky_flow_UnsafeSerial_00024_open - (JNIEnv *, jobject, jstring, jint, jint, jboolean, jint); - -/* - * Class: ch_jodersky_flow_UnsafeSerial__ - * Method: debug - * Signature: (Z)V - */ -JNIEXPORT void JNICALL Java_ch_jodersky_flow_UnsafeSerial_00024_debug - (JNIEnv *, jobject, jboolean); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/native/src/platform/posix/flow.c b/native/src/platform/posix/akka_serial.c index 969949b..a2de056 100644 --- a/native/src/platform/posix/flow.c +++ b/native/src/platform/posix/akka_serial.c @@ -6,7 +6,7 @@ #include <fcntl.h> #include <sys/file.h> #include <sys/select.h> -#include "flow.h" +#include "akka_serial.h" #define DATA_CANCEL 0xffffffff diff --git a/native/src/platform/windows/README b/native/src/platform/windows/README index 3d24410..ea7c736 100644 --- a/native/src/platform/windows/README +++ b/native/src/platform/windows/README @@ -1 +1 @@ -The contents of the file flow.c were found in the avrdude project. They look like they may be a good starting point for serial communication on windows. +The contents of the file akka_serial.c were found in the avrdude project. They look like they may be a good starting point for serial communication on windows. diff --git a/native/src/platform/windows/flow.c.disabled b/native/src/platform/windows/akka_serial.c.disabled index 86a267c..86a267c 100644 --- a/native/src/platform/windows/flow.c.disabled +++ b/native/src/platform/windows/akka_serial.c.disabled diff --git a/native/src/readme.md b/native/src/readme.md index 84306fe..9b9d00d 100644 --- a/native/src/readme.md +++ b/native/src/readme.md @@ -1,3 +1,3 @@ -# Native backend for flow +# Native backend for akka-serial Refer to [developer.md](../../site/jekyll/documentation/developer.md) for information on how to build. diff --git a/stream/src/main/scala/ch/jodersky/flow/stream/Serial.scala b/stream/src/main/scala/ch/jodersky/akka/serial/stream/Serial.scala index 2195435..bf6094e 100644 --- a/stream/src/main/scala/ch/jodersky/flow/stream/Serial.scala +++ b/stream/src/main/scala/ch/jodersky/akka/serial/stream/Serial.scala @@ -9,8 +9,9 @@ import akka.io.IO import akka.stream.scaladsl.Flow import akka.util.ByteString -import ch.jodersky.akka.serial.{Serial => CoreSerial} import impl._ +import ch.jodersky.akka.serial.{Serial => CoreSerial} + object Serial extends ExtensionId[Serial] with ExtensionIdProvider { diff --git a/stream/src/main/scala/ch/jodersky/flow/stream/StreamSerialException.scala b/stream/src/main/scala/ch/jodersky/akka/serial/stream/StreamSerialException.scala index ed94374..ed94374 100644 --- a/stream/src/main/scala/ch/jodersky/flow/stream/StreamSerialException.scala +++ b/stream/src/main/scala/ch/jodersky/akka/serial/stream/StreamSerialException.scala diff --git a/stream/src/main/scala/ch/jodersky/flow/stream/StreamWatcherException.scala b/stream/src/main/scala/ch/jodersky/akka/serial/stream/StreamWatcherException.scala index 96fbb77..96fbb77 100644 --- a/stream/src/main/scala/ch/jodersky/flow/stream/StreamWatcherException.scala +++ b/stream/src/main/scala/ch/jodersky/akka/serial/stream/StreamWatcherException.scala diff --git a/stream/src/main/scala/ch/jodersky/flow/stream/impl/SerialConnectionLogic.scala b/stream/src/main/scala/ch/jodersky/akka/serial/stream/impl/SerialConnectionLogic.scala index 13ee5d6..13ee5d6 100644 --- a/stream/src/main/scala/ch/jodersky/flow/stream/impl/SerialConnectionLogic.scala +++ b/stream/src/main/scala/ch/jodersky/akka/serial/stream/impl/SerialConnectionLogic.scala diff --git a/stream/src/main/scala/ch/jodersky/flow/stream/impl/SerialConnectionStage.scala b/stream/src/main/scala/ch/jodersky/akka/serial/stream/impl/SerialConnectionStage.scala index 20f38eb..20f38eb 100644 --- a/stream/src/main/scala/ch/jodersky/flow/stream/impl/SerialConnectionStage.scala +++ b/stream/src/main/scala/ch/jodersky/akka/serial/stream/impl/SerialConnectionStage.scala diff --git a/stream/src/main/scala/ch/jodersky/flow/stream/impl/WatcherLogic.scala b/stream/src/main/scala/ch/jodersky/akka/serial/stream/impl/WatcherLogic.scala index e9dc41d..e9dc41d 100644 --- a/stream/src/main/scala/ch/jodersky/flow/stream/impl/WatcherLogic.scala +++ b/stream/src/main/scala/ch/jodersky/akka/serial/stream/impl/WatcherLogic.scala diff --git a/stream/src/main/scala/ch/jodersky/flow/stream/impl/WatcherStage.scala b/stream/src/main/scala/ch/jodersky/akka/serial/stream/impl/WatcherStage.scala index be6af18..be6af18 100644 --- a/stream/src/main/scala/ch/jodersky/flow/stream/impl/WatcherStage.scala +++ b/stream/src/main/scala/ch/jodersky/akka/serial/stream/impl/WatcherStage.scala diff --git a/stream/src/test/scala/ch/jodersky/flow/stream/SerialSpec.scala b/stream/src/test/scala/ch/jodersky/akka/serial/stream/SerialSpec.scala index 29c4ef6..29c4ef6 100644 --- a/stream/src/test/scala/ch/jodersky/flow/stream/SerialSpec.scala +++ b/stream/src/test/scala/ch/jodersky/akka/serial/stream/SerialSpec.scala diff --git a/sync/build.sbt b/sync/build.sbt new file mode 100644 index 0000000..2e58d91 --- /dev/null +++ b/sync/build.sbt @@ -0,0 +1,5 @@ +import flow.Dependencies + +libraryDependencies += Dependencies.scalatest % "test" + +target in javah := (baseDirectory in ThisBuild).value / "native" / "src" / "include" diff --git a/core/src/main/scala/ch/jodersky/akka/serial/Parity.scala b/sync/src/main/scala/ch/jodersky/akka/serial/Parity.scala index 4e90744..4e90744 100644 --- a/core/src/main/scala/ch/jodersky/akka/serial/Parity.scala +++ b/sync/src/main/scala/ch/jodersky/akka/serial/Parity.scala diff --git a/core/src/main/scala/ch/jodersky/akka/serial/SerialSettings.scala b/sync/src/main/scala/ch/jodersky/akka/serial/SerialSettings.scala index e5ab797..e5ab797 100644 --- a/core/src/main/scala/ch/jodersky/akka/serial/SerialSettings.scala +++ b/sync/src/main/scala/ch/jodersky/akka/serial/SerialSettings.scala diff --git a/core/src/main/scala/ch/jodersky/akka/serial/exceptions.scala b/sync/src/main/scala/ch/jodersky/akka/serial/exceptions.scala index 47a59e4..47a59e4 100644 --- a/core/src/main/scala/ch/jodersky/akka/serial/exceptions.scala +++ b/sync/src/main/scala/ch/jodersky/akka/serial/exceptions.scala diff --git a/core/src/main/scala/ch/jodersky/akka/serial/SerialConnection.scala b/sync/src/main/scala/ch/jodersky/akka/serial/sync/SerialConnection.scala index 6481ee6..e49f408 100644 --- a/core/src/main/scala/ch/jodersky/akka/serial/SerialConnection.scala +++ b/sync/src/main/scala/ch/jodersky/akka/serial/sync/SerialConnection.scala @@ -1,4 +1,5 @@ package ch.jodersky.akka.serial +package sync import java.nio.ByteBuffer import java.util.concurrent.atomic.AtomicBoolean @@ -10,7 +11,7 @@ import java.util.concurrent.atomic.AtomicBoolean * * The underlying serial port is assumed open when this class is initialized. */ -private[serial] class SerialConnection private ( +class SerialConnection private ( unsafe: UnsafeSerial, val port: String ) { @@ -49,14 +50,14 @@ private[serial] class SerialConnection private ( /** * Reads data from underlying serial connection into a ByteBuffer. - * Note that data is read into the buffer's memory, its attributes - * such as position and limit are not modified. + * Note that data is read into the buffer's memory, starting at the + * first position. The buffer's limit is set to the number of bytes + * read. * * A call to this method is blocking, however it is interrupted * if the connection is closed. * - * This method works for direct and indirect buffers but is optimized - * for the former. + * This method works only for direct buffers. * * @param buffer a ByteBuffer into which data is read * @return the actual number of bytes read @@ -67,7 +68,9 @@ private[serial] class SerialConnection private ( if (!closed.get) { try { reading = true - unsafe.read(buffer) + val n = unsafe.read(buffer) + buffer.limit(n) + n } finally { reading = false if (closed.get) readLock.notify() @@ -85,8 +88,7 @@ private[serial] class SerialConnection private ( * The write is non-blocking, this function returns as soon as the data is copied into the kernel's * transmission buffer. * - * This method works for direct and indirect buffers but is optimized - * for the former. + * This method works only for direct buffers. * * @param buffer a ByteBuffer from which data is taken * @return the actual number of bytes written @@ -108,7 +110,7 @@ private[serial] class SerialConnection private ( } -private[serial] object SerialConnection { +object SerialConnection { /** * Opens a new connection to a serial port. diff --git a/core/src/main/scala/ch/jodersky/akka/serial/UnsafeSerial.scala b/sync/src/main/scala/ch/jodersky/akka/serial/sync/UnsafeSerial.scala index 91d8be6..87a565d 100644 --- a/core/src/main/scala/ch/jodersky/akka/serial/UnsafeSerial.scala +++ b/sync/src/main/scala/ch/jodersky/akka/serial/sync/UnsafeSerial.scala @@ -1,4 +1,5 @@ package ch.jodersky.akka.serial +package sync import java.nio.ByteBuffer @@ -16,7 +17,7 @@ import ch.jodersky.jni.nativeLoader * * @param serialAddr address of natively allocated serial configuration structure */ -@nativeLoader("flow4") +@nativeLoader("akkaserial5") private[serial] class UnsafeSerial(final val serialAddr: Long) { final val ParityNone: Int = 0 diff --git a/core/src/test/scala/ch/jodersky/flow/PseudoTerminal.scala b/sync/src/test/scala/ch/jodersky/akka/serial/PseudoTerminal.scala index 86919d2..86919d2 100644 --- a/core/src/test/scala/ch/jodersky/flow/PseudoTerminal.scala +++ b/sync/src/test/scala/ch/jodersky/akka/serial/PseudoTerminal.scala diff --git a/sync/src/test/scala/ch/jodersky/akka/serial/sync/SerialConnectionSpec.scala b/sync/src/test/scala/ch/jodersky/akka/serial/sync/SerialConnectionSpec.scala new file mode 100644 index 0000000..cd218a3 --- /dev/null +++ b/sync/src/test/scala/ch/jodersky/akka/serial/sync/SerialConnectionSpec.scala @@ -0,0 +1,101 @@ +package ch.jodersky.akka.serial +package sync + +import java.nio.ByteBuffer +import org.scalatest._ + +class SerialConnectionSpec extends WordSpec with PseudoTerminal { + + def withEchoConnection[A](action: SerialConnection => A): A = { + withEcho { (port, settings) => + val connection = SerialConnection.open(port, settings) + try { + action(connection) + } finally { + connection.close() + } + } + } + + "A SerialConnection" should { + + "open a valid port" in { + withEcho { (port, settings) => + SerialConnection.open(port, settings) + } + } + + "throw an exception on an invalid port" in { + val settings = SerialSettings(baud = 115200) + intercept[NoSuchPortException] { + SerialConnection.open("/dev/nonexistant", settings) + } + } + + "read the same data it writes to an echo pty" in { + withEchoConnection { conn => + /* Note: this test assumes that all data will be written and read + * within single write and read calls. This in turn assumes that + * internal operating system buffers have enough capacity to + * store all data. */ + val bufferSize = 64 + + val outString = "hello world" + val outBuffer = ByteBuffer.allocateDirect(bufferSize) + val outData = outString.getBytes + outBuffer.put(outData) + conn.write(outBuffer) + + val inBuffer = ByteBuffer.allocateDirect(bufferSize) + conn.read(inBuffer) + val inData = new Array[Byte](inBuffer.remaining()) + inBuffer.get(inData) + val inString = new String(inData) + + assert(inString == outString) + } + } + + "interrupt a read when closing a port" in { + withEchoConnection { conn => + val buffer = ByteBuffer.allocateDirect(64) + + val closer = new Thread { + override def run(): Unit = { + Thread.sleep(100) + conn.close() + } + } + closer.start() + intercept[PortInterruptedException]{ + conn.read(buffer) + } + closer.join() + } + } + + "throw an exception when reading from a closed port" in { + withEchoConnection { conn => + val buffer = ByteBuffer.allocateDirect(64) + conn.close() + + intercept[PortClosedException]{ + conn.read(buffer) + } + } + } + + "throw an exception when writing to a closed port" in { + withEchoConnection { conn => + val buffer = ByteBuffer.allocateDirect(64) + conn.close() + + intercept[PortClosedException]{ + conn.write(buffer) + } + } + } + + } + +} |