UNPKG

9.14 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11var __generator = (this && this.__generator) || function (thisArg, body) {
12 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14 function verb(n) { return function (v) { return step([n, v]); }; }
15 function step(op) {
16 if (f) throw new TypeError("Generator is already executing.");
17 while (_) try {
18 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19 if (y = 0, t) op = [op[0] & 2, t.value];
20 switch (op[0]) {
21 case 0: case 1: t = op; break;
22 case 4: _.label++; return { value: op[1], done: false };
23 case 5: _.label++; y = op[1]; op = [0]; continue;
24 case 7: op = _.ops.pop(); _.trys.pop(); continue;
25 default:
26 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30 if (t[2]) _.ops.pop();
31 _.trys.pop(); continue;
32 }
33 op = body.call(thisArg, _);
34 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36 }
37};
38Object.defineProperty(exports, "__esModule", { value: true });
39var cyclon_p2p_common_1 = require("cyclon.p2p-common");
40var CYCLON_SHUFFLE_CHANNEL_TYPE = 'cyclonShuffle';
41var WebRTCComms = /** @class */ (function () {
42 function WebRTCComms(rtc, shuffleStateFactory, logger, roomsToJoin) {
43 this.rtc = rtc;
44 this.shuffleStateFactory = shuffleStateFactory;
45 this.logger = logger;
46 this.roomsToJoin = roomsToJoin;
47 if (!(roomsToJoin && roomsToJoin.length > 0)) {
48 throw new Error('Must specify at least one room to join');
49 }
50 }
51 /**
52 * Initialize the Comms object
53 *
54 * @param localNode The local Cyclon node
55 * @param metadataProviders
56 */
57 WebRTCComms.prototype.initialize = function (localNode, metadataProviders) {
58 var _this = this;
59 this.localNode = localNode;
60 this.rtc.connect(metadataProviders, this.roomsToJoin);
61 this.rtc.onChannel('cyclonShuffle', function (channel) { return _this.handleIncomingShuffle(channel); });
62 this.rtc.on('incomingTimeout', function (channelType, sourcePointer) {
63 if (channelType === CYCLON_SHUFFLE_CHANNEL_TYPE) {
64 _this.requireLocalNode().emit('shuffleTimeout', 'incoming', sourcePointer);
65 }
66 });
67 this.rtc.on('incomingError', function (channelType, sourcePointer, error) {
68 if (channelType === CYCLON_SHUFFLE_CHANNEL_TYPE) {
69 _this.logger.error('An error occurred on an incoming shuffle', error);
70 _this.requireLocalNode().emit('shuffleError', 'incoming', sourcePointer, error);
71 }
72 });
73 this.rtc.on('offerReceived', function (channelType, sourcePointer) {
74 if (channelType === CYCLON_SHUFFLE_CHANNEL_TYPE) {
75 _this.logger.debug("Incoming shuffle starting with " + sourcePointer.id);
76 _this.requireLocalNode().emit('shuffleStarted', 'incoming', sourcePointer);
77 }
78 });
79 };
80 /**
81 * Send a shuffle request to another node
82 *
83 * @param destinationNodePointer
84 * @param shuffleSet
85 */
86 WebRTCComms.prototype.sendShuffleRequest = function (destinationNodePointer, shuffleSet) {
87 return __awaiter(this, void 0, void 0, function () {
88 return __generator(this, function (_a) {
89 switch (_a.label) {
90 case 0:
91 this.lastShuffleNode = destinationNodePointer;
92 return [4 /*yield*/, this.createOutgoingShuffle(this.shuffleStateFactory.createOutgoingShuffleState(this.requireLocalNode(), destinationNodePointer, shuffleSet), destinationNodePointer)];
93 case 1: return [2 /*return*/, _a.sent()];
94 }
95 });
96 });
97 };
98 WebRTCComms.prototype.createOutgoingShuffle = function (outgoingState, destinationNodePointer) {
99 return __awaiter(this, void 0, void 0, function () {
100 var channel;
101 return __generator(this, function (_a) {
102 switch (_a.label) {
103 case 0:
104 _a.trys.push([0, , 4, 5]);
105 return [4 /*yield*/, this.rtc.openChannel(CYCLON_SHUFFLE_CHANNEL_TYPE, destinationNodePointer)];
106 case 1:
107 channel = _a.sent();
108 outgoingState.storeChannel(channel);
109 outgoingState.sendShuffleRequest();
110 return [4 /*yield*/, outgoingState.processShuffleResponse()];
111 case 2:
112 _a.sent();
113 return [4 /*yield*/, outgoingState.sendResponseAcknowledgement()];
114 case 3:
115 _a.sent();
116 return [3 /*break*/, 5];
117 case 4:
118 outgoingState.close();
119 return [7 /*endfinally*/];
120 case 5: return [2 /*return*/];
121 }
122 });
123 });
124 };
125 WebRTCComms.prototype.createNewPointer = function () {
126 return this.rtc.createNewPointer();
127 };
128 WebRTCComms.prototype.getLocalId = function () {
129 return this.rtc.getLocalId();
130 };
131 /**
132 * Handle an incoming shuffle
133 */
134 WebRTCComms.prototype.handleIncomingShuffle = function (channel) {
135 return __awaiter(this, void 0, void 0, function () {
136 var remotePeer, incomingShuffleState, e_1;
137 return __generator(this, function (_a) {
138 switch (_a.label) {
139 case 0:
140 remotePeer = channel.getRemotePeer();
141 incomingShuffleState = this.shuffleStateFactory.createIncomingShuffleState(this.requireLocalNode(), remotePeer);
142 _a.label = 1;
143 case 1:
144 _a.trys.push([1, 4, 5, 6]);
145 return [4 /*yield*/, incomingShuffleState.processShuffleRequest(channel)];
146 case 2:
147 _a.sent();
148 return [4 /*yield*/, incomingShuffleState.waitForResponseAcknowledgement(channel)];
149 case 3:
150 _a.sent();
151 this.requireLocalNode().emit('shuffleCompleted', 'incoming', remotePeer);
152 return [3 /*break*/, 6];
153 case 4:
154 e_1 = _a.sent();
155 if (e_1 instanceof cyclon_p2p_common_1.TimeoutError) {
156 this.logger.warn(e_1.message);
157 this.requireLocalNode().emit('shuffleTimeout', 'incoming', remotePeer);
158 }
159 else {
160 this.logger.error('An unknown error occurred on an incoming shuffle', e_1);
161 this.requireLocalNode().emit('shuffleError', 'incoming', remotePeer, 'unknown');
162 }
163 return [3 /*break*/, 6];
164 case 5:
165 channel.close();
166 return [7 /*endfinally*/];
167 case 6: return [2 /*return*/];
168 }
169 });
170 });
171 };
172 WebRTCComms.prototype.requireLocalNode = function () {
173 if (this.localNode) {
174 return this.localNode;
175 }
176 else {
177 throw new Error('Comms not yet initialized (localNode is not defined)');
178 }
179 };
180 return WebRTCComms;
181}());
182exports.WebRTCComms = WebRTCComms;
183//# sourceMappingURL=WebRTCComms.js.map
\No newline at end of file