Class: DatalackeyIO

Inherits:
Object
  • Object
show all
Defined in:
lib/datalackeylib.rb

Constant Summary collapse

@@internal_notification_map =
{
  error: {
    user_id: [ 'error', 'identifier', '?' ],
    format: [ 'error', 'format' ]
  },
  stored: [ 'data', 'stored', '?', '?' ],
  deleted: [ 'data', 'deleted', '?', '?' ],
  data_error: [ 'data', 'error', '?', '?' ],
  started: [ 'process', 'started', '?', '?' ],
  ended: [ 'process', 'ended', '?', '?' ]
}
@@internal_generic_map =
{
  error: {
    syntax: [
      [ 'error', 'missing', '*' ],
      [ 'error', 'not-string', '*' ],
      [ 'error', 'not-string-null', '*' ],
      [ 'error', 'pairless', '*' ],
      [ 'error', 'unexpected', '*' ],
      [ 'error', 'unknown', '*' ],
      [ 'error', 'command', 'missing', '?' ],
      [ 'error', 'command', 'not-string', '?' ],
      [ 'error', 'command', 'unknown', '?' ]
    ]
  },
  done: [ 'done', '' ],
  child: [ 'run', 'running', '?' ]
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(to_datalackey, from_datalackey, notification_callable = nil, to_datalackey_echo_callable = nil, from_datalackey_echo_callable = nil) ⇒ DatalackeyIO

Returns a new instance of DatalackeyIO.



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/datalackeylib.rb', line 314

def initialize(to_datalackey, from_datalackey, notification_callable = nil,
    to_datalackey_echo_callable = nil, from_datalackey_echo_callable = nil)
  @to_datalackey_mutex = Mutex.new
  @to_datalackey = to_datalackey
  @to_datalackey_echo = to_datalackey_echo_callable
  @from_datalackey = from_datalackey
  @identifier = 0
  @tracked_mutex = Mutex.new
  # Handling of notifications.
  @notify_tracker = PatternAction.new([ @@internal_notification_map ])
  @notify_tracker.set_identifier(nil)
  @internal = PatternAction.new([ @@internal_generic_map ])
  @tracked = Hash.new(nil)
  @waiting = nil
  @return_mutex = Mutex.new
  @return_condition = ConditionVariable.new
  @dataprocess_mutex = Mutex.new
  @data = Hash.new(0)
  @process = { }
  @children = { }
  @version = { }
  @read_datalackey = Thread.new do
    accum = []
    loop do
      begin
        raw = @from_datalackey.readpartial(32768)
      rescue IOError
        break
      rescue EOFError
        break
      end
      loc = raw.index("\n")
      until loc.nil?
        accum.push(raw[0, loc]) if loc.positive? # Newline at start ends line.
        raw = raw[loc + 1, raw.size - loc - 1]
        loc = raw.index("\n")
        joined = accum.join
        accum.clear
        next if joined.empty?
        from_datalackey_echo_callable.call(joined) unless from_datalackey_echo_callable.nil?
        msg = JSON.parse joined
        # See if we are interested in it.
        if msg.first.nil?
          act, vars = @notify_tracker.best_match(msg)
          next if act.nil?
          # We know there is only one action that matches.
          act = act.first
          actionable = nil
          name = vars.first
          id = vars.last
          # Messages from different threads may arrive out of order so
          # new data/process may be in book-keeping when previous should
          # be removed. With data these imply over-writing immediately,
          # with processes re-use of identifier and running back to back.
          case act.first
          when :stored
            @dataprocess_mutex.synchronize do
              if @data[name] < id
                @data[name] = id
                actionable = act
              end
            end
          when :deleted
            @dataprocess_mutex.synchronize do
              if @data.key?(name) && @data[name] <= id
                @data.delete name
                actionable = act
              end
            end
          when :data_error
            @dataprocess_mutex.synchronize do
              @data.delete(name) if @data[name] == id
            end
            actionable = act
          when :started
            @dataprocess_mutex.synchronize { @process[name] = id }
            actionable = act
          when :ended
            @dataprocess_mutex.synchronize do
              if @process[name] == id
                @process.delete(name)
                @children.delete(name)
              end
            end
            actionable = act
          when :error
            case act[1]
            when :format
              @to_datalackey_mutex.synchronize { @to_datalackey.putc 0 }
            when :user_id
              unless @waiting.nil?
                # Does the waited command have invalid id?
                begin
                  int = Integer(@waiting)
                  fract = @waiting - int
                  raise ArgumentError, '' unless fract.zero?
                rescue ArgumentError, TypeError
                  unless @waiting.is_a? String
                    @tracked_mutex.synchronize do
                      trackers = @tracked[@waiting]
                      trackers.first.message = msg
                      trackers.first.exit = [ act ]
                      @tracked.delete(@waiting)
                      @waiting = nil
                    end
                    @return_mutex.synchronize { @return_condition.signal }
                  end
                end
              end
            end
            actionable = act
          end
          next if notification_callable.nil? || actionable.nil?
          notification_callable.call(actionable, msg, vars)
          next
        end
        # Not a notification.
        trackers = @tracked_mutex.synchronize { @tracked[msg[0]] }
        next if trackers.nil?
        finish = false
        last = nil
        # Deal with user-provided PatternAction (or NoPatternNoAction).
        tracker = trackers.first
        act, vars = tracker.best_match(msg)
        unless act.nil?
          act.each do |item|
            tracker.generators.each do |p|
              break if p.call(item, msg, vars)
            end
            next unless msg.first == @waiting
            case item.first
            when :return, 'return'
              finish = true
              last = act if last.nil?
            when :error, 'error'
              finish = true
              last = act
            end
          end
        end
        # Check internal PatternAction.
        internal = trackers.last
        act, vars = internal.best_match(msg)
        unless act.nil?
          act = act.first # We know patterns are all unique in mapping.
          if act.first == :child
            @dataprocess_mutex.synchronize { @children[msg[0]] = vars.first }
          elsif msg.first == @waiting
            finish = true
            if act.first == :done
              @tracked_mutex.synchronize { @tracked.delete(msg[0]) }
            elsif act.first == :error
              last = [ act ]
            end
          end
        end
        if finish
          tracker.message = msg
          tracker.exit = last
          @tracked_mutex.synchronize { @waiting = nil }
          @return_mutex.synchronize { @return_condition.signal }
        end
      end
      accum.push(raw) unless raw.empty?
    end
    @from_datalackey.close
    @return_mutex.synchronize { @return_condition.signal }
  end
  # Outside thread block.
  send(PatternAction.new([{ version: [ 'version', '', '?' ] }], [
    proc do |action, message, vars|
      if action.first == :version
        @syntax = vars.first['commands']
        @version = { }
        vars.first.each_pair do |key, value|
          @version[key] = value if value.is_a? Integer
        end
        true
      else false
      end
    end
  ]), ['version'])
end

Instance Attribute Details

#syntaxObject (readonly)

Returns the value of attribute syntax.



312
313
314
# File 'lib/datalackeylib.rb', line 312

def syntax
  @syntax
end

#versionObject (readonly)

Returns the value of attribute version.



312
313
314
# File 'lib/datalackeylib.rb', line 312

def version
  @version
end

Class Method Details

.internal_generic_mapObject



308
309
310
# File 'lib/datalackeylib.rb', line 308

def self.internal_generic_map
  Marshal.load(Marshal.dump(@@internal_generic_map))
end

.internal_notification_mapObject



304
305
306
# File 'lib/datalackeylib.rb', line 304

def self.internal_notification_map
  Marshal.load(Marshal.dump(@@internal_notification_map))
end

Instance Method Details

#closeObject



514
515
516
# File 'lib/datalackeylib.rb', line 514

def close
  @to_datalackey_mutex.synchronize { @to_datalackey.close }
end

#closed?Boolean

Returns:

  • (Boolean)


510
511
512
# File 'lib/datalackeylib.rb', line 510

def closed?
  @from_datalackey.closed?
end

#dataObject



498
499
500
# File 'lib/datalackeylib.rb', line 498

def data
  @dataprocess_mutex.synchronize { return @data.clone }
end

#dump(json_as_string) ⇒ Object



553
554
555
556
557
558
559
560
561
# File 'lib/datalackeylib.rb', line 553

def dump(json_as_string)
  @to_datalackey_mutex.synchronize do
    @to_datalackey.write json_as_string
    @to_datalackey.flush
    @to_datalackey_echo.call(json_as_string) unless @to_datalackey_echo.nil?
  rescue Errno::EPIPE
    # Should do something in this case. Child process died?
  end
end

#finishObject



518
519
520
# File 'lib/datalackeylib.rb', line 518

def finish
  @read_datalackey.join
end

#launchedObject



506
507
508
# File 'lib/datalackeylib.rb', line 506

def launched
  @dataprocess_mutex.synchronize { return @children.clone }
end

#processObject



502
503
504
# File 'lib/datalackeylib.rb', line 502

def process
  @dataprocess_mutex.synchronize { return @process.clone }
end

#send(pattern_action, command, user_id = false) ⇒ Object

Pass nil pattern_action if you are not interested in doing anything.



523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/datalackeylib.rb', line 523

def send(pattern_action, command, user_id = false)
  return nil if @to_datalackey_mutex.synchronize { @to_datalackey.closed? }
  if user_id
    id = command[0]
  else
    id = @identifier
    @identifier += 1
    command.prepend id
  end
  tracker = pattern_action.nil? ? NoPatternNoAction.new : pattern_action.clone
  tracker.set_identifier(id)
  tracker.command = JSON.generate(command)
  internal = @internal.clone
  internal.set_identifier(id)
  @tracked_mutex.synchronize do
    @tracked[id] = [ tracker, internal ] unless id.nil?
    @waiting = id
  end
  dump(tracker.command)
  return tracker if id.nil? # There will be no responses.
  @return_mutex.synchronize { @return_condition.wait(@return_mutex) }
  tracker.status = true
  unless tracker.exit.nil?
    tracker.exit.each do |item|
      tracker.status = false if item.first == :error || item.first == 'error'
    end
  end
  tracker
end

#verify(command) ⇒ Object



563
564
565
# File 'lib/datalackeylib.rb', line 563

def verify(command)
  @syntax.nil? ? nil : true
end