diff options
Diffstat (limited to 'flow-main')
-rw-r--r-- | flow-main/src/main/scala/com/github/jodersky/flow/Serial.scala | 5 | ||||
-rw-r--r-- | flow-main/src/main/scala/com/github/jodersky/flow/internal/Watcher.scala | 14 |
2 files changed, 14 insertions, 5 deletions
diff --git a/flow-main/src/main/scala/com/github/jodersky/flow/Serial.scala b/flow-main/src/main/scala/com/github/jodersky/flow/Serial.scala index 2abe242..d3b8873 100644 --- a/flow-main/src/main/scala/com/github/jodersky/flow/Serial.scala +++ b/flow-main/src/main/scala/com/github/jodersky/flow/Serial.scala @@ -97,14 +97,15 @@ object Serial extends ExtensionKey[SerialExt] { * the given directory. * In case the given directory cannot be watched, the manager responds with a `CommandFailed` message. * - * Note: the directory must exist when this message is sent. + * Note: the sender is also notified of currently existing ports. * * @param directory the directory to watch + * @param skipInitial don't get notified of already existing ports * * @see Unwatch * @see Connected */ - case class Watch(directory: String = "/dev") extends Command + case class Watch(directory: String = "/dev", skipInitial: Boolean = false) extends Command /** * Stop receiving notifications about a previously watched directory. diff --git a/flow-main/src/main/scala/com/github/jodersky/flow/internal/Watcher.scala b/flow-main/src/main/scala/com/github/jodersky/flow/internal/Watcher.scala index 3c30ccb..c8ca374 100644 --- a/flow-main/src/main/scala/com/github/jodersky/flow/internal/Watcher.scala +++ b/flow-main/src/main/scala/com/github/jodersky/flow/internal/Watcher.scala @@ -2,7 +2,7 @@ package com.github.jodersky.flow package internal import akka.actor.{ Actor, ActorRef, Props } -import java.nio.file.{ ClosedWatchServiceException, FileSystems, Path, Paths, WatchEvent, WatchKey } +import java.nio.file.{ ClosedWatchServiceException, Files, FileSystems, Path, Paths, WatchEvent, WatchKey } import java.nio.file.StandardWatchEventKinds._ import scala.collection.JavaConversions._ import scala.collection.mutable.{ HashMap, Map, MultiMap, Set } @@ -34,7 +34,7 @@ class Watcher(from: Option[ActorRef]) extends Actor { def receive = { - case w @ Serial.Watch(directory) => + case w @ Serial.Watch(directory, skipInitial) => val normalPath = Paths.get(directory).toAbsolutePath val normal = normalPath.toString @@ -42,7 +42,15 @@ class Watcher(from: Option[ActorRef]) extends Actor { keys.getOrElseUpdate(normal, watcher.watch(normalPath)) } match { case Failure(err) => reply(Serial.CommandFailed(w, err), sender) - case Success(key) => clients addBinding (normal, sender) + case Success(key) => + clients addBinding (normal, sender) + if (!skipInitial) { + Files.newDirectoryStream(normalPath) foreach { path => + if (!Files.isDirectory(path)) { + reply(Serial.Connected(path.toString), sender) + } + } + } } case u @ Serial.Unwatch(directory) => |