-
Notifications
You must be signed in to change notification settings - Fork 44
/
example-host.js
77 lines (68 loc) · 2.48 KB
/
example-host.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var alljoyn = require('./');
console.log("Test loading alljoyn bus...", alljoyn);
var sessionId = 0;
var portNumber = 27;
var advertisedName = "org.alljoyn.bus.samples.chat.test";
var bus = alljoyn.BusAttachment("chat");
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);
setTimeout(function(){
chatObject.signal(null, sessionId, inter, "Chat", "Hello, I am the host!");
}, 1000);
}
);
console.log("CreateInterface "+bus.createInterface("org.alljoyn.bus.samples.chat", inter));
console.log("AddSignal "+inter.addSignal("Chat", "s", "msg"));
bus.registerBusListener(listener);
console.log("Start "+bus.start());
var chatObject = alljoyn.BusObject("/chatService");
console.log("chat.AddInterface "+chatObject.addInterface(inter));
console.log("RegisterSignalHandler "+bus.registerSignalHandler(chatObject, function(msg, info){
// console.log("Signal received: ", msg, info);
console.log(msg["0"]);
}, inter, "Chat"));
console.log("RegisterBusObject "+bus.registerBusObject(chatObject));
console.log("Connect "+bus.connect());
console.log("RequestName "+bus.requestName(advertisedName));
console.log("BindSessionPort "+bus.bindSessionPort(27, 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();
}
// write the key to stdout all normal like
process.stdout.write( key + '\n' );
// chatObject.signal(null, sessionId, inter, 'hello' );
chatObject.signal(null, sessionId, inter, "Chat", key);
});