Permalink
Browse files

cleaned things up and completely broke service

  • Loading branch information...
1 parent 2a54a24 commit 3a05afe6e6f801b09a3810eea27e7366221cadad @Raynos committed Aug 11, 2012
View
20 browser.js
@@ -1,5 +1,5 @@
-var rack = require("hat").rack(128, 16, 16)
- , MuxDemux = require("mux-demux")
+var StreamServer = require("browser-stream-server")
+ , rack = require("hat").rack(128, 16, 16)
module.exports = seaport
@@ -23,14 +23,12 @@ function seaport(server, prefix) {
}
function service(serviceName, callback) {
- var mdm = MuxDemux({
- error: false
- })
- , connection = server.createStream(prefix + "/service/" +
- serviceName + "/?uuid=" + rack())
-
- connection.pipe(mdm).pipe(connection)
-
- mdm.on("connection", callback)
+ console.log("created StreamServer", serviceName)
+ var streamServer = StreamServer(server, {
+ prefix: prefix + "/service"
+ }, callback)
+ var stream = streamServer.listen(serviceName)
+ console.log("server", stream.meta)
+ return stream
}
}
View
10 examples/lazynode/http.js
@@ -0,0 +1,10 @@
+var path = require("path")
+ , http = require("http")
+ , browserifyServer = require("browserify-server")
+
+var handler = browserifyServer(path.join(__dirname, "static"))
+ , server = http.createServer(handler).listen(8080)
+
+module.exports = server
+
+console.log("server listening on port", 8080)
View
16 examples/lazynode/magic.js
@@ -0,0 +1,16 @@
+var seaport = require("seaport-stream")
+ , lazynode = require("lazynode")
+ , methods = {
+ time: function (cb) {
+ cb(new Date().toString())
+ }
+ }
+
+var ports = seaport.connect("localhost", 9093)
+
+ports.service("magic@1.2.3", createMagic)
+
+function createMagic(stream) {
+ var up = lazynode(methods)
+ up.pipe(stream).pipe(up)
+}
View
10 examples/lazynode/seaport.js
@@ -0,0 +1,10 @@
+var seaport = require("seaport")
+ , seaportProxy = require("../..")
+
+var seaportServer = seaport.createServer()
+seaportServer.listen(9093)
+console.log("seaport server listening on port", 9093)
+
+var ports = seaportProxy.connect("localhost", 9093)
+
+module.exports = ports
View
9 examples/lazynode/server.js
@@ -0,0 +1,9 @@
+var ports = require("./seaport")
+ , magicServer = require("./magic")
+ , server = require("./http")
+
+var boot = require("boot")
+
+var sock = boot(ports)
+sock.install(server, '/boot')
+console.log("sock hooked on", "/boot")
View
8 examples/lazynode/static/index.html
@@ -0,0 +1,8 @@
+<html>
+ <head>
+ <title> Simple seaport proxy </title>
+ </head>
+ <body>
+ <script src="bundle.js"></script>
+ </body>
+</html>
View
19 examples/lazynode/static/index.js
@@ -0,0 +1,19 @@
+var boot = require("boot")
+ , lazynode = require("lazynode")
+ , seaport = require("../../../browser")
+ , ports = seaport(boot("/boot"))
+
+var remote = lazynode.connect({
+ createStream: createStream
+ , methods: ["time"]
+})
+
+setInterval(function () {
+ remote.time(function (t) {
+ console.log("time = " + t)
+ })
+}, 1000)
+
+function createStream() {
+ return ports.get("magic@1.2.x")
+}
View
4 examples/service/browserList.js
@@ -1,12 +1,12 @@
var lazynode = require("lazynode")
- , list = []
+ , list = {}
module.exports = browserList
function browserList(stream) {
var up = lazynode({
add: function (id, cb) {
- list.push(id)
+ list[id] = true
cb && cb(null)
}
, get: function (cb) {
View
25 examples/service/server.js
@@ -3,11 +3,30 @@ var ports = require("./seaport")
, browserList = require("./browserList")
var boot = require("boot")
- , streamRouter = require("stream-router")()
+ , StreamRouter = require("stream-router")
+
+var streamRouter = StreamRouter()
+ , sock = boot(log(streamRouter))
streamRouter.addRoute("/seaport/*", ports)
streamRouter.addRoute("/browserList/*", browserList)
-var sock = boot(streamRouter)
sock.install(server, '/boot')
-console.log("sock hooked on", "/boot")
+console.log("sock hooked on", "/boot")
+
+function log(f) {
+ return function (stream) {
+ console.log("[INCOMING-STREAM]", {
+ meta: stream.meta
+ , id: stream.id
+ })
+ /*stream.on("data", function (data) {
+ console.log("[STREAM-DATA]", {
+ meta: stream.meta
+ , id: stream.id
+ , data: data
+ })
+ })*/
+ f.apply(this, arguments)
+ }
+}
View
4 examples/service/static/browserList.js
@@ -13,10 +13,12 @@ var browserList = module.exports = lazynode.connect({
button.addEventListener("click", refreshList)
function refreshList() {
+ console.log("calling get on browserList")
browserList.get(render)
function render(list) {
- output.textContent = list.join(",")
+ console.log("got render")
+ output.textContent = Object.keys(list).join(",")
}
}
View
4 examples/service/static/createServer.js
@@ -1,4 +1,4 @@
-var seaport = require("../..")
+var seaport = require("../../../browser")
, lazynode = require("lazynode")
, mdm = require("./index")
, browserList = require("./browserList")
@@ -18,10 +18,12 @@ function createServer() {
browserList.add(serverName, serviceOnProxy)
function serviceOnProxy() {
+ console.log("browserlist returned")
ports.service(serverName, handleStream)
}
function handleStream(stream) {
+ console.log("handling stream", stream)
var up = lazynode(methods)
stream.pipe(up).pipe(stream)
View
21 examples/service/static/joinServer.js
@@ -1,4 +1,4 @@
-var seaport = require("../..")
+var seaport = require("../../../browser")
, lazynode = require("lazynode")
, mdm = require("./index")
, ports = seaport(mdm)
@@ -17,19 +17,16 @@ button.addEventListener("click", function () {
output.textContent = ""
- setInterval(render, 1000)
+ setInterval(function () {
+ remote.time(function (time) {
+ console.log("time?", time)
+ var div = document.createElement("div")
+ div.textContent = time
+ output.appendChild(div)
+ })
+ }, 1000)
function createStream() {
return ports.get(serverName)
}
-
- function render() {
- remote.time(renderTime)
- }
-
- function renderTime(time) {
- var div = document.createElement("div")
- div.textContent = time
- output.appendChild(div)
- }
})
View
2 examples/simple/static/index.js
@@ -1,5 +1,5 @@
var boot = require("boot")
- , seaport = require("../..")
+ , seaport = require("../../../browser")
, ports = seaport(boot("/boot"))
var stream = ports.get("magic@1.2.x")
View
14 index.js
@@ -24,12 +24,14 @@ function SeaportProxy(ports, port, opts, prefix) {
prefix = prefix || "/seaport"
var streamRouter = StreamRouter()
- streamRouter.addRoute(prefix + "/get/:service/*",
- partial(invokeMethod, "get", ports))
- streamRouter.addRoute(prefix + "/query/:service/*",
- partial(invokeMethod, "query", ports))
- streamRouter.addRoute(prefix + "/service/:service/*",
- partial(service, ports))
+ , serviceRoute = prefix + "/service/*"
+
+ streamRouter.addRoute(prefix + "/get/:service/*"
+ , partial(invokeMethod, "get", ports))
+ streamRouter.addRoute(prefix + "/query/:service/*"
+ , partial(invokeMethod, "query", ports))
+ streamRouter.addRoute(prefix + "/service/*"
+ , service(ports, prefix + "/service"))
return streamRouter
}
View
108 lib/service.js
@@ -1,42 +1,116 @@
-var net = require("net")
+var StreamServerProxy = require("browser-stream-server")
+ , net = require("net")
+ , PauseStream = require("pause-stream")
+ , through = require("through")
+
+module.exports = service
+
+function service(ports, prefix) {
+ var proxy = StreamServerProxy(prefix)
+ , servers = {}
+
+ console.log("proxy created")
+
+ proxy.on("server-created", createServer)
+ proxy.on("server-destroyed", cleanupServer)
+
+ return proxy
+
+ // create seaport service when server stream comes up
+ function createServer(serverName) {
+ console.log("creating server", serverName)
+ var server = servers[serverName] = net.createServer(handleConnection)
+ ports.service(serverName, listen)
+
+ function handleConnection(connection) {
+ console.log("got incoming connection", connection)
+ var stream = proxy.connect(serverName)
+ , buffer = PauseStream().pause()
+ , intermediate = through(stringer)
+
+ connection.pipe(buffer)
+
+ process.nextTick(pipe)
+
+ function pipe() {
+ buffer.pipe(intermediate)
+ .pipe(stream).pipe(connection)
+
+ buffer.resume()
+ }
+ }
+
+ function listen(port, ready) {
+ server.listen(port, ready)
+ }
+ }
+
+ // close servers when server stream goes down
+ function cleanupServer(serverName) {
+ var server = servers[serverName]
+ server && server.close()
+ }
+}
+
+function stringer(data) {
+ this.emit("data", data.toString())
+}
+/*var net = require("net")
, rack = require("hat").rack(128, 16, 16)
, through = require("through")
, PauseStream = require("pause-stream")
- , MuxDemux = require("mux-demux")
+ , url = require("url")
+ , connections = {}
module.exports = service
function service(ports, stream, params) {
var serviceName = params.service
- , server = net.createServer(onConnection)
- , mdm = MuxDemux({
- error: false
- })
+ , streamId = url.parse(stream.meta, true).query.streamId
+
+ console.log("got streamId", streamId)
+
+ if (streamId) {
+ var connection = connections[streamId]
+ , intermediate = through(stringer)
- stream.pipe(mdm).pipe(stream)
+ if (connection === undefined) {
+ return stream.end()
+ }
+
+ stream.pipe(connection)
- ports.service(serviceName, createServer)
+ process.nextTick(pipe)
+
+ } else {
+ console.log("started server")
+ var server = net.createServer(onConnection)
+ ports.service(serviceName, createServer)
+ }
function createServer(port, ready) {
server.listen(port, ready)
}
function onConnection(connection) {
- var stream = mdm.createStream(rack())
- , intermediate = through(stringer)
- , buffer = PauseStream().pause()
+ console.log("got connection")
+ var streamId = rack()
+ connections[streamId] = connection
- stream.pipe(buffer)
+ connection.on("end", deleteConnection)
- process.nextTick(pipe)
+ stream.write(streamId)
- function pipe() {
- connection.pipe(intermediate).pipe(stream)
- buffer.resume().pipe(connection)
+ function deleteConnection() {
+ delete connections[streamId]
}
}
+
+ function pipe() {
+ connection.pipe(intermediate).pipe(stream)
+ }
}
function stringer(data) {
this.emit("data", data.toString())
-}
+}*/
View
10 package.json
@@ -21,17 +21,17 @@
"pause-stream": "0.0.3",
"deck": "0.0.4",
"ap": "~0.1.0",
- "through": "~0.1.3",
+ "through": "~0.1.4",
"seaport": "~0.8.0",
- "stream-router": "0.0.1"
+ "stream-router": "0.0.2",
+ "browser-stream-server": "~0.3.1"
},
"devDependencies": {
- "boot": "~0.6.0",
+ "boot": "~0.7.1",
"browserify": "~1.14.5",
"ecstatic": "~0.1.6",
- "browserify-server": "0.0.1",
+ "browserify-server": "0.2.0",
"seaport-stream": "0.0.1",
- "stream-router": "0.0.1",
"lazynode": "0.1.0",
"hat": "0.0.3"
},

0 comments on commit 3a05afe

Please sign in to comment.