forked from versatica/JsSIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessage.js
278 lines (228 loc) · 5.65 KB
/
Message.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
const EventEmitter = require('events').EventEmitter;
const JsSIP_C = require('./Constants');
const SIPMessage = require('./SIPMessage');
const Utils = require('./Utils');
const RequestSender = require('./RequestSender');
const Exceptions = require('./Exceptions');
const debug = require('debug')('JsSIP:Message');
module.exports = class Message extends EventEmitter
{
constructor(ua)
{
super();
this._ua = ua;
this._request = null;
this._closed = false;
this._direction = null;
this._local_identity = null;
this._remote_identity = null;
// Whether an incoming message has been replied.
this._is_replied = false;
// Custom message empty object for high level use.
this._data = {};
}
get direction()
{
return this._direction;
}
get local_identity()
{
return this._local_identity;
}
get remote_identity()
{
return this._remote_identity;
}
send(target, body, options = {})
{
const originalTarget = target;
if (target === undefined || body === undefined)
{
throw new TypeError('Not enough arguments');
}
// Check target validity.
target = this._ua.normalizeTarget(target);
if (!target)
{
throw new TypeError(`Invalid target: ${originalTarget}`);
}
// Get call options.
const extraHeaders = Utils.cloneArray(options.extraHeaders);
const eventHandlers = Utils.cloneObject(options.eventHandlers);
const contentType = options.contentType || 'text/plain';
// Set event handlers.
for (const event in eventHandlers)
{
if (Object.prototype.hasOwnProperty.call(eventHandlers, event))
{
this.on(event, eventHandlers[event]);
}
}
extraHeaders.push(`Content-Type: ${contentType}`);
this._request = new SIPMessage.OutgoingRequest(
JsSIP_C.MESSAGE, target, this._ua, null, extraHeaders);
if (body)
{
this._request.body = body;
}
const request_sender = new RequestSender(this._ua, this._request, {
onRequestTimeout : () =>
{
this._onRequestTimeout();
},
onTransportError : () =>
{
this._onTransportError();
},
onReceiveResponse : (response) =>
{
this._receiveResponse(response);
}
});
this._newMessage('local', this._request);
request_sender.send();
}
init_incoming(request)
{
this._request = request;
this._newMessage('remote', request);
// Reply with a 200 OK if the user didn't reply.
if (!this._is_replied)
{
this._is_replied = true;
request.reply(200);
}
this._close();
}
/**
* Accept the incoming Message
* Only valid for incoming Messages
*/
accept(options = {})
{
const extraHeaders = Utils.cloneArray(options.extraHeaders);
const body = options.body;
if (this._direction !== 'incoming')
{
throw new Exceptions.NotSupportedError('"accept" not supported for outgoing Message');
}
if (this._is_replied)
{
throw new Error('incoming Message already replied');
}
this._is_replied = true;
this._request.reply(200, null, extraHeaders, body);
}
/**
* Reject the incoming Message
* Only valid for incoming Messages
*/
reject(options = {})
{
const status_code = options.status_code || 480;
const reason_phrase = options.reason_phrase;
const extraHeaders = Utils.cloneArray(options.extraHeaders);
const body = options.body;
if (this._direction !== 'incoming')
{
throw new Exceptions.NotSupportedError('"reject" not supported for outgoing Message');
}
if (this._is_replied)
{
throw new Error('incoming Message already replied');
}
if (status_code < 300 || status_code >= 700)
{
throw new TypeError(`Invalid status_code: ${status_code}`);
}
this._is_replied = true;
this._request.reply(status_code, reason_phrase, extraHeaders, body);
}
_receiveResponse(response)
{
if (this._closed)
{
return;
}
switch (true)
{
case /^1[0-9]{2}$/.test(response.status_code):
// Ignore provisional responses.
break;
case /^2[0-9]{2}$/.test(response.status_code):
this._succeeded('remote', response);
break;
default:
{
const cause = Utils.sipErrorCause(response.status_code);
this._failed('remote', response, cause);
break;
}
}
}
_onRequestTimeout()
{
if (this._closed)
{
return;
}
this._failed('system', null, JsSIP_C.causes.REQUEST_TIMEOUT);
}
_onTransportError()
{
if (this._closed)
{
return;
}
this._failed('system', null, JsSIP_C.causes.CONNECTION_ERROR);
}
_close()
{
this._closed = true;
this._ua.destroyMessage(this);
}
/**
* Internal Callbacks
*/
_newMessage(originator, request)
{
if (originator === 'remote')
{
this._direction = 'incoming';
this._local_identity = request.to;
this._remote_identity = request.from;
}
else if (originator === 'local')
{
this._direction = 'outgoing';
this._local_identity = request.from;
this._remote_identity = request.to;
}
this._ua.newMessage(this, {
originator,
message : this,
request
});
}
_failed(originator, response, cause)
{
debug('MESSAGE failed');
this._close();
debug('emit "failed"');
this.emit('failed', {
originator,
response : response || null,
cause
});
}
_succeeded(originator, response)
{
debug('MESSAGE succeeded');
this._close();
debug('emit "succeeded"');
this.emit('succeeded', {
originator,
response
});
}
};