Class: OML4R::Channel

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

Overview

Measurement Channel

Constant Summary collapse

@@channels =
{}
@@default_domain =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#urlObject

Returns the value of attribute url.



617
618
619
# File 'lib/oml4r.rb', line 617

def url
  @url
end

Class Method Details

.[](name = :default, domain = :default) ⇒ Object



577
578
579
580
581
582
583
584
585
586
587
588
589
# File 'lib/oml4r.rb', line 577

def self.[](name = :default, domain = :default)
  key = "#{name}:#{domain}"
  unless (@@channels.key?(key))
    # If domain != :default and we have one for :default, create a new one
    if (domain != :default)
      if (dc = @@channels["#{name}:default"])
        return self._create(key, domain, dc.url)
      end
    end
    raise OML4RException.new "OML4R: Unknown channel '#{name}'"
  end
  @@channels[key]
end

._connect(fquri) ⇒ IO

Parse the given fully-qualified collection URI, and return a suitably connected objet

Supported URIs are

tcp:host:port
file:/P/A/T/H

Parameters:

  • fquri (String)

    a fully qualified collection URI

Returns:

  • (IO)

    an object suitably connected to the required URL

Raises:



563
564
565
566
567
568
569
570
571
572
573
574
575
# File 'lib/oml4r.rb', line 563

def self._connect(fquri)
  scheme, host, port = fquri.split(':')
  out = case scheme
        when 'tcp'
          out = TCPSocket.new(host, port)
        when 'file'
          # host is really a filename here
          out = (host == '-' ? $stdout : File.open(host, "w+"))
        else
          raise OML4RException.new "OML4R: Unknown scheme '#{scheme}"
        end
  out
end

._create(key, domain, url) ⇒ Object



548
549
550
# File 'lib/oml4r.rb', line 548

def self._create(key, domain, url)
  @@channels[key] = self.new(url, domain)
end

.close_allObject



611
612
613
614
615
# File 'lib/oml4r.rb', line 611

def self.close_all()
  @@channels.values.each { |c| c.close }
  @@channels = {}
  MPBase.__unfreeze__()
end

.create(name, url, domain = :default) ⇒ Object



537
538
539
540
541
542
543
544
545
546
# File 'lib/oml4r.rb', line 537

def self.create(name, url, domain = :default)
  key = "#{name}:#{domain}"
  if channel = @@channels[key]
    if url != channel.url
      raise OML4RException.new "OML4R: Channel '#{name}' already defined with different url"
    end
    return channel
  end
  return self._create(key, domain, url)
end

.init_all(domain, nodeID, appName, startTime, protocol) ⇒ Object



591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
# File 'lib/oml4r.rb', line 591

def self.init_all(domain, nodeID, appName, startTime, protocol)
  @@default_domain = domain
  @@nodeID = nodeID
  @@appName = appName
  @@startTime = startTime
  @@protocol = protocol

  MPBase.__freeze__(appName, startTime)

  # send channel header
  @@channels.values.each { |c| c.init(nodeID, appName, startTime, protocol) }

  # send schema definitions
  MPBase.each_mp do |klass, defs|
    klass.__print_meta__(appName)
  end

  MPBase.__useOML__()
end

Instance Method Details

#build_schema(mp_name, add_prefix, pdefs) ⇒ Object



627
628
629
630
631
632
633
634
635
636
# File 'lib/oml4r.rb', line 627

def build_schema(mp_name, add_prefix, pdefs)
  @index += 1
  line = [@index, (!@@appName.nil? && add_prefix)? "#{@@appName}_#{mp_name}" : mp_name]
  pdefs.each do |d|
    line << "#{d[:name]}:#{d[:type]}"
  end
  msg = line.join(' ')
  @schemas << msg
  [@index, msg]
end

#closeObject



652
653
654
655
# File 'lib/oml4r.rb', line 652

def close()
  @queue.push nil  # indicate end of work
  @runner.join()
end

#init(nodeID, appName, startTime, protocol) ⇒ Object



647
648
649
650
# File 'lib/oml4r.rb', line 647

def init(nodeID, appName, startTime, protocol)
  @nodeID, @appName, @startTime, @protocol = nodeID, appName, startTime, protocol
  @out = _connect(@url)
end

#send(msg) ⇒ Object



638
639
640
# File 'lib/oml4r.rb', line 638

def send(msg)
  @queue.push msg
end

#send_schema_update(msg) ⇒ Object



642
643
644
645
# File 'lib/oml4r.rb', line 642

def send_schema_update(msg)
  @header_sent = true
  @queue.push msg
end