Module: Redom

Defined in:
lib/redom.rb,
lib/redom/task.rb,
lib/redom/proxy.rb,
lib/redom/utils.rb,
lib/redom/parser.rb,
lib/redom/worker.rb,
lib/redom/runtime.rb,
lib/redom/version.rb,
lib/redom/connection.rb,
lib/redom/dispatcher.rb,
lib/redom/thread_pool.rb

Defined Under Namespace

Modules: Connection, Utils Classes: Dispatcher, Parser, Proxy, Task, ThreadPool, Worker

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :log => STDOUT,
  :log_level => 'error',
  :worker => 5,
  :buff_size => 200
}
REQ_HANDSHAKE =
0
REQ_METHOD_INVOCATION =
1
REQ_PROXY_RESULT =
2
IDX_REQUEST_TYPE =
0
IDX_CONNECTION_CLASS =

REQ_HANDSHAKE

1
IDX_CONNECTION_ID =

REQ_METHOD_INVOCATION

1
IDX_METHOD_NAME =
2
IDX_ARGUMENTS =
3
IDX_TASK_ID =

REQ_PROXY_RESULT

1
IDX_PROXY_RESULT =
2
TYPE_UNDEFINED =
0
TYPE_PROXY =
1
TYPE_ARRAY =
2
TYPE_ERROR =
3
TYPE_METHOD =
4
T_INFO_METHOD_NAME =
0
T_INFO_ARGUMENTS =
1
T_INFO_BLOCK =
2
P_INFO_OID =
0
P_INFO_RCVR =
1
P_INFO_NAME =
2
P_INFO_ARGS =
3
VERSION =
'0.1.2'

Class Method Summary collapse

Class Method Details

.on_close(ws) ⇒ Object



87
88
89
# File 'lib/redom.rb', line 87

def on_close(ws)
  @@dispatcher.on_close(ws)
end

.on_error(ws, err) ⇒ Object



91
92
93
# File 'lib/redom.rb', line 91

def on_error(ws, err)
  @@dispatcher.on_error(ws, err)
end

.on_message(ws, msg) ⇒ Object



83
84
85
# File 'lib/redom.rb', line 83

def on_message(ws, msg)
  @@dispatcher.on_message(ws, msg)
end

.on_open(ws) ⇒ Object



79
80
81
# File 'lib/redom.rb', line 79

def on_open(ws)
  @@dispatcher.on_open(ws)
end

.runtimeObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/redom/runtime.rb', line 3

def runtime
<<EOS
(function() {
  var
  TYPE_UNDEFINED = 0,
  TYPE_PROXY     = 1,
  TYPE_ARRAY     = 2,
  TYPE_ERROR     = 3,
  TYPE_METHOD    = 4,

  REQ_HANDSHAKE         = 0,
  REQ_METHOD_INVOCATION = 1,
  REQ_PROXY_RESULT      = 2,

  P_INFO_OID  = 0,
  P_INFO_RCVR = 1,
  P_INFO_NAME = 2,
  P_INFO_ARGS = 3;

  var connections = {};

  if (window.Opal) {
Opal.$method = function(cid, name) {
  if (Opal.top['$' + name]) {
    return Opal.top['$' + name];
  } else {
    return connections[cid](name);
  }
}

Object.prototype.$djsCall = function(name) {
  if (name == '') {
    return (function(obj) {
      return function(key) {
        return obj[key];
      };
    })(this);
  }

  if (Opal.top[name]) {
    if (typeof Opal.top[name] == 'function') {
      return function() {
        return Opal.top[name].apply(Opal.top, arguments);
      };
    } else {
      return function() {
        return Opal.top[name];
      };
    }
  }
  if (name[0] == "$" && this[name] == null) {
    name = name.substring(1);
  };
  
  if (this[name] != null) {
    if (typeof this[name] == 'function') {
      return (function(obj) {
        return function() {
          return obj[name].apply(obj, arguments);
        };
      })(this);
    } else {
      return (function(obj) {
        return function() {
          return obj[name];
        };
      })(this);
    }
  } else if (window[name]) {
    if (typeof window[name] == 'function') {
      return function() {
        return window[name].apply(window, arguments);
      };
    } else {
      return function() {
        return window[name];
      };
    }
  }
  
  return function() {
    return null;
  };
};
Object.prototype.$djsAssign = function(name) {
  return (function(obj) {
    return function(value) {
      return obj[name] = value;
    };
  })(this);
};
Opal.top.$window = window;
Opal.top.$document = window.document;
Opal.top.$navigator = window.navigator;
Opal.top.$location = window.location;
Opal.top.$history = window.history;
  }

  var Redom = function(server) {
var ws = new WebSocket(server),
  refs = {
    seq: 0,
    hash: {},
    put: function() {
      var key;
      if (arguments.length == 2) {
        key = arguments[0];
        this.hash[key] = arguments[1];
      } else if (arguments.length == 1) {
        key = "R" + (++this.seq);
        this.hash[key] = arguments[0];
      } else {
        console.log("ERROR: wrong number of arguments!");
      }
      return key;
    },
    get: function(key) {
      return this.hash[key];
    }
  },
  tasks = [];

// Initialize refs
refs.put("window", window);
refs.put("document", document);
refs.put("history", history);
refs.put("navigator", navigator);
refs.put("location", location);

// Initialize WebSocket event handlers
function open(connectionClassName) {
  ws.onopen = function() {
    ws.send(serialize([REQ_HANDSHAKE, connectionClassName]));
    console.log("WebSocket opened.");
  };
  ws.onclose = function() {
    console.log("WebSocket closed.");
  };
  ws.onerror = function(error) {
    console.log(error);
  };
  ws.onmessage = function(event) {
    var data = unserialize(event.data);
    var tid = data[0];
    var proxies = data[1];
    var queue = tasks[tid];
    if (!queue) {
      queue = [];
      tasks[tid] = queue;
    }
    for (var i = 0; i < proxies.length; i++) {
      queue.push(proxies[i]);
    }
    try {
      process(tid);
    } catch (e) {
      console.log(e);
    }
  };
};

// Close this WebSocket connection
function close() {
  ws.close();
};

// Serialize JavaScript object into message
function serialize(data) {
  return JSON.stringify(data);
};
// Unserialize message into JavaScript object
function unserialize(msg) {
  return JSON.parse(msg);
};

// Restore arguments
function restore(args) {
  if (args instanceof Array) {
    switch(args[0]) {
    case TYPE_ARRAY:
      for (var i = 0; i < args[1].length; i++) {
        args[1][i] = restore(args[1][i]);
      }
      return args[1];
      break;
    case TYPE_PROXY:
      return refs.get(args[1]);
      break;
    case TYPE_METHOD:
      return (function(cid, name) {
        if (window[name] && typeof(window[name]) == 'function') {
          return function() {
            window[name];
          };
        } else {
          return function() {
            var type, args = [];
            for (var i = 0; i < arguments.length; i++) {
              switch(type = typeOf(arguments[i])) {
                case null:
                  args.push(arguments[i]);
                  break;
                case TYPE_PROXY:
                case TYPE_ARRAY:
                  args.push([type, refs.put(arguments[i])]);
                  break;
              }
            }
            ws.send(serialize([REQ_METHOD_INVOCATION, cid, name, args]));
          }
        }
      })(args[1], args[2]);
      break;
    }
  } else {
    return args;
  }
};

// Evaluation proxies from server
function process(tid) {
  var proxy, oid, name, args, result, type,
    rsps = [],
    queue = tasks[tid];
  while (proxy = queue.shift()) {
    oid = proxy[P_INFO_OID];
    rcvr = refs.get(proxy[P_INFO_RCVR]);
    name = proxy[P_INFO_NAME];
    args = restore(proxy[P_INFO_ARGS]);

    if (rcvr) {
      result = execute(rcvr, name, args);
      if (typeof result == "undefined") {
        rsps.push([oid, [TYPE_UNDEFINED]]);
        ws.send(serialize([REQ_PROXY_RESULT, tid, rsps]));
        return;
      } else {
        refs.put(oid, result);
        switch (type = typeOf(result)) {
          case null:
            rsps.push([oid, result]);
            break;
          case TYPE_PROXY:
          case TYPE_ARRAY:
            rsps.push([oid, [type]]);
            break;
        };
      }
    } else {
      if (name) {
        rsps.push([oid, TYPE_ERROR, "no such object. ID='" + proxy[P_INFO_RCVR] + "'."]);
        ws.send(serialize([REQ_PROXY_RESULT, tid, rsps]));
        return;
      } else {
        connections[args] = (function(ws, cid) {
          return function(name) {
            return function(e) {
              ws.send(serialize([REQ_METHOD_INVOCATION, cid, name, [[TYPE_PROXY, refs.put(e)]]]));
            };
          };
        })(ws, args);
      }
    }
  }
  ws.send(serialize([REQ_PROXY_RESULT, tid, rsps]));
};

// Type of object
function typeOf(obj) {
  switch(typeof obj) {
    case "string":
    case "number":
    case "boolean":
    case "undefined":
      return null;
    case "object":
      if (obj == null) {
        return null;
      } else if (obj instanceof Array) {
        return TYPE_ARRAY;
      } else {
        return TYPE_PROXY;
      }
    case "function":
      return TYPE_METHOD;
  }
  return TYPE_UNDEFINED;
};

// Method invocation
function execute(rcvr, name, args) {
  var result;
  if (name.match(/^(.+)=$/)) {
    name = RegExp.$1;
    if (name == "[]") {
      result = rcvr[args[0]] = args[1];
    } else {
      result = rcvr[name] = args[0];
    }
  } else {
    if (name == "[]") {
      result = rcvr[args[0]];
    } else {
      result = rcvr[name];
      if (typeof result == "function") {
        result = result.apply(rcvr, args);
        result = typeof result == "undefined" ? null : result;
      }
    }
  }
  return result;
};

return {
  getServer: function() {
    return server;
  },

  open: function(connectionClassName) {
    open(connectionClassName);
  },

  close: function() {
    close();
  }
};
  };

  window.Redom = Redom;
})();
EOS
end

.start(opts = {}) ⇒ Object

Start Redom dispatcher

Parameters:

  • opts (Hash) (defaults to: {})

    Options



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/redom.rb', line 54

def start(opts = {})
  opts = DEFAULT_OPTIONS.merge(opts)
  logger = Logger.new(opts[:log])
  logger.level = case opts[:log_level].downcase
  when 'fatal'
    Logger::FATAL
  when 'error'
    Logger::ERROR
  when 'warn'
    Logger::WARN
  when 'info'
    Logger::INFO
  when 'debug'
    Logger::DEBUG
  else
    Logger::FATAL
  end
  Utils.logger = logger
  Utils.dispatcher = @@dispatcher = Dispatcher.new(opts)
end

.stopObject



75
76
77
# File 'lib/redom.rb', line 75

def stop
  @@dispatcher.stop
end