window mode

This commit is contained in:
Rodolphe Breard 2012-11-07 10:53:48 +01:00
parent 6864c4389d
commit d2570a8e41
7 changed files with 60 additions and 68 deletions

View file

@ -20,26 +20,17 @@ var NsClient = function() {
this.allowed_statuses = ["actif", "away", "idle", "lock"];
this.is_connected = false;
this.socket = null;
this.verbose = false;
};
NsClient.prototype.connect = function() {
var cnt = function(elem) {
return function(infos) {
if (typeof infos.login !== "undefined" && typeof infos.pwd_socks !== "undefined") {
if (elem.verbose)
console.log('creating socket...');
chrome.socket.create('tcp', {}, function(sock_inf) {
if (elem.verbose)
console.log('socket created, id: ' + sock_inf.socketId);
elem.socket = sock_inf.socketId;
chrome.socket.connect(elem.socket, "ns-server.epita.fr", 4242, function(res) {
if (elem.verbose)
console.log('connected to server');
chrome.socket.read(elem.socket, null, function(rd_inf) {
if (rd_inf.resultCode > 0) {
if (elem.verbose)
console.log(ab2str(rd_inf.data));
var data = ab2str(rd_inf.data).split(' '),
auth = "ext_user_log ";
auth += infos.login + " ";
@ -92,13 +83,8 @@ NsClient.prototype.daemonize = function() {
if (rd_inf.resultCode > 0) {
var data = ab2str(rd_inf.data);
if (elem.verbose)
console.log("recv: " + data);
if (data.substr(0, 5) === "ping ") {
chrome.socket.write(elem.socket, rd_inf.data, function(w_inf) {
if (elem.verbose)
console.log("sent: " + data);
chrome.socket.read(elem.socket, null, this);
});
}
@ -114,20 +100,20 @@ NsClient.prototype.daemonize = function() {
};
NsClient.prototype.updateStatus = function() {
var status_txt = document.getElementById("status_txt");
console.log(status_txt);
if (this.is_connected) {
if (status_txt !== null)
status_txt.innerHTML = this.state;
var status_msg = "user_cmd state ";
status_msg += this.state + ":";
status_msg += Math.round(new Date().getTime() / 1000) + "\n";
chrome.socket.write(this.socket, str2ab(status_msg), function(w_inf) {});
} else {
console.warn("not connected");
}
};
NsClient.prototype.recv = function() {
if (this.is_connected) {
} else {
console.warn("not connected");
if (status_txt !== null)
status_txt.innerHTML = 'disconnected';
}
};
@ -144,14 +130,12 @@ NsClient.prototype.changeStatus = function(new_status) {
};
NsClient.prototype.init = function() {
var upd = function(elem) {
var status_update = function(elem) {
return function() {
if (elem.is_connected) {
elem.updateStatus();
}
elem.updateStatus();
};
},
con = function(elem) {
connect = function(elem) {
return function() {
if (!elem.is_connected) {
elem.connect();
@ -159,7 +143,8 @@ NsClient.prototype.init = function() {
};
};
con(this).apply();
setInterval(con(this), 10000);
setInterval(upd(this), 600000);
status_update(this).apply();
connect(this).apply();
setInterval(connect(this), 10000);
setInterval(status_update(this), 600000);
};