Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

work in progress

  • Loading branch information...
commit 5c59966ee6e6a7477b57bfd71b83bc8f400d3b51 0 parents
@Raynos authored
3  .gitignore
@@ -0,0 +1,3 @@
+node_modules
+*.log
+*.err
19 LICENCE
@@ -0,0 +1,19 @@
+Copyright (c) 2012 Raynos.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
15 README.md
@@ -0,0 +1,15 @@
+# peer-connection-pool
+
+Pool of peer connections
+
+## Example
+
+## Installation
+
+`npm install peer-connection-pool`
+
+## Contributors
+
+ - Raynos
+
+## MIT Licenced
50 example/connect.js
@@ -0,0 +1,50 @@
+var MuxDemux = require("mux-demux")
+ , PeerConnectionShim = require("peer-connection-shim")
+ , Peers = require("peer-nodes")
+ , reconnect = require("reconnect/sock")
+
+ , PeerConnectionPool = require("../")
+
+module.exports = connect
+
+function connect(options, callback) {
+ console.log("listen", options.uri)
+
+
+ // Connect to the remote sockJS server which acts as a
+ // signal channel and a relay server
+ reconnect(function (stream) {
+ console.log("got stream")
+
+ // Multiplex the connection due to sockJS limitation
+ // Open up three streams for peer list replication,
+ // signal channel for the pool & and the relay stream
+ // for the peer connection shim
+ var mdm = MuxDemux()
+
+ stream.pipe(mdm).pipe(stream)
+
+ var peerStream = mdm.createStream(
+ "/v1/scuttlebutt/" + options.namespace)
+ , poolStream = mdm.createStream(
+ "/v1/echo/" + options.namespace)
+ , relayStream = mdm.createStream(
+ "/v1/relay/" + options.namespace)
+ , peers = Peers()
+ // Pass the pool a function which generates a new
+ // PeerConnection. In this case use the shim but in
+ // the future just use webrtc
+ , pool = PeerConnectionPool(function () {
+ return PeerConnectionShim({
+ stream: relayStream
+ })
+ })
+
+ // pump the streams up
+ peerStream.pipe(peers.createStream()).pipe(peerStream)
+ poolStream.pipe(pool.createStream()).pipe(poolStream)
+
+ // pass peers & pool to callback
+ callback(peers, pool)
+ }).listen(options.uri)
+}
10 example/server.js
@@ -0,0 +1,10 @@
+var http = require("http")
+ , sock = require("signal-channel")
+
+var server = http.createServer(function (req, res) {
+ res.end(
+ "<a href='http://github.com/Raynos/signal-channel'>" +
+ "Signal-channel </a>")
+}).listen(8080)
+
+sock.install(server, "/sock")
56 example/simple.js
@@ -0,0 +1,56 @@
+var WriteStream = require("write-stream")
+ , uuid = require("node-uuid")
+ , assert = require("assert")
+
+ , connect = require("./connect")
+
+// connect to the signal channel
+connect({
+ uri: "//localhost:8080/sock"
+ , namespace: "unique name for app"
+}, function (peers, pool) {
+ console.log("called callback")
+
+ // You are given a peers and pool object. peers is a
+ // distributed list of all peers connected to your signal
+ // channel server on your namespace.
+
+ // pool is a peer connection pooling abstraction giving you
+ // a dead simple API for connecting to peers and listening
+ // on incoming requests from peers
+
+ // You need a unique id to distinguish yourself. Probably
+ // best to use a unique user name instead.
+ var id = uuid()
+
+ console.log("my own id", id)
+
+ // Listen to the pool on your own identifier.
+ // For each new incoming connection just echo back their data
+ pool.listen(id).on("connection", function (stream) {
+ console.log("new connection", stream.peerId)
+ stream.pipe(stream)
+ })
+
+ // For each new peer, open a connection to them and see
+ // whether they echo it back
+ peers.on("join", function (peer) {
+ if (peer.id <= id) {
+ console.log("other side or self?", peer.id, id)
+ // OTHER SIDE HANDLES IT. PEER TO PEER IS SYMETTRIC
+ return
+ }
+
+ console.log("creating stream")
+ var stream = pool.connect(peer.id)
+
+ stream.pipe(WriteStream(function onwrite(message) {
+ console.log("got message", message)
+ assert.equal(message, "hello world")
+ }))
+
+ stream.write("hello world")
+ })
+
+ peers.join({ id: id })
+})
108 index.js
@@ -0,0 +1,108 @@
+var PeerConnection = require("peer-connection")
+ , RemoteEvents = require("remote-events")
+ , EventEmitter = require("events").EventEmitter
+
+module.exports = PeerConnectionPool
+
+function PeerConnectionPool(options) {
+ var peerHash = {}
+ , emitter = new RemoteEvents()
+ , pool = new EventEmitter()
+ , id
+
+ if (typeof options === "function") {
+ options = { createConnection: options }
+ }
+
+ pool.listen = listen
+ pool.connect = connect
+ pool.createStream = createStream
+
+ return pool
+
+ function listen(localId) {
+ id = localId
+
+ emitter.on(id + ":offer", onremoteoffer)
+
+ emitter.on(id + ":answer", onremoteanswer)
+
+ emitter.on(id + ":candidate", onremotecandidate)
+
+ return pool
+ }
+
+ function onremoteoffer(remoteId, offer) {
+ // console.log("onremoteoffer")
+ var pc = createPeerConnection(remoteId)
+
+ pc.createAnswer(offer, onlocalanswer)
+
+ function onlocalanswer(err, answer) {
+ if (err) {
+ return pool.emit("error", err)
+ }
+
+ emitter.emit(remoteId + ":answer", id, answer)
+ }
+ }
+
+ function onremoteanswer(remoteId, answer) {
+ // console.log("onremoteanswer")
+ var pc = peerHash[remoteId]
+
+ pc.setRemote(answer)
+ }
+
+ function onremotecandidate(remoteId, candidate) {
+ var pc = peerHash[remoteId]
+
+ pc.addCandidate(candidate)
+ }
+
+ function connect(remoteId, name) {
+ if (!id) {
+ throw new Error("must listen before connect")
+ }
+
+ var pc = createPeerConnection(remoteId)
+
+ pc.createOffer(onlocaloffer)
+
+ return pc.connect(name)
+
+ function onlocaloffer(err, offer) {
+ if (err) {
+ return pool.emit("error", err)
+ }
+
+ emitter.emit(remoteId + ":offer", id, offer)
+ }
+ }
+
+ function createPeerConnection(remoteId) {
+ var pc = peerHash[remoteId] = PeerConnection(
+ options.createConnection())
+
+ pc.on("candidate", onlocalcandidate)
+
+ pc.on("connection", onconnection)
+
+ return pc
+
+ function onlocalcandidate(candidate) {
+ emitter.emit(remoteId + ":candidate", id, candidate)
+ }
+
+ function onconnection(stream) {
+ console.log("connection", stream)
+ stream.peerId = remoteId
+
+ pool.emit("connection", stream)
+ }
+ }
+
+ function createStream() {
+ return emitter.getStream()
+ }
+}
53 package.json
@@ -0,0 +1,53 @@
+{
+ "name": "peer-connection-pool",
+ "version": "0.1.0",
+ "description": "Pool of peer connections",
+ "keywords": [
+ "webrtc",
+ "p2p",
+ "peer",
+ "real-time",
+ "realtime",
+ "real",
+ "time",
+ "peer2peer"
+ ],
+ "author": "Raynos <raynos2@gmail.com>",
+ "repository": "git://github.com/Raynos/peer-connection-pool.git",
+ "main": "index",
+ "homepage": "https://github.com/Raynos/peer-connection-pool",
+ "contributors": [
+ {
+ "name": "Jake Verbaten"
+ }
+ ],
+ "bugs": {
+ "url": "https://github.com/Raynos/peer-connection-pool/issues",
+ "email": "raynos2@gmail.com"
+ },
+ "dependencies": {
+ "peer-connection": "~0.2.0",
+ "remote-events": "~1.1.1",
+ "mux-demux-logger": "~0.1.0"
+ },
+ "devDependencies": {
+ "tap": "~0.3.1",
+ "write-stream": "~0.4.3",
+ "node-uuid": "~1.4.0",
+ "mux-demux": "~3.3.6",
+ "reconnect": "~0.3.1",
+ "peer-nodes": "~0.1.3",
+ "peer-connection-shim": "~0.5.0",
+ "signal-channel": "~0.2.4",
+ "through": "~1.1.2"
+ },
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "http://github.com/Raynos/peer-connection-pool/raw/master/LICENSE"
+ }
+ ],
+ "scripts": {
+ "test": "tap --stderr --tap ./test"
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.