UNPKG

3.75 kBJavaScriptView Raw
1var mensaje = require('js2xmlparser');
2var xml2js = require('xml2js').parseString;
3var net = require('net');
4var global = { conn: new net.Socket(),
5 last_id: '',
6 last_cmd: '',
7 curr_state: 'wait',
8 buffer: ''
9 };
10var defaults = {
11 'receive_lock' : function(data, callback) {
12 global.last_id = data['$'].id;
13 console.log('*** recibimos lock id: ****',global.last_id);
14 if (callback) callback(data);
15 }
16};
17
18exports.getLastID = function() {
19 return global.last_id;
20};
21
22exports.parseXml = function(xml, callback) {
23 xml2js(xml, function(err, result) {
24 var parent_tag = Object.keys(result)[0].toString();
25 callback(parent_tag, result[parent_tag]);
26 });
27};
28
29exports.init = function(callback, options) {
30 if (options) {
31 // definimos valores default, en caso de haber options definidos
32 if ('retry' in options) {
33 if (options.retry==true) {
34 defaults['goodbye'] = function() {
35 console.log('conn:retrying connecting as requested in options.');
36 exports.init();
37 }
38 }
39 }
40 //
41 }
42 global.conn.connect(9000, 'localhost', function() {
43 callback();
44 });
45};
46
47exports.on = function(events) {
48 global.conn.on('data', function(data) {
49 var xmlheader = 'encoding="UTF-8"?>';
50 var msg_continues = false;
51 if (data.toString().indexOf('<cont>')!=-1) {
52 // este pack de datos necesita sumarse al buffer
53 var tt = data.toString().split('<cont>').join('');
54 global.buffer += tt;
55 msg_continues = true;
56 } else {
57 global.buffer = data.toString();
58 }
59 var justcmd = global.buffer;
60 var pos = justcmd.toString().indexOf('encoding="UTF-8"?>')+xmlheader.length;
61 if (pos!=-1) {
62 justcmd = justcmd.slice(pos);
63 }
64 if (msg_continues==false) {
65 try {
66 var recibido = xml2js(justcmd, function(err, result) {
67 var cmd = Object.keys(result)[0].toString();
68 var scmd = cmd.split('collaboration_').join('');
69 if (err) console.log('error en xml2js:',err);
70 global.last_cmd = scmd;
71 if (scmd in events) {
72 //console.log('conn:llamando cmd:'+scmd+' definido en param events');
73 events[scmd](result[cmd]);
74 } else if ('*' in events) {
75 // evento fallback en si mismo detectado
76 if (scmd in defaults) {
77 defaults[scmd](result[cmd],function(x) {
78 events['*'](result[cmd],cmd);
79 });
80 } else {
81 events['*'](result[cmd],cmd);
82 }
83 } else if (scmd in defaults) {
84 defaults[scmd](result[cmd]);
85 //console.log('conn:cmd:'+scmd+' no ha sido definido en param events. se omite.');
86 }
87 });
88 } catch(e) {
89 console.log('error procesando respuesta xml a JS. xml:', justcmd);
90 console.log(e);
91 }
92 }
93 });
94};
95
96exports.write = function(cmd, obj) {
97 var tmp = { cmd: 'collaboration_'+cmd, data:obj };
98 var msg = mensaje(tmp.cmd, obj);
99 //msg = msg.split('&gt;').join('>');
100 msg = msg.split('\n').join('');
101 console.log('\nconn:send:enviando:',msg);
102 global.conn.write(msg+'\r\n','utf-8', function() {
103 //global.conn.end();
104 });
105};
106
107exports.send = {
108 login : function(name) {
109 exports.write('hello', { "@" : { "user_id" : name+"@MacBookdeX.home",
110 "password" : "depurador123",
111 }
112 }
113 );
114 },
115 getlock : function() {
116 exports.write('require_lock',{});
117 },
118 transaction : function(action,undo) {
119 var espera = 50;
120 if (global.last_cmd!='receive_lock' || global.last_id=='') {
121 exports.send.getlock();
122 espera = 1000;
123 }
124 setTimeout(function() {
125 // ejecutamos con desfase segun si hay o no lock.
126 exports.write('transaction',{
127 '@' : {
128 'id' : global.last_id,
129 'do_action' : mensaje('compound_action',action),
130 'undo_action' : mensaje('compound_action',undo)
131 }
132 });
133 }, espera);
134 }
135};
136
137exports.destroy = function() {
138 global.conn.destroy();
139};
\No newline at end of file