Commit f1caf11c authored by Hardik Juneja's avatar Hardik Juneja Committed by hardikj

gadget_officejs: Add support for websocket

parent 637e2050
......@@ -11,19 +11,22 @@
<script src="handlebars.js"></script>
<script class="view-web-page-template" type="text/x-handlebars-template">
<form class="select-share-type">
<div class="center">
<div class="ui-field-contain">
<label data-i18n="Public Link:"> Public Link: </label>
<button type="submit" id="generatelink" name="generatelink" data-i18n="Jio Link" style="display: inline-block; width: 34%;"> Jio Link </button>
<button type="submit" id="generatelink1" name="generatelink1" data-i18n="Websocket Link" style="display: inline-block; width: 34%;"> Websocket Link </button>
<input type="text" id="sharelink" name="share" style="display:none" value=""/>
</div>
</div>
</form>
<form class="view-web-page-form">
<div class="center">
<div class="ui-field-contain">
<label data-i18n="Title:">Title:</label>
<input type="text" name="title" value="{{title}}">
</div>
<div class="ui-field-contain">
<label data-i18n="Public Link:"> Public Link: </label>
<button type="submit" id="generatelink" name="generatelink" data-i18n="Generate Link" style="display:block;"> Generate Link </button>
<input type="text" id="sharelink" name="share" style="display:none" value=""/>
</div>
<button type="submit" data-i18n="Save" style="display:none;">Save</button>
</div>
......
......@@ -232,7 +232,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>953.27768.8271.35532</string> </value>
<value> <string>954.5706.38367.28381</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -250,7 +250,7 @@
</tuple>
<state>
<tuple>
<float>1471964256.18</float>
<float>1474551059.28</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -28,6 +28,60 @@
return gadget.put(gadget.options.jio_key, doc);
});
}
function shareDoc (gadget, type) {
if(window.location.hash) {
var index = window.location.hash.indexOf('room=');
if (index === -1) {
var S4 = function() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
};
var room = (S4()+S4()+"-"+S4()+S4());
return new RSVP.Queue()
.push(function () {
return gadget.declareGadget(
"gadget_officejs_page_share_text.html",
{
scope: "share_text_via_webrtc",
element: gadget.props.element.querySelector(".webrtc-gadget")
}
);
})
.push(function(g){
var config;
if (type === "jio") {
config = {
type: "query",
sub_storage: {
type: "uuid",
sub_storage: {
"type": "dav",
"url": "https://softinst67513.host.vifib.net/share/",
"basic_login": btoa("admin:vowhkida")
}
}
};
} else {
config = {url: "ws://192.168.242.146:9999/"};
}
var sharelink = window.location.origin + window.location.pathname + '#page=webrtc_viewer&room='+room+'&type='+type+'&config='+encodeURIComponent(JSON.stringify(config));
gadget.props.element.querySelector('#sharelink').value = sharelink;
gadget.props.element.querySelector('#sharelink').style.display = 'block';
gadget.props.element.querySelector('#generatelink').remove();
gadget.props.element.querySelector('#generatelink1').remove();
if(config) {
return g.initiate(room, gadget, type, config);
} else {
throw new Error("Share config is missing");
}
})
}
}
}
function maximize(gadget) {
var iframe = gadget.props.element.querySelector('iframe'),
......@@ -110,55 +164,6 @@
return gadget.props.deferred.resolve();
});
})
.declareMethod("shareDoc", function () {
var gadget = this;
if(window.location.hash) {
var index = window.location.hash.indexOf('room=');
if (index === -1) {
var S4 = function() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
};
var room = (S4()+S4()+"-"+S4()+S4());
return new RSVP.Queue()
.push(function () {
return gadget.declareGadget(
"gadget_officejs_page_share_text.html",
{
scope: "share_text_via_webrtc",
element: gadget.props.element.querySelector(".webrtc-gadget")
}
);
})
.push(function(g){
var config = {
type: "query",
sub_storage: {
type: "uuid",
sub_storage: {
//type: "indexeddb",
//"database": "handshake"
"type": "dav",
"url": "https://softinst67513.host.vifib.net/share/",
"basic_login": btoa("admin:vowhkida")
}
}
}
var sharelink = window.location.origin + window.location.pathname + '#page=webrtc_viewer&room='+room+'&config='+encodeURIComponent(JSON.stringify(config));
gadget.props.element.querySelector('#sharelink').value = sharelink;
gadget.props.element.querySelector('#sharelink').style.display = 'block';
gadget.props.element.querySelector('#generatelink').remove();
if(config) {
return g.initiate(room, gadget, config);
} else {
return g.initiate(room, gadget);
}
})
}
}
})
/////////////////////////////////////////
// Render text content gadget
......@@ -208,19 +213,29 @@
.push(function () {
return gadget.props.deferred.promise;
})
.push(function () {
return loopEventListener(
gadget.props.element.querySelector('form'),
'submit',
true,
function (event) {
if (document.activeElement.name === "generatelink") {
return gadget.shareDoc();
} else {
.push(function () {
return RSVP.all([
loopEventListener(
gadget.props.element.querySelector('form.select-share-type'),
'submit',
true,
function (event) {
if (document.activeElement.name === "generatelink") {
return shareDoc(gadget, "jio");
} else if (document.activeElement.name === "generatelink1") {
return shareDoc(gadget, "websocket");
}
}
),
loopEventListener(
gadget.props.element.querySelector('form.view-web-page-form'),
'submit',
true,
function (event) {
return saveContent(gadget, event);
}
}
);
)
]);
});
});
......
......@@ -228,7 +228,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>953.38231.58110.12526</string> </value>
<value> <string>954.5702.26020.8277</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -246,7 +246,7 @@
</tuple>
<state>
<tuple>
<float>1472570624.98</float>
<float>1474550862.46</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -12,6 +12,8 @@
</head>
<body>
<div class="gadget_webrtc_jio_bridge"></div>
<div data-gadget-url="gadget_webrtc_jio_bridge.html"
data-gadget-scope="gadget_webrtc_jio_bridge.html"
data-gadget-sandbox="public"></div>
</body>
</html>
......@@ -234,7 +234,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>953.38184.65477.60859</string> </value>
<value> <string>953.57026.12963.53469</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -252,7 +252,7 @@
</tuple>
<state>
<tuple>
<float>1472631749.27</float>
<float>1473758929.92</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -242,7 +242,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>953.29508.22380.37649</string> </value>
<value> <string>954.5733.26518.27989</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -260,7 +260,7 @@
</tuple>
<state>
<tuple>
<float>1472047040.89</float>
<float>1474552757.01</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -22,11 +22,9 @@
</div>
<div class="ui-block-c"></div>
</div>
<div class="gadget_websocket">
<div data-gadget-url="gadget_websocket.html" data-gadget-scope="gadget_websocket.html">
</div>
</div>
<div class="gadget_webrtc_datachannel"></div>
<div data-gadget-url="gadget_webrtc_jio_bridge.html"
data-gadget-scope="gadget_webrtc_jio_bridge.html"
data-gadget-sandbox="public"></div>
</body>
</html>
......@@ -236,7 +236,7 @@
</item>
<item>
<key> <string>actor</string> </key>
<value> <string>romain</string> </value>
<value> <string>zope</string> </value>
</item>
<item>
<key> <string>comment</string> </key>
......@@ -250,7 +250,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>945.33680.60468.51660</string> </value>
<value> <string>953.42397.2191.24098</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -268,8 +268,8 @@
</tuple>
<state>
<tuple>
<float>1441358472.38</float>
<string>GMT</string>
<float>1474364886.77</float>
<string>UTC</string>
</tuple>
</state>
</object>
......
......@@ -17,13 +17,28 @@
});
}
function declareSubGadget(gadget, url) {
var container_element = gadget.state_parameter_dict.element.querySelector("." + url.split(".")[0]),
element = document.createElement("div");
container_element.innerHTML = "";
container_element.appendChild(element);
return gadget.declareGadget(url, {
element: element,
scope: url,
sandbox: "public"
});
}
function getWebRTCScopeList(gadget) {
var result_list = [],
element_list = gadget.state_parameter_dict.element.querySelector(".gadget_webrtc_datachannel")
element_list = gadget.state_parameter_dict.element.querySelector(".gadget_webrtc")
.childNodes,
i;
for (i = 0; i < element_list.length; i += 1) {
result_list.push(element_list[i].getAttribute("data-gadget-scope"));
if (element_list[i].getAttribute) {
result_list.push(element_list[i].getAttribute("data-gadget-scope"));
}
}
return result_list;
}
......@@ -40,7 +55,7 @@
}
function sendWebRTC(gadget, rtc_gadget, scope, message) {
return rtc_gadget.send(message)
return rtc_gadget.send(message, "webrtc"+gadget.state_parameter_dict.counter)
.push(undefined, function (error) {
if ((error instanceof DOMException) && (error.name === 'InvalidStateError')) {
return dropSubGadget(gadget, scope)
......@@ -114,96 +129,70 @@
}));
});
})
/*
.allowPublicAcquisition("notifyWebSocketClosed", function () {
if (this.state_parameter_dict.user_type !== "user") {
throw new Error("Unexpected Web Socket connection close");
}
})
*/
.allowPublicAcquisition("notifyWebSocketMessage", function (argument_list) {
var json = JSON.parse(argument_list[0]),
scope,
rtc_gadget,
socket_gadget,
gadget = this;
if (json.action === "offer") {
// XXX https://github.com/diafygi/webrtc-ips
return gadget.getDeclaredGadget("gadget_websocket.html")
.push(function (gg) {
gadget.state_parameter_dict.connecting = true;
gadget.state_parameter_dict.counter += 1;
socket_gadget = gg;
var new_element = document.createElement("div");
gadget.state_parameter_dict.element.querySelector(".gadget_webrtc_datachannel").appendChild(new_element);
scope = "webrtc" + gadget.state_parameter_dict.counter;
return gadget.declareGadget("gadget_webrtc_datachannel.html", {
scope: scope,
element: new_element
});
})
.push(function (gg) {
rtc_gadget = gg;
// https://github.com/diafygi/webrtc-ips
var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/,
ip_list = [],
ip_dict = {},
ip_addr,
line_list = JSON.parse(json.data).sdp.split('\n'),
i;
for (i = 0; i < line_list.length; i += 1) {
if (line_list[i].indexOf('a=candidate:') === 0) {
ip_addr = ip_regex.exec(line_list[i])[1];
if (!ip_addr.match(/^[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7}$/)) {
// Hide ipv6
if (!ip_dict[ip_addr]) {
ip_list.push(ip_addr);
ip_dict[ip_addr] = true;
}
}
.declareMethod("onOffer", function (options, offer) {
var gadget = this;
gadget.state_parameter_dict.counter += 1;
return new RSVP.Queue()
.push(function(response) {
gadget.connecting = true;
// https://github.com/diafygi/webrtc-ips
var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/,
ip_list = [],
ip_dict = {},
ip_addr,
line_list = JSON.parse(offer.data).sdp.split('\n'),
i;
for (i = 0; i < line_list.length; i += 1) {
if (line_list[i].indexOf('a=candidate:') === 0) {
ip_addr = ip_regex.exec(line_list[i])[1];
if (!ip_addr.match(/^[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7}$/)) {
// Hide ipv6
if (!ip_dict[ip_addr]) {
ip_list.push(ip_addr);
ip_dict[ip_addr] = true;
}
}
gadget.state_parameter_dict.scope_ip[scope] = ip_list;
return rtc_gadget.createAnswer(json.from, json.data);
})
.push(function (local_connection) {
return socket_gadget.send(JSON.stringify({to: json.from, action: "answer", data: local_connection}));
})
.push(function () {
return RSVP.any([
RSVP.Queue()
.push(function () {
return RSVP.delay(10000);
})
.push(function () {
console.info('-- webrtc client disappears...');
return dropSubGadget(gadget, scope);
}),
rtc_gadget.waitForConnection()
]);
})
.push(function () {
gadget.state_parameter_dict.connecting = false;
return updateInfo(gadget);
});
}
}
}
gadget.state_parameter_dict.scope_ip["webrtc"+gadget.state_parameter_dict.counter] = ip_list
options.to = offer.from;
return;
})
})
.declareMethod('postConnection', function(options, offer) {
var gadget = this;
.declareService(function () {
var sgadget,
gadget = this;
return this.getDeclaredGadget('gadget_websocket.html')
.push(function (socket_gadget) {
sgadget = socket_gadget;
return socket_gadget.createSocket("ws://127.0.0.1:9999/");
})
.push(function () {
// Wait for the gadget to be dropped from the page
// and close the socket/rtc connections
return RSVP.defer().promise;
})
.push(undefined, function (error) {
return new RSVP.Queue()
.push(function () {
gadget.state_parameter_dict.connecting = false;
return updateInfo(gadget);
});
})
.declareMethod('initiate', function (roomid) {
var gadget = this,
options = {
peerid: "master",
roomid: "/"+roomid+"/",
type: "websocket",
config: {'url': "ws://127.0.0.1:9999/"},
listner: true,
preConnection: gadget.onOffer.bind(gadget),
postConnection: gadget.postConnection.bind(gadget),
};
return gadget.getDeclaredGadget("gadget_webrtc_jio_bridge.html")
.push(function (rtc_gadget) {
options.rtc_gadget = rtc_gadget;
return rtc_gadget.connect(options);
});
/*.push(undefined, function (error) {
if (sgadget === undefined) {
return;
}
......@@ -228,7 +217,7 @@
.push(function () {
throw error;
});
});
}); */
});
}(window, rJS, document, RSVP, console, DOMException));
\ No newline at end of file
......@@ -228,7 +228,7 @@
</item>
<item>
<key> <string>actor</string> </key>
<value> <string>romain</string> </value>
<value> <string>zope</string> </value>
</item>
<item>
<key> <string>comment</string> </key>
......@@ -242,7 +242,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>945.33680.49983.21452</string> </value>
<value> <string>954.5724.21109.37529</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -260,8 +260,8 @@
</tuple>
<state>
<tuple>
<float>1441358513.14</float>
<string>GMT</string>
<float>1474552150.73</float>
<string>UTC</string>
</tuple>
</state>
</object>
......
......@@ -271,7 +271,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>953.35402.46528.58316</string> </value>
<value> <string>953.57025.36062.60552</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -289,7 +289,7 @@
</tuple>
<state>
<tuple>
<float>1472400667.43</float>
<float>1473698013.89</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -88,7 +88,7 @@
})
.push(function(g){
if(gadget.props.options['config']) {
return g.slaveInitiate(room, gadget, gadget.props.options['config']);
return g.slaveInitiate(room, gadget, gadget.props.options['type'], gadget.props.options['config']);
} else {
return g.slaveInitiate(room, gadget)
.push(null, function(error){
......
......@@ -267,7 +267,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>953.38233.8749.16844</string> </value>
<value> <string>954.5557.49354.23227</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -285,7 +285,7 @@
</tuple>
<state>
<tuple>
<float>1472570633.51</float>
<float>1474542105.73</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -17,8 +17,8 @@
data-gadget-scope="jio_gadget"
data-gadget-sandbox="public"></div>
<div class="gadget_websocket"></div>
<div class="gadget_webrtc_datachannel"></div>
<div data-gadget-url="gadget_webrtc_jio_bridge.html"
data-gadget-scope="gadget_webrtc_jio_bridge.html"
data-gadget-sandbox="public"></div>
</body>
</html>
\ No newline at end of file
......@@ -218,7 +218,7 @@
</item>
<item>
<key> <string>actor</string> </key>
<value> <string>romain</string> </value>
<value> <string>zope</string> </value>
</item>
<item>
<key> <string>comment</string> </key>
......@@ -232,7 +232,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>945.32246.38846.38144</string> </value>
<value> <string>954.2812.41384.46984</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -250,8 +250,8 @@
</tuple>
<state>
<tuple>
<float>1441283008.98</float>
<string>GMT</string>
<float>1474377398.17</float>
<string>UTC</string>
</tuple>
</state>
</object>
......
......@@ -4,7 +4,7 @@
"use strict";
var timeout = 60000,
websocket_timeout = 5000;
websocket_timeout = 500000;
function S4() {
return ('0000' + Math.floor(
......@@ -24,7 +24,7 @@
if (!gadget.state_parameter_dict.jio_created) {
return gadget.redirect({page: 'setting'});
}
return gadget.getDeclaredGadget('gadget_webrtc_datachannel.html')
return gadget.getDeclaredGadget("gadget_webrtc_jio_bridge.html")
.push(function (rtc_gadget) {
gadget.state_parameter_dict.message_count += 1;
gadget.state_parameter_dict.message_dict[gadget.state_parameter_dict.message_count] = RSVP.defer();
......@@ -34,7 +34,7 @@
type: "jio_query",
method_name: method_name,
argument_list: Array.prototype.slice.call(argument_list)
})),
}), 'webrtc0'),
RSVP.any([
RSVP.timeout(timeout),
gadget.state_parameter_dict.message_dict[gadget.state_parameter_dict.message_count].promise
......@@ -44,7 +44,6 @@
.push(function (result_list) {
return result_list[1];
});
}
function declareSubGadget(gadget, url) {
......@@ -105,58 +104,41 @@
.declareMethod('createJio', function (options) {
var context = this,
socket_gadget,
rtc_gadget;
rtc_gadget,
rtc_options = {
type: "websocket",
roomid: "/ok/",
config: {url : options.socket_url},
initiator: true,
to: 'master'
};
//if ((options === undefined) || (options.socket_url === undefined)) {
// return context.redirect({page: 'setting'});
//}
if ((options === undefined) || (options.socket_url === undefined)) {
return context.redirect({page: 'setting'});
}
context.state_parameter_dict.jio_created = true;
return declareSubGadget(context, 'gadget_websocket.html')
.push(function (gadget) {
socket_gadget = gadget;
context.state_parameter_dict.uuid = UUID();
context.state_parameter_dict.answer_defer = RSVP.defer();
context.state_parameter_dict.message_count = 0;
context.state_parameter_dict.message_dict = {};
// Send offer and expect answer in less than XXXms (arbitrary value...)
return RSVP.any([
RSVP.Queue()
.push(function () {
return RSVP.timeout(websocket_timeout);
})
.push(undefined, function () {
return context.redirect({page: 'setting'});
// throw new Error("No remote WebRTC connection available");
}),
declareSubGadget(context, 'gadget_websocket.html')
.push(function (gadget) {
socket_gadget = gadget;
// XXX Drop hardcoded URL
return socket_gadget.createSocket(options.socket_url);
})
.push(function () {
return declareSubGadget(context, 'gadget_webrtc_datachannel.html');
})
.push(function (gadget) {
rtc_gadget = gadget;
return rtc_gadget.createOffer(context.state_parameter_dict.uuid);
})
.push(function (description) {
return RSVP.all([
socket_gadget.send(JSON.stringify({from: context.state_parameter_dict.uuid, action: "offer", data: description})),
context.state_parameter_dict.answer_defer.promise
]);
})
.push(function (response_list) {
return rtc_gadget.registerAnswer(response_list[1]);
})
.push(function () {
return socket_gadget.close();
})
]);
});
context.state_parameter_dict.uuid = UUID();
context.state_parameter_dict.answer_defer = RSVP.defer();
context.state_parameter_dict.message_count = 0;
context.state_parameter_dict.message_dict = {};
// Send offer and expect answer in less than XXXms (arbitrary value...)
return RSVP.any([
RSVP.Queue()
.push(function () {
return RSVP.timeout(websocket_timeout);
})
.push(undefined, function () {
//return context.redirect({page: 'setting'});
// throw new Error("No remote WebRTC connection available");
}),
context.getDeclaredGadget("gadget_webrtc_jio_bridge.html")
.push(function (rtc_gadget) {
rtc_options.peerid = context.state_parameter_dict.uuid;
return rtc_gadget.connect(rtc_options);
})
]);
})
.declareMethod('allDocs', function () {
return wrapJioAccess(this, 'allDocs', arguments);
......
......@@ -216,7 +216,7 @@
</item>
<item>
<key> <string>actor</string> </key>
<value> <string>romain</string> </value>
<value> <string>zope</string> </value>
</item>
<item>
<key> <string>comment</string> </key>
......@@ -230,7 +230,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>945.39449.60302.38024</string> </value>
<value> <string>954.5720.33596.8260</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -248,8 +248,8 @@
</tuple>
<state>
<tuple>
<float>1441715721.23</float>
<string>GMT</string>
<float>1474551874.29</float>
<string>UTC</string>
</tuple>
</state>
</object>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment