Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bae39f5
commit 01fb473
Showing
3 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,55 @@ | |||
var IPCNode=require("../../IPCNode").IPCNode; | |||
var sys=require("sys"); | |||
|
|||
var a=new IPCNode(); | |||
var b=new IPCNode(); | |||
|
|||
sys.pump(a,b); | |||
sys.pump(b,a); | |||
|
|||
var ipcobj={ | |||
msg: "", | |||
set: function(s) { this.msg=s; }, | |||
get: IPCNode.sync(function() { | |||
return this.msg; | |||
}), | |||
timerAsync: IPCNode.async(function(callback) { | |||
setTimeout(function() { callback(); },1000); | |||
}), | |||
timerCustom: function(callback) { | |||
var callbackRef=IPCNode.reference(callback); | |||
setTimeout(function() { | |||
callbackRef(); | |||
IPCNode.dispose(callbackRef); | |||
},1000); | |||
} | |||
}; | |||
|
|||
function demonstrate(obj,method) { | |||
obj.set("Hello world"); | |||
obj.get(function(msg) { | |||
sys.puts("Got message over "+method+": '"+msg+"'"); | |||
}); | |||
obj.timerAsync(function() { | |||
sys.puts("timerAsync callback over "+method); | |||
}); | |||
obj.timerCustom(function() { | |||
sys.puts("timerCustom callback over "+method); | |||
}); | |||
} | |||
|
|||
b.on("register",function(name,obj) { | |||
if (name=="ipc") | |||
demonstrate(obj,"IPC"); | |||
}); | |||
|
|||
sys.puts("Demonstrating directly"); | |||
demonstrate(ipcobj,"local"); | |||
sys.puts("Demonstrating over IPCNode"); | |||
a.register("ipc",ipcobj); | |||
a.on("clean",function() { | |||
sys.puts("Server endpoint clean"); | |||
}); | |||
b.on("clean",function() { | |||
sys.puts("Client endpoint clean"); | |||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,33 @@ | |||
var sys=require("sys"); | |||
var net=require("net"); | |||
var IPCNode=require("../../IPCNode").IPCNode; | |||
|
|||
var client=net.createConnection(81234); | |||
var ipc=new IPCNode(); | |||
client.on("connect",function() { | |||
sys.pump(client,ipc,function() { ipc.end(); sys.puts("Connection closed"); }); | |||
sys.pump(ipc,client,function() { client.end(); client.destroy() }); | |||
}); | |||
client.on("error",function(e) { | |||
sys.puts("Client error: "+e.toString()); | |||
ipc.end(); | |||
}); | |||
ipc.on("error",function(e) { | |||
sys.puts("IPC error: "+e.toString()); | |||
client.end(); | |||
}); | |||
ipc.on("register",function(name,obj) { | |||
sys.puts("Got object: "+name); | |||
if (name=="main") { | |||
//obj will be disposed once this method ends, so we need a copy for in the callback functions | |||
var copy=IPCNode.reference(obj); | |||
copy.increase(); | |||
copy.get_counter(function(i) { | |||
copy.printString("Client number: "+i); | |||
copy.printArray(["Hello","World"]); | |||
copy.printSimpleObject({Hello: "World",How: "Are", You: "Today"}); | |||
//No longer using copy, so dispose it. | |||
IPCNode.dispose(copy); | |||
}); | |||
} | |||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,56 @@ | |||
var net=require("net"); | |||
var sys=require("sys"); | |||
var IPCNode=require("../../IPCNode").IPCNode; | |||
|
|||
//Main object being made available to the clients | |||
var mainObject={ | |||
counter: 0, | |||
increase: function() { | |||
this.counter++; | |||
}, | |||
get_counter: IPCNode.sync(function() { | |||
return this.counter; | |||
}), | |||
printString: function(msg) { | |||
sys.puts("String: "+msg); | |||
}, | |||
printArray: function(msgs) { | |||
sys.puts("Array:"); | |||
for (var i=0; i<msgs.length; i++) | |||
sys.puts("\t"+msgs[i]); | |||
}, | |||
printSimpleObject: function(msgs) { | |||
sys.puts("Object:"); | |||
for (var i in msgs) | |||
sys.puts("\t"+i+": "+msgs[i]); | |||
} | |||
}; | |||
|
|||
net.createServer(function(client) { | |||
sys.puts("New connection"); | |||
client.on("connect",function() { | |||
sys.puts("Connected"); | |||
var ipc=new IPCNode(); | |||
sys.pump(ipc,client,function() { ipc.end(); }); | |||
sys.pump(client,ipc,function() { client.end(); client.destroy(); }); | |||
|
|||
ipc.on("error",function(e) { | |||
sys.puts("IPC error: "+e.toString()); | |||
client.end(); | |||
client.destroy(); | |||
}); | |||
ipc.on("clean",function(from) { | |||
sys.puts("IPC stream is clean, no more cross-process oject references"); | |||
ipc.end(); | |||
client.end(); | |||
client.destroy(); | |||
}); | |||
|
|||
ipc.register("main",mainObject); | |||
}); | |||
client.on("error",function(e) { | |||
sys.puts("Client error: "+e.toString()); | |||
ipc.end(); | |||
}); | |||
}).listen(81234); | |||
sys.puts("Listening on port 81234"); |