1 | var mensaje = require('js2xmlparser');
|
2 | var xml2js = require('xml2js').parseString;
|
3 | var net = require('net');
|
4 | var global = { conn: new net.Socket(),
|
5 | last_id: '',
|
6 | last_cmd: '',
|
7 | curr_state: 'wait',
|
8 | buffer: ''
|
9 | };
|
10 | var 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 |
|
18 | exports.getLastID = function() {
|
19 | return global.last_id;
|
20 | };
|
21 |
|
22 | exports.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 |
|
29 | exports.init = function(callback, options) {
|
30 | if (options) {
|
31 |
|
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 |
|
47 | exports.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 |
|
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 |
|
73 | events[scmd](result[cmd]);
|
74 | } else if ('*' in events) {
|
75 |
|
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 |
|
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 |
|
96 | exports.write = function(cmd, obj) {
|
97 | var tmp = { cmd: 'collaboration_'+cmd, data:obj };
|
98 | var msg = mensaje(tmp.cmd, obj);
|
99 |
|
100 | msg = msg.split('\n').join('');
|
101 | console.log('\nconn:send:enviando:',msg);
|
102 | global.conn.write(msg+'\r\n','utf-8', function() {
|
103 |
|
104 | });
|
105 | };
|
106 |
|
107 | exports.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 |
|
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 |
|
137 | exports.destroy = function() {
|
138 | global.conn.destroy();
|
139 | }; |
\ | No newline at end of file |