forked from Roave/shorty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver-example.js
executable file
·108 lines (88 loc) · 3.03 KB
/
server-example.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/local/bin/node
/**
* This file is part of Shorty.
*
* Shorty is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3 of the License.
*
* Shorty is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Shorty. If not, see <http://www.gnu.org/licenses/>.
*
* @category shorty
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPL
* @copyright Copyright 2010 Evan Coury (http://www.Evan.pro/)
* @package examples
*/
var shorty = require('./lib/shorty'),
util = require('util');
var messageId = 0;
var shortyServer = shorty.createServer('config.json');
// all event handlers must be set up before calling shortyServer.start()
shortyServer.on('bind', function(pdu, client, callback) {
callback("ESME_ROK");
});
shortyServer.on('bindSuccess', function(client, pdu) {
console.log('bind success');
});
shortyServer.on('deliver_sm_resp', function(client, pdu) {
console.log("sms marked as delivered: " + pdu.sequence_number);
});
shortyServer.on('unbind', function(client, pdu) {
console.log("client unbinding");
});
shortyServer.on('unbind_resp', function(client, pdu) {
console.log("client unbound");
});
// client info, pdu info, callback(messageId, status)
shortyServer.on('submit_sm', function(clientInfo, pdu, callback) {
var source = pdu.source_addr.toString('ascii'),
dest = pdu.destination_addr.toString('ascii');
console.log(pdu.short_message.toString('ascii'));
// Any messages sent from this number will fail
if (pdu.sender === "15555551234") {
// indicate failure
callback("ESME_RSUBMITFAIL", messageId++);
} else {
// indicate success
callback("ESME_ROK", messageId++);
}
});
shortyServer.start();
process.openStdin();
// called every time the user writes a line on stdin
process.stdin.on('data', function(chunk) {
var line, parts, message, i, id;
// buffer to a string
line = chunk.toString();
// remove the newline at the end
line = line.substr(0, line.length - 1);
// split by spaces
parts = line.split(" ");
// put the message back together
message = "";
for (i = 2; i < parts.length; i++) {
message += parts[i] + " ";
}
id = shortyServer.deliverMessage('SMSCLOUD', {
'source_addr': parts[0],
'destination_addr': parts[1],
'sm_length': Buffer.byteLength(message),
'short_message': new Buffer(message)
});
});
var sighandle = function() {
shortyServer.stop();
process.exit();
};
process.on('SIGHUP', sighandle);
process.on('SIGINT', sighandle);
process.on('SIGQUIT', sighandle);
process.on('SIGKILL', sighandle);
process.on('SIGTERM', sighandle);
process.on('SIGSTOP', sighandle);