Class: GRI::RemoteLDB

Inherits:
Object show all
Defined in:
lib/gri/ldb.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tra_uri, host) ⇒ RemoteLDB

Returns a new instance of RemoteLDB.



96
97
98
99
100
# File 'lib/gri/ldb.rb', line 96

def initialize tra_uri, host
  @tra_uri = tra_uri
  @host = host
  @sock = nil
end

Class Method Details

.get_gritab_lines(tra_uri) ⇒ Object



65
66
67
68
69
# File 'lib/gri/ldb.rb', line 65

def self.get_gritab_lines tra_uri
  lines = nil
  tra_uri.path = '/gritab'
  get_lines tra_uri
end

.get_lines(uri) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/gri/ldb.rb', line 71

def self.get_lines uri
  case uri.scheme
  when 'http'
    res = Net::HTTP.get uri
    lines = res.split /\n/
  when 'tra'
    sock = TCPSocket.new(uri.host, uri.port || 7079)
    sock.puts "#{uri.path} #{uri.query}"
    lines = tra_gets sock
    sock.close
  end
  lines
end

.tra_gets(sock) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/gri/ldb.rb', line 85

def self.tra_gets sock
  lines = []
  while line = sock.gets
    line.chomp!
    break if line == '.'
    line.sub!(/\A\./, '')
	lines.push line
  end
  lines
end

Instance Method Details

#closeObject



146
147
148
# File 'lib/gri/ldb.rb', line 146

def close
  @sock.close rescue nil
end

#get_after(data_name, time, pos = 0) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/gri/ldb.rb', line 102

def get_after data_name, time, pos=0
  open_after(data_name, time, pos) {|t, line, pos|
    h = LTSV.parse_string line
    t ||= Time.at(h['_time'].to_i)
    yield t, h, pos
  }
end

#get_data_namesObject



150
151
152
153
154
155
156
157
158
159
# File 'lib/gri/ldb.rb', line 150

def get_data_names
  @tra_uri.path = '/get_data_names'
  @tra_uri.query = "h=#{@host}"
  lines = self.class.get_lines @tra_uri
  lines.inject({}) {|h, line|
    data_name, interval = line.split /_/
    h[data_name || ''] = (interval || 300).to_i
    h
  }
end

#open_after(data_name, time, pos = 0) ⇒ Object



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
# File 'lib/gri/ldb.rb', line 110

def open_after data_name, time, pos=0
  case @tra_uri.scheme
  when 'http'
    http = Net::HTTP.new @tra_uri.host, @tra_uri.port
    begin
      url = @tra_uri + "get?h=#{@host}&s=#{data_name}&t=#{time.to_i}"
      res = http.get url.request_uri
      res.body.each_line {|line| yield nil, line.chomp, pos}
    end while(time = res['x-gri-continue'])
    http.finish
  when 'tra'
    sock = sock_connect @tra_uri.host, @tra_uri.port
    begin
      query = "h=#{@host}&s=#{data_name}&t=#{time.to_i}&pos=#{pos}"
      sock.puts "/get #{query}"
      lines = self.class.tra_gets sock
      ind = lines.index ''
      headers = lines.slice!(0, ind+1)
      if headers.detect {|line| line =~ /x-gri-pos: (\d+)/i}
        pos = $1.to_i
      end
      lines.each {|line| yield nil, line, pos}
      if headers.detect {|line| line =~ /x-gri-continue: (\d+) (\d+)/i}
        time = $1.to_i
        pos = $2.to_i
      else
        time = nil
      end
    end while time
  end
end

#sock_connect(host, port) ⇒ Object



142
143
144
# File 'lib/gri/ldb.rb', line 142

def sock_connect host, port
  @sock ||= TCPSocket.new host, port
end