Navigation Menu

Skip to content

Commit

Permalink
Wiring up 2 actors
Browse files Browse the repository at this point in the history
  • Loading branch information
mch committed Apr 5, 2012
1 parent 3567a5f commit bee8a99
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 22 deletions.
3 changes: 2 additions & 1 deletion src/core/ubi/core/IPlugin.scala
@@ -1,5 +1,6 @@
package ubi.core

trait IPlugin {
def register(service : LocationService);
def register();
def initialize();
}
12 changes: 7 additions & 5 deletions src/core/ubi/core/LocationService.scala
@@ -1,16 +1,18 @@
package ubi.core

import scala.actors.Actor;
import scala.actors.Actor._;
import scala.collection.mutable.HashMap;
import ubi.log.Log;
import ubi.log.LogLevel._;

class LocationService {
val plugins = new HashMap[String,Actor]();
val plugins = new HashMap[String,PluginBase]();

def register(name : String, plugin : Actor) {
Log.log(INFO, "Registering plugin '" + name + "'");
def register(name : String, plugin : PluginBase) {
Log.log(INFO, "Registering plugin: " + name);
plugins.put(name, plugin);
}

def findActor(name : String) : Option[PluginBase] = {
plugins.get(name);
}
}
7 changes: 4 additions & 3 deletions src/core/ubi/core/PluginBase.scala
@@ -1,14 +1,15 @@
package ubi.core

import scala.actors.Actor;
import scala.actors.Actor._;
import ubi.core.subscribers.SubscriberHandler;

class PluginBase(name : String)
extends SubscriberHandler with Actor with IPlugin {
def register(service : LocationService) {
service.register(name, this);
def register() {
Globals.locationService.register(name, this);
}

def initialize() {}

def act() {}
}
10 changes: 8 additions & 2 deletions src/core/ubi/core/PluginHandler.scala
Expand Up @@ -2,7 +2,13 @@ package ubi.core

class PluginHandler {
def initialize() {
new MicClient().register(Globals.locationService);
new VoiceCommandModule().register(Globals.locationService);
val micClient = new MicClient();
val voiceCommandModule = new VoiceCommandModule();
micClient.register();
voiceCommandModule.register();
micClient.initialize();
voiceCommandModule.initialize();
micClient.start();
voiceCommandModule.start();
}
}
20 changes: 17 additions & 3 deletions src/core/ubi/core/VoiceCommandModule.scala
@@ -1,10 +1,24 @@
package ubi.core

import scala.actors.Actor;
import scala.actors.Actor._;
import micclient.messages.DataPacket
import ubi.log.{LogLevel, Log}
import ubi.log.LogLevel._;
;

class VoiceCommandModule extends PluginBase("VoiceCommandModule") {
override def initialize() {
val actor = Globals.locationService.findActor("MicClient");
if (actor.isDefined) {
actor.get.subscribers.add(this);
}
}

override def act() {

while (true) {
receive {
case msg : DataPacket =>
Log.log(INFO, "Received data: " + msg.words);
}
}
}
}
16 changes: 9 additions & 7 deletions src/core/ubi/core/micclient/MicClient.scala
@@ -1,22 +1,24 @@
package ubi.core

import scala.actors.Actor;
import scala.actors.Actor._;
import scala.collection.immutable.List;
import ubi.core.subscribers.SubscriberHandler;
import ubi.core.micclient.messages.DataPacket;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

class MicClient extends PluginBase("MicClient") {
def notifySubscribers() {
def notifySubscribers(list : List[String]) {
if (subscribers.isEmpty) return;
var packet = new DataPacket();
packet.source = this;
packet.words = List("McHalls", "was", "here");
packet.words = list;
for (subscriber <- subscribers) {
packet.target = subscriber;
subscriber ! packet;
}
}

override def act() {
while (true) {
Thread.sleep(1000);
notifySubscribers(List("McHalls", "was", "here"));
}
}
}
1 change: 0 additions & 1 deletion src/core/ubi/core/subscribers/SubscriberHandler.scala
@@ -1,7 +1,6 @@
package ubi.core.subscribers

import scala.actors.Actor;
import scala.actors.Actor._;
import scala.collection.mutable.Set;

trait SubscriberHandler {
Expand Down

0 comments on commit bee8a99

Please sign in to comment.