Last active
August 29, 2015 14:18
-
-
Save sgrebnov/98b2032956e9b9a3d038 to your computer and use it in GitHub Desktop.
octoblu/alljoyn method calls sample
This file contains hidden or 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
// https://github.com/sgrebnov/alljoyn | |
var alljoyn = require('alljoyn'); | |
var sessionId = 0; | |
var portNumber = 25; | |
var advertisedName = 'org.alljoyn.Bus.sample'; | |
var interfaceName = 'org.alljoyn.Bus.sample'; | |
var interfacePath = '/sample'; | |
function onMethodCall(paramName, chatObject, msg) { | |
console.log('onMethodCall("' + paramName + '")'); | |
chatObject.reply(msg, "Hello " + paramName); | |
} | |
console.log('Starting service ' + advertisedName); | |
var bus = alljoyn.BusAttachment("host"); | |
var inter = alljoyn.InterfaceDescription(); | |
var listener = alljoyn.BusListener( | |
function(name){ | |
console.log("FoundAdvertisedName", name); | |
sessionId = bus.joinSession(name, portNumber, 0); | |
console.log("JoinSession "+sessionId); | |
}, | |
function(name){ | |
console.log("LostAdvertisedName", name); | |
}, | |
function(name){ | |
console.log("NameOwnerChanged", name); | |
} | |
); | |
var portListener = alljoyn.SessionPortListener( | |
function(port, joiner){ | |
console.log("AcceptSessionJoiner", port, joiner); | |
return port == portNumber; | |
}, | |
function(port, sId, joiner){ | |
sessionId = sId; | |
console.log("SessionJoined", port, sessionId, joiner); | |
} | |
); | |
console.log("CreateInterface "+ bus.createInterface(interfaceName, inter)); | |
console.log("Add method " + inter.addMethod("getParamValue", "s", "s", "paramName,paramValue", 0)); | |
bus.registerBusListener(listener); | |
console.log("Start "+bus.start()); | |
var chatObject = alljoyn.BusObject(interfacePath); | |
console.log("chat.AddInterface "+chatObject.addInterface(inter)); | |
console.log("chat.addMethodHandler "+chatObject.addMethodHandler(inter, 'getParamValue', function(args, msg){ | |
onMethodCall(args['0'], chatObject, msg); | |
})); | |
console.log("RegisterBusObject "+bus.registerBusObject(chatObject)); | |
console.log("Connect "+bus.connect()); | |
console.log("RequestName "+bus.requestName(advertisedName)); | |
console.log("BindSessionPort "+bus.bindSessionPort(portNumber, portListener)); | |
console.log("AdvertiseName "+bus.advertiseName(advertisedName)); | |
// Added Chat to example | |
var stdin = process.stdin; | |
// without this, we would only get streams once enter is pressed | |
stdin.setRawMode( true ); | |
// resume stdin in the parent process (node app won't quit all by itself | |
// unless an error or process.exit() happens) | |
stdin.resume(); | |
// i don't want binary, do you? | |
stdin.setEncoding( 'utf8' ); | |
// on any data into stdin | |
stdin.on( 'data', function( key ){ | |
// ctrl-c ( end of text ) | |
//if ( key === '\u0003' ) { | |
process.exit(); | |
//} | |
//process.stdout.write( key + '\n' ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment