Permalink
Browse files

Merge branch 'remoting'

  • Loading branch information...
m0wfo committed May 22, 2012
2 parents 7ae653b + 7f8d63c commit 3c5fedef8b185e8e13c6454bfbc291922c167288
View
@@ -16,6 +16,7 @@ var actor = function(fn,options) {
preStart: function() {
this.handlers = [fn];
this.handler = fn;
+ if (options && options.prestart) options.prestart.call(this);
},
reply: function(msg) {
this.getSender().tell(msg);
@@ -36,11 +37,14 @@ var actor = function(fn,options) {
h = this.handlers[this.handlers.length-1];
h.call(this, msg);
}
+ },
+ postStop: function() {
+ if (options && options.shutdown) options.shutdown.call(this);
}
};
var af = actorFactory(base);
- if (options != null) {
+ if (options && options["n"]) {
actor = Dispatch.getSystem().actorOf(new Props().withCreator(af).withRouter(new RoundRobinRouter(options["n"])));
} else {
actor = Dispatch.getSystem().actorOf(new Props().withCreator(af));
View
@@ -0,0 +1,76 @@
+importClass(javax.jmdns.JmDNS);
+importClass(javax.jmdns.ServiceInfo);
+importClass(javax.jmdns.ServiceListener);
+
+function parseEvent(e) {
+ var info = e.getInfo();
+ var addr = info.getAddresses();
+ var addresses = [];
+ for (var a in addr) addresses.push(a);
+ var description = new java.lang.String(info.getTextBytes()).toString();
+ return {
+ 'name': info.getName(),
+ 'host': addresses,
+ 'port': info.getPort(),
+ 'type': info.getType(),
+ 'description': description
+ };
+};
+
+var MDNSService = function(responder, service) {
+
+ this.close = function() {
+ responder.send({kill: service});
+ }
+
+}
+
+var MDNSListener = function(responder, listener) {
+
+ this.stop = function() {
+ responder.send({kill: listener});
+ }
+
+};
+
+var MDNS = function() {
+
+ var responder = new actor(function(msg) {
+ if (msg instanceof ServiceInfo) {
+ this.responder.registerService(msg);
+ } else if (msg instanceof ServiceListener) {
+ console.log("L")
+ } else {
+ if (msg.kill instanceof ServiceInfo) this.responder.unregisterService(msg.kill);
+ if (msg.kill instanceof ServiceListener) this.responder.removeServiceListener(msg.kill);
+ }
+ }, {prestart: function() {
+ this.responder = JmDNS.create();
+ }, shutdown: function() {
+ this.responder.close();
+ }});
+
+ this.announce = function(type, name, port, description) {
+ var info = ServiceInfo.create(type, name, port, description);
+ responder.send(info);
+ return new MDNSService(responder, info);
+ }
+
+ this.listen = function(type, add, remove) {
+ var listener = ServiceListener({
+ serviceResolved: function(e) {
+ add(parseEvent(e));
+ },
+ serviceRemoved: function(e) {
+ remove(parseEvent(e));
+ }
+ });
+ responder.addServiceListener(type, listener);
+
+ return new MDNSListener(responder, listener);
+ };
+
+};
+
+module.exports = new MDNS();
+
@@ -13,6 +13,5 @@ public static void main(String[] args) {
System.exit(0);
}
-
-
}
+
@@ -1,74 +0,0 @@
-package com.sprintstack.dispatch;
-
-import akka.actor.ActorRef;
-import akka.actor.Address;
-import akka.actor.Props;
-import akka.routing.RemoteRouterConfig;
-import akka.routing.RoundRobinRouter;
-import java.io.IOException;
-import javax.jmdns.JmDNS;
-import javax.jmdns.ServiceEvent;
-import javax.jmdns.ServiceInfo;
-import javax.jmdns.ServiceListener;
-import java.util.concurrent.CopyOnWriteArraySet;
-
-public class SystemDiscovery {
-
- private static JmDNS browser;
- private static String serviceName = "_sprintstack._tcp.local.";
- private static CopyOnWriteArraySet<ActorRef> systems;
-
- private static void setupJmDNS() {
- if (browser == null) {
- systems = new CopyOnWriteArraySet<ActorRef>();
- systems.add(localRoutee());
- try {
- browser = JmDNS.create();
- browser.registerServiceType(serviceName);
- } catch (IOException e) {
- System.out.println("Couldn't create MDNS instance.");
- }
- }
- }
-
- private static ActorRef localRoutee() {
- return Dispatch.getSystem().actorOf(new Props(EventListenerActor.class).withRouter(new RoundRobinRouter(5)));
- }
-
- private static ActorRef createRoutee(Address address) {
- Address[] addrs = { address };
- return Dispatch.getSystem().actorOf(new Props(EventListenerActor.class).withRouter(new RemoteRouterConfig(new RoundRobinRouter(25),
- addrs)));
- }
-
- public static void announce(String systemName, String appName) {
- setupJmDNS();
- ServiceInfo service = ServiceInfo.create(serviceName, systemName, 8080, appName);
- try {
- browser.registerService(service);
- } catch (IOException e) {
- System.out.println("Couldn't register service.");
- }
- }
-
- public static void listen() {
- setupJmDNS();
- browser.addServiceListener(serviceName, new ServiceListener() {
- public void serviceResolved(ServiceEvent e) {
- ServiceInfo info = e.getInfo();
- Address address = new Address("akka", info.getName(), info.getHostAddress(), info.getPort());
- ActorRef routee = createRoutee(address);
- systems.add(routee);
- }
-
- public void serviceAdded(ServiceEvent e) {
- }
-
- public void serviceRemoved(ServiceEvent e) {
- System.out.println("removed");
- }
- });
- }
-
-}
-

0 comments on commit 3c5fede

Please sign in to comment.