Commit e81813231184d5ab90a6457a20746073ccd7dbc0

Authored by Adhidarma Hadiwinoto
1 parent 8ef0a5382d
Exists in master

Terminate on error.

Can be disabled by config.do_not_terminate_on_error

Showing 2 changed files with 16 additions and 3 deletions Inline Diff

1 { 1 {
2 "origin": "PLEASE_CHANGE_ME", 2 "origin": "PLEASE_CHANGE_ME",
3 "username": "PLEASE_CHANGE_ME", 3 "username": "PLEASE_CHANGE_ME",
4 "password": "PLEASE_CHANGE_ME", 4 "password": "PLEASE_CHANGE_ME",
5 "warming_up_ms": 1000, 5 "warming_up_ms": 1000,
6 "messaging_url": "http://localhost:32979/", 6 "messaging_url": "http://localhost:32979/",
7 "listen_port": 32989, 7 "listen_port": 32989,
8 "ping_interval_ms": 60000 8 "ping_interval_ms": 60000,
9 "do_not_terminate_on_error": false
9 } 10 }
10 11
1 "use strict"; 1 const MODULE_NAME = 'TRANSPORT';
2 2
3 const bot = require("simple-xmpp"); 3 const bot = require("simple-xmpp");
4 const config = require('komodo-sdk/config'); 4 const config = require('komodo-sdk/config');
5 const logger = require('komodo-sdk/logger'); 5 const logger = require('komodo-sdk/logger');
6 const messagingService = require('komodo-center-messaging-client-lib'); 6 const messagingService = require('komodo-center-messaging-client-lib');
7 7
8 let isReady; 8 let isReady;
9 9
10 bot.on('online', function(data) { 10 bot.on('online', function(data) {
11 logger.info('XMPP transport connected, JID: ' + data.jid.user); 11 logger.info('XMPP transport connected, JID: ' + data.jid.user);
12 bot.getRoster(); 12 bot.getRoster();
13 13
14 setTimeout( 14 setTimeout(
15 function() { 15 function() {
16 isReady = true; 16 isReady = true;
17 17
18 logger.verbose('Transport is ready'); 18 logger.verbose('Transport is ready');
19 19
20 if (messagingService.onOnline) { 20 if (messagingService.onOnline) {
21 messagingService.onOnline({ 21 messagingService.onOnline({
22 me: config.username, 22 me: config.username,
23 }); 23 });
24 } 24 }
25 }, 25 },
26 26
27 config.warming_up_ms || (30 * 1000) 27 config.warming_up_ms || (30 * 1000)
28 ) 28 )
29 }) 29 })
30 30
31 bot.on('chat', function(partner, msg) { 31 bot.on('chat', function(partner, msg) {
32 if (!msg || !msg.trim()) { 32 if (!msg || !msg.trim()) {
33 return; 33 return;
34 } 34 }
35 35
36 if (partner == config.username.replace(/\/.*$/, '')) { 36 if (partner == config.username.replace(/\/.*$/, '')) {
37 return; 37 return;
38 } 38 }
39 39
40 if (!isReady) { 40 if (!isReady) {
41 logger.warn('Warming up is not finished yet, ignoring message', {me: config.username, partner: partner, msg: msg}); 41 logger.warn('Warming up is not finished yet, ignoring message', {me: config.username, partner: partner, msg: msg});
42 return; 42 return;
43 } 43 }
44 44
45 logger.info('Incoming message via XMPP transport', {me: config.username, partner: partner, msg: msg}); 45 logger.info('Incoming message via XMPP transport', {me: config.username, partner: partner, msg: msg});
46 46
47 if (messagingService && messagingService.onIncomingMessage) { 47 if (messagingService && messagingService.onIncomingMessage) {
48 messagingService.onIncomingMessage( 48 messagingService.onIncomingMessage(
49 { 49 {
50 me: config.username, 50 me: config.username,
51 partner: partner, 51 partner: partner,
52 msg: msg.trim() 52 msg: msg.trim()
53 } 53 }
54 ) 54 )
55 } 55 }
56 }) 56 });
57
58 bot.on('error', (err) => {
59 logger.warn(`${MODULE_NAME} F2E53C12: Error detected.`, {
60 eCode: err.code,
61 eMessage: err.message,
62 });
63
64 if (!config.do_not_terminate_on_error) {
65 logger.warn(`${MODULE_NAME} BA6C0C55: Terminating on error`);
66 process.exit(1);
67 }
68 });
57 69
58 function send(partner, msg) { 70 function send(partner, msg) {
59 logger.verbose('Sending message via XMPP transport', {transport: 'xmpp', me: config.username, partner: partner, msg: msg}); 71 logger.verbose('Sending message via XMPP transport', {transport: 'xmpp', me: config.username, partner: partner, msg: msg});
60 bot.send(partner, msg); 72 bot.send(partner, msg);
61 } 73 }
62 74
63 bot.on('subscribe', function(from) { 75 bot.on('subscribe', function(from) {
64 logger.verbose('Incoming subscribe request from ' + from); 76 logger.verbose('Incoming subscribe request from ' + from);
65 bot.acceptSubscription(from); 77 bot.acceptSubscription(from);
66 bot.subscribe(from); 78 bot.subscribe(from);
67 }) 79 })
68 80
69 function ping() { 81 function ping() {
70 isReady && bot.send(config.username, 'PING!'); 82 isReady && bot.send(config.username, 'PING!');
71 } 83 }
72 84
73 function init() { 85 function init() {
74 messagingService.setTransport(exports); 86 messagingService.setTransport(exports);
75 87
76 bot.connect({ 88 bot.connect({
77 jid: config.username, 89 jid: config.username,
78 password: config.password, 90 password: config.password,
79 host: config.xmpp_host 91 host: config.xmpp_host
80 }); 92 });
81 93
82 setInterval( 94 setInterval(
83 ping, 95 ping,
84 config.ping_interval_ms || 60000 96 config.ping_interval_ms || 60000
85 ) 97 )
86 } 98 }
87 99
88 init(); 100 init();
89 101
90 exports.init = init; 102 exports.init = init;
91 exports.send = send; 103 exports.send = send;