Class: Gom::Remote::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/gom/remote/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, callback_port = nil) ⇒ Connection

url: initial GOM url, path or attribute. The remote GOM server address gets extracted from this and, unless nil, the given block will be called with the remaining GOM path, aka:

url == http://gom:1234/foo/bar:attribute

will use ‘gom:1234’ as GOM server and call the block with ‘/foo/bar:attribute’ as path argument.



39
40
41
42
43
44
45
46
# File 'lib/gom/remote/connection.rb', line 39

def initialize url, callback_port = nil
  @target_url, @initial_path = (Connection.split_url url)
  #Gom::Remote.connection and (raise "connection already open")
  Gom::Remote.connection = self

  @subscriptions = []
  @callback_server = init_callback_server callback_port
end

Instance Attribute Details

#callback_serverObject (readonly)

Returns the value of attribute callback_server.



12
13
14
# File 'lib/gom/remote/connection.rb', line 12

def callback_server
  @callback_server
end

#initial_pathObject (readonly)

Returns the value of attribute initial_path.



12
13
14
# File 'lib/gom/remote/connection.rb', line 12

def initial_path
  @initial_path
end

#target_urlObject (readonly)

Returns the value of attribute target_url.



12
13
14
# File 'lib/gom/remote/connection.rb', line 12

def target_url
  @target_url
end

Class Method Details

.init(url, callback_port = nil) ⇒ Object



24
25
26
27
# File 'lib/gom/remote/connection.rb', line 24

def init url, callback_port = nil
  connection = (self.new url, callback_port)
  [connection, connection.initial_path]
end

.split_url(url) ⇒ Object

take apart the URL into GOM and node path part



16
17
18
19
20
21
22
# File 'lib/gom/remote/connection.rb', line 16

def split_url url
  u = URI.parse url
  re = %r|#{u.scheme}://#{u.host}(:#{u.port})?|
    server = (re.match url).to_s
  path = (url.sub server, '').sub(/\/$/, '')
  [server, path]
end

Instance Method Details

#destroy(path) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gom/remote/connection.rb', line 69

def destroy path
  url = "#{@target_url}#{path}"
  uri = URI.parse url
  req = Net::HTTP::Delete.new uri.path

  session = (Net::HTTP.new uri.host, uri.port)
  case res = session.start { |http| http.request req }
  when Net::HTTPSuccess, Net::HTTPRedirection
    # OK
  else
    res.error!
  end
end

#read(path) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/gom/remote/connection.rb', line 83

def read path
  url = "#{@target_url}#{path}"
  open(url).read
rescue Timeout::Error => e
  raise "connection timeout: #{url}"
rescue OpenURI::HTTPError => e
  case code = e.to_s.to_i rescue 0
  when 404
    raise NameError, "undefined: #{path}"
  else
    puts " ## gom connection error: #{url} -- #{e}"
    throw e
  end
rescue => e
  puts " ## read error: #{url} -- #{e}"
  throw e
end

#refreshObject

update subscription observers. GNP callbacks will look like:

http://<callback server>:<callback port>/gnp;<subscription name>;<subscription path>


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/gom/remote/connection.rb', line 105

def refresh
  puts " -- refresh subscriptions(#{@subscriptions.size}):"

  run_callback_server # call it once to make sure it runs
  
  @subscriptions.each do |sub| 
    puts "     - #{sub.name}"
    params = { "attributes[accept]" => 'application/json' }

    query = "/gnp;#{sub.name};#{sub.entry_uri}"
    params["attributes[callback_url]"] = "#{callback_server.base_url}#{query}"

    [:operations, :uri_regexp, :condition_script].each do |key|
      (v = sub.send key) and params["attributes[#{key}]"] = v
    end

    url = "#{@target_url}#{sub.uri}"
    http_put(url, params) # {|req| req.content_type = 'application/json'}
  end
end

#subscribe(sub) ⇒ Object



126
127
128
129
# File 'lib/gom/remote/connection.rb', line 126

def subscribe sub
  @subscriptions.delete sub # every sub only once!
  @subscriptions.push sub
end

#write(path, value) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/gom/remote/connection.rb', line 48

def write path, value
  if value.kind_of? Hash 
    write_node path, attributes
  else
    write_attribute path, value
  end
end

#write_attribute(path, value) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/gom/remote/connection.rb', line 56

def write_attribute path, value
  # TODO: Primitive#encode returns to_s for all unknow types. exception
  # would be correct.
  txt, type = (Gom::Core::Primitive.encode value)
  params = { "attribute" => txt, "type" => type }
  url = "#{@target_url}#{path}"
  http_put(url, params)
end

#write_node(path, attributes) ⇒ Object



65
66
67
# File 'lib/gom/remote/connection.rb', line 65

def write_node path, attributes
  raise "not yet implemented"
end