|
| 1 | +import 'package:sip_ua/src/name_addr_header.dart'; |
| 2 | +import 'constants.dart' as DartSIP_C; |
| 3 | +import 'constants.dart'; |
| 4 | +import 'event_manager/event_manager.dart'; |
| 5 | +import 'event_manager/internal_events.dart'; |
| 6 | +import 'exceptions.dart' as Exceptions; |
| 7 | +import 'logger.dart'; |
| 8 | +import 'request_sender.dart'; |
| 9 | +import 'sip_message.dart'; |
| 10 | +import 'ua.dart'; |
| 11 | +import 'uri.dart'; |
| 12 | +import 'utils.dart' as Utils; |
| 13 | + |
| 14 | +class Options extends EventManager with Applicant { |
| 15 | + Options(UA ua) { |
| 16 | + _ua = ua; |
| 17 | + _request = null; |
| 18 | + _closed = false; |
| 19 | + |
| 20 | + _direction = null; |
| 21 | + _local_identity = null; |
| 22 | + _remote_identity = null; |
| 23 | + |
| 24 | + // Whether an incoming Options has been replied. |
| 25 | + _is_replied = false; |
| 26 | + |
| 27 | + // Custom Options empty object for high level use. |
| 28 | + _data = <String, dynamic>{}; |
| 29 | + } |
| 30 | + |
| 31 | + UA? _ua; |
| 32 | + dynamic _request; |
| 33 | + bool? _closed; |
| 34 | + String? _direction; |
| 35 | + NameAddrHeader? _local_identity; |
| 36 | + NameAddrHeader? _remote_identity; |
| 37 | + bool? _is_replied; |
| 38 | + Map<String, dynamic>? _data; |
| 39 | + String? get direction => _direction; |
| 40 | + |
| 41 | + NameAddrHeader? get local_identity => _local_identity; |
| 42 | + |
| 43 | + NameAddrHeader? get remote_identity => _remote_identity; |
| 44 | + |
| 45 | + Map<String, dynamic>? get data => _data; |
| 46 | + |
| 47 | + void send(String target, String body, [Map<String, dynamic>? options]) { |
| 48 | + String originalTarget = target; |
| 49 | + options = options ?? <String, dynamic>{}; |
| 50 | + |
| 51 | + if (target == null) { |
| 52 | + throw Exceptions.TypeError('A target is required for OPTIONS'); |
| 53 | + } |
| 54 | + |
| 55 | + // Check target validity. |
| 56 | + URI? normalized = _ua!.normalizeTarget(target); |
| 57 | + if (normalized == null) { |
| 58 | + throw Exceptions.TypeError('Invalid target: $originalTarget'); |
| 59 | + } |
| 60 | + |
| 61 | + // Get call options. |
| 62 | + List<dynamic> extraHeaders = Utils.cloneArray(options['extraHeaders']); |
| 63 | + EventManager eventHandlers = options['eventHandlers'] ?? EventManager(); |
| 64 | + String contentType = options['contentType'] ?? 'application/sdp'; |
| 65 | + |
| 66 | + // Set event handlers. |
| 67 | + addAllEventHandlers(eventHandlers); |
| 68 | + |
| 69 | + extraHeaders.add('Content-Type: $contentType'); |
| 70 | + |
| 71 | + _request = |
| 72 | + OutgoingRequest(SipMethod.OPTIONS, normalized, _ua, null, extraHeaders); |
| 73 | + if (body != null) { |
| 74 | + _request.body = body; |
| 75 | + } |
| 76 | + |
| 77 | + EventManager handlers = EventManager(); |
| 78 | + handlers.on(EventOnRequestTimeout(), (EventOnRequestTimeout value) { |
| 79 | + _onRequestTimeout(); |
| 80 | + }); |
| 81 | + handlers.on(EventOnTransportError(), (EventOnTransportError value) { |
| 82 | + _onTransportError(); |
| 83 | + }); |
| 84 | + handlers.on(EventOnReceiveResponse(), (EventOnReceiveResponse event) { |
| 85 | + _receiveResponse(event.response); |
| 86 | + }); |
| 87 | + |
| 88 | + RequestSender request_sender = RequestSender(_ua!, _request, handlers); |
| 89 | + |
| 90 | + _newOptions('local', _request); |
| 91 | + |
| 92 | + request_sender.send(); |
| 93 | + } |
| 94 | + |
| 95 | + void init_incoming(IncomingRequest request) { |
| 96 | + _request = request; |
| 97 | + |
| 98 | + _newOptions('remote', request); |
| 99 | + |
| 100 | + // Reply with a 200 OK if the user didn't reply. |
| 101 | + if (_is_replied == null || _is_replied == false) { |
| 102 | + _is_replied = true; |
| 103 | + request.reply(200); |
| 104 | + } |
| 105 | + |
| 106 | + close(); |
| 107 | + } |
| 108 | + |
| 109 | + /* |
| 110 | + * Accept the incoming Options |
| 111 | + * Only valid for incoming Options |
| 112 | + */ |
| 113 | + void accept(Map<String, dynamic> options) { |
| 114 | + List<dynamic> extraHeaders = Utils.cloneArray(options['extraHeaders']); |
| 115 | + String? body = options['body']; |
| 116 | + |
| 117 | + if (_direction != 'incoming') { |
| 118 | + throw Exceptions.NotSupportedError( |
| 119 | + '"accept" not supported for outgoing Options'); |
| 120 | + } |
| 121 | + |
| 122 | + if (_is_replied != null) { |
| 123 | + throw AssertionError('incoming Options already replied'); |
| 124 | + } |
| 125 | + |
| 126 | + _is_replied = true; |
| 127 | + _request.reply(200, null, extraHeaders, body); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Reject the incoming Options |
| 132 | + * Only valid for incoming Optionss |
| 133 | + */ |
| 134 | + void reject(Map<String, dynamic> options) { |
| 135 | + int status_code = options['status_code'] ?? 480; |
| 136 | + String? reason_phrase = options['reason_phrase']; |
| 137 | + List<dynamic> extraHeaders = Utils.cloneArray(options['extraHeaders']); |
| 138 | + String? body = options['body']; |
| 139 | + |
| 140 | + if (_direction != 'incoming') { |
| 141 | + throw Exceptions.NotSupportedError( |
| 142 | + '"reject" not supported for outgoing Options'); |
| 143 | + } |
| 144 | + |
| 145 | + if (_is_replied != null) { |
| 146 | + throw AssertionError('incoming Options already replied'); |
| 147 | + } |
| 148 | + |
| 149 | + if (status_code < 300 || status_code >= 700) { |
| 150 | + throw Exceptions.TypeError('Invalid status_code: $status_code'); |
| 151 | + } |
| 152 | + |
| 153 | + _is_replied = true; |
| 154 | + _request.reply(status_code, reason_phrase, extraHeaders, body); |
| 155 | + } |
| 156 | + |
| 157 | + void _receiveResponse(IncomingResponse? response) { |
| 158 | + if (_closed != null) { |
| 159 | + return; |
| 160 | + } |
| 161 | + if (RegExp(r'^1[0-9]{2}$').hasMatch(response!.status_code)) { |
| 162 | + // Ignore provisional responses. |
| 163 | + } else if (RegExp(r'^2[0-9]{2}$').hasMatch(response.status_code)) { |
| 164 | + _succeeded('remote', response); |
| 165 | + } else { |
| 166 | + String cause = Utils.sipErrorCause(response.status_code); |
| 167 | + _failed('remote', response.status_code, cause, response.reason_phrase); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + void _onRequestTimeout() { |
| 172 | + if (_closed != null) { |
| 173 | + return; |
| 174 | + } |
| 175 | + _failed( |
| 176 | + 'system', 408, DartSIP_C.CausesType.REQUEST_TIMEOUT, 'Request Timeout'); |
| 177 | + } |
| 178 | + |
| 179 | + void _onTransportError() { |
| 180 | + if (_closed != null) { |
| 181 | + return; |
| 182 | + } |
| 183 | + _failed('system', 500, DartSIP_C.CausesType.CONNECTION_ERROR, |
| 184 | + 'Transport Error'); |
| 185 | + } |
| 186 | + |
| 187 | + @override |
| 188 | + void close() { |
| 189 | + _closed = true; |
| 190 | + _ua!.destroyOptions(this); |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Internal Callbacks |
| 195 | + */ |
| 196 | + |
| 197 | + void _newOptions(String originator, dynamic request) { |
| 198 | + if (originator == 'remote') { |
| 199 | + _direction = 'incoming'; |
| 200 | + _local_identity = request.to; |
| 201 | + _remote_identity = request.from; |
| 202 | + } else if (originator == 'local') { |
| 203 | + _direction = 'outgoing'; |
| 204 | + _local_identity = request.from; |
| 205 | + _remote_identity = request.to; |
| 206 | + } |
| 207 | + |
| 208 | + _ua!.newOptions(this, originator, request); |
| 209 | + } |
| 210 | + |
| 211 | + void _failed(String originator, int? status_code, String cause, |
| 212 | + String? reason_phrase) { |
| 213 | + logger.debug('OPTIONS failed'); |
| 214 | + close(); |
| 215 | + logger.debug('emit "failed"'); |
| 216 | + emit(EventCallFailed( |
| 217 | + originator: originator, |
| 218 | + cause: ErrorCause( |
| 219 | + cause: cause, |
| 220 | + status_code: status_code, |
| 221 | + reason_phrase: reason_phrase))); |
| 222 | + } |
| 223 | + |
| 224 | + void _succeeded(String originator, IncomingResponse? response) { |
| 225 | + logger.debug('OPTIONS succeeded'); |
| 226 | + |
| 227 | + close(); |
| 228 | + |
| 229 | + logger.debug('emit "succeeded"'); |
| 230 | + |
| 231 | + emit(EventSucceeded(originator: originator, response: response)); |
| 232 | + } |
| 233 | +} |
0 commit comments