pangdudu-robots 0.2.3.2 → 0.2.3.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,2 +1,3 @@
1
1
  #!/bin/bash
2
+ avahi-publish-service "Robots IPC cluster server" _robots._tcp 2687 &
2
3
  dbus-daemon --print-address --config-file=remote.session.dbus.conf
@@ -1,7 +1,7 @@
1
1
  require 'socket' #to get hostname etc.
2
2
  require 'rubygems'
3
3
  require 'rofl' #gem install pangdudu-rofl --source=http://gems.github.com
4
- #local requires, actually don't need them, just to let you know whats going on
4
+ #local requires
5
5
  require 'robots_agent'
6
6
 
7
7
  module Robots
@@ -21,6 +21,8 @@ module Robots
21
21
  @robots_running = true
22
22
  end
23
23
 
24
+ #OVERWRITE FOLLOWING METHODS:
25
+
24
26
  #receives messages from the robots, should be overwritten in the implementing class
25
27
  def receive_msg msg
26
28
  ilog msg
@@ -35,6 +37,8 @@ module Robots
35
37
  end
36
38
  end
37
39
 
40
+ #OVERWRITE END
41
+
38
42
  #check if robots are set up
39
43
  def check_robotsagent
40
44
  @haz_robots = @robotsagent.nil?
@@ -1,7 +1,7 @@
1
1
  require 'rubygems'
2
- require 'dbus' #gem install pangdudu-ruby-dbus --source=http://gems.github.com
3
- require 'rofl' #gem install pangdudu-rofl --source=http://gems.github.com
4
- #local requires, actually don't need them, just to let you know whats going on
2
+ require 'dbus'
3
+ require 'rofl'
4
+ #local requires
5
5
  require 'robots_infrastructure'
6
6
 
7
7
  class Robot
@@ -13,6 +13,9 @@ class Robot
13
13
  dlog "new Robot initialized."
14
14
  @coreservice, @dbussession = nil,nil
15
15
  @listeners,@emitters,@infrastructure = [],{},{}
16
+ @remote_robots_daemons = [] #we'll use avahi service discovery to fill this
17
+ @system_bus = DBus::SystemBus.instance #we'll need this all over
18
+ @active_busses = [] #we'll add them to the dbus loop later on
16
19
  @servicetypes = fill_servicetypes #refer to robots_infrastructure.rb
17
20
  end
18
21
 
@@ -22,9 +25,26 @@ class Robot
22
25
  elog "Agent not configured, exiting!"
23
26
  exit(0)
24
27
  end
25
- setup_robots_service
26
- register_callbacks
27
- #test_messages; #main_loop;
28
+ #we can always register the system (avahi) services, because they don't depend on the robots infrastructure
29
+ register_system_callbacks
30
+ #if our session is connecting to a special (remote robots) daemon, we need a little more care
31
+ if @config[:sessiontype].eql? "robots"
32
+ #if a host and a port were specified, we can connect directly
33
+ unless @config[:daemonhost].eql?("avahi") and @config[:daemonport].eql?("avahi")
34
+ setup_robots_service
35
+ register_robots_callbacks
36
+ else
37
+ dlog "Using avahi auto daemon discovery!"
38
+ dlog "Remote daemons found: #{@remote_robots_daemons.inspect}"
39
+ exit(0)
40
+ end
41
+ end
42
+ #if the session uses the system or session bus, everythings easy
43
+ unless @config[:sessiontype].eql? "robots"
44
+ setup_robots_service
45
+ register_robots_callbacks
46
+ end
47
+ #finally get into the dbus loop
28
48
  end
29
49
 
30
50
  #setup the robots service
@@ -91,23 +111,66 @@ class Robot
91
111
  return nil
92
112
  end
93
113
  end
94
-
95
- #register callback methods
96
- def register_callbacks
97
- #setup callbacks for infrastructure emitters
98
- @emitters.each { |name,emitter| register_emitter_callback name,emitter }
114
+
115
+ #register callbacks for avahi service discovery and other useful services from the local system bus
116
+ def register_system_callbacks
99
117
  #old systems sometimes don't have avahi
100
- if DBus::SystemBus.instance.proxy.ListNames[0].include? "org.freedesktop.Avahi"
118
+ if @system_bus.proxy.ListNames[0].include? "org.freedesktop.Avahi"
101
119
  #setup service browser
102
- bus = DBus::SystemBus.instance #needs to be the system bus for the avahi stuff
103
- proxy = bus.introspect("org.freedesktop.Avahi","/")
120
+ #bus = DBus::SystemBus.instance #needs to be the system bus for the avahi stuff
121
+ proxy = @system_bus.introspect("org.freedesktop.Avahi","/")
104
122
  @avahi = proxy["org.freedesktop.Avahi.Server"]
105
123
  @servicetypes.each { |name,type| register_service_callback name,type }
106
124
  else
107
- wlog "Sorry, no Avahi service running, no magic service discovery."
125
+ wlog "Sorry, no Avahi service running, no auto service discovery."
126
+ #check if we depend on avahi running
127
+ if @config[:daemonhost].eql?("avahi") and @config[:daemonport].eql?("avahi") and @config[:sessiontype].eql?("robots")
128
+ elog "Sorry mate, can't continue without avahi. Change your 'sessiontype' to 'system' or 'session', will now exit."
129
+ exit(0) #cruel, I know, but it makes no sense to continue,
130
+ end
131
+ end
132
+ end
133
+
134
+ #builds a service type specific callback
135
+ def register_service_callback name,type
136
+ #bus = DBus::SystemBus.instance #needs to be the system bus for the avahi stuff
137
+ sb = @avahi.ServiceBrowserNew(-1,-1,"_#{type}._tcp","local",0)
138
+ #now we start the match rule definition
139
+ mr = DBus::MatchRule.new
140
+ mr.type = "signal"
141
+ mr.interface = "org.freedesktop.Avahi.ServiceBrowser"
142
+ mr.path = sb.first
143
+ @system_bus.add_match(mr) { |msg| service_callback name,msg } unless type.eql? "robots"
144
+ #our service is handled specialy
145
+ @system_bus.add_match(mr) { |msg| robots_daemon_callback name,msg } if type.eql? "robots"
146
+ end
147
+
148
+ #gets called on a service change
149
+ def service_callback name,msg
150
+ if msg.member.eql? "ItemNew"
151
+ dlog "new #{name}: #{msg.params[2]}"
152
+ end
153
+ if msg.member.eql? "ItemRemoved"
154
+ dlog "removed #{name}: #{msg.params[2]}"
155
+ end
156
+ end
157
+
158
+ #gets called when a robots daemon has been found
159
+ def robots_daemon_callback name,msg
160
+ if msg.member.eql? "ItemNew"
161
+ dlog "new robots daemon #{name}: #{msg.params[2]}"
162
+ end
163
+ if msg.member.eql? "ItemRemoved"
164
+ dlog "robots daemon removed #{name}: #{msg.params[2]}"
108
165
  end
109
166
  end
110
167
 
168
+ #register robots callback methods
169
+ def register_robots_callbacks
170
+ #setup callbacks for infrastructure emitters
171
+ @emitters.each { |name,emitter| register_emitter_callback name,emitter }
172
+ end
173
+
111
174
  #register an emitter callback
112
175
  def register_emitter_callback name,emitter
113
176
  bus = get_bus
@@ -131,38 +194,14 @@ class Robot
131
194
  #local non proxy object, so we need to emit a signal and tell everyone
132
195
  @infrastructure["emitter"].newMessage(msg) if @infrastructure.has_key? "emitter"
133
196
  end
134
-
135
- #builds a service type specific callback
136
- def register_service_callback name,type
137
- bus = DBus::SystemBus.instance #needs to be the system bus for the avahi stuff
138
- sb = @avahi.ServiceBrowserNew(-1,-1,"_#{type}._tcp","local",0)
139
- #now we start the match rule definition
140
- mr = DBus::MatchRule.new
141
- mr.type = "signal"
142
- mr.interface = "org.freedesktop.Avahi.ServiceBrowser"
143
- mr.path = sb.first
144
- bus.add_match(mr) { |msg| service_callback name,msg }
145
- end
146
-
147
- #gets called on a service change
148
- def service_callback name,msg
149
- if msg.member.eql? "ItemNew"
150
- dlog "new #{name}: #{msg.params[2]}"
151
- end
152
- if msg.member.eql? "ItemRemoved"
153
- dlog "removed #{name}: #{msg.params[2]}"
154
- end
155
- end
156
-
197
+
157
198
  #check if a service is running
158
199
  def running? service_name
159
200
  bus = get_bus
160
201
  return bus.proxy.ListNames[0].include? service_name
161
202
  end
162
203
 
163
- #functions used by the module
164
-
165
- #send
204
+ #send a message over the robots service
166
205
  def send_msg msg
167
206
  @emitters["robots"].send_message(msg) unless @emitters["robots"].nil?
168
207
  wlog "emitter is nil, can not send message." if @emitters["robots"].nil?
@@ -184,13 +223,15 @@ class Robot
184
223
  #get the bus session, cleaner then creating new sessions all over
185
224
  def get_bus
186
225
  if @dbussession.nil?
187
- @dbussession = DBus::SystemBus.instance if @config[:sessiontype].eql? "system"
226
+ @dbussession = @system_bus if @config[:sessiontype].eql? "system"
188
227
  @dbussession = DBus::SessionBus.instance if @config[:sessiontype].eql? "session"
189
228
  if @config[:sessiontype].eql? "robots"
190
229
  DBus.const_set("SessionSocketName", @config[:robots_socket_name]) #overwrite the modules constant
191
230
  get_remote_cookie if @config[:sshcookie] #i need not say more,...
192
231
  @dbussession = DBus::SessionBus.instance
193
232
  end
233
+ #need to add them to the dbus_loop later on
234
+ @active_busses << @dbussession unless @active_busses.include? @dbussession
194
235
  end
195
236
  #no point if this is still true :)
196
237
  if @dbussession.nil?
@@ -220,27 +261,7 @@ class Robot
220
261
  dlog "Nothing to hack, boring."
221
262
  end
222
263
  end
223
-
224
- #functions used by the module end here
225
-
226
- #dbus main loop
227
- def main_loop
228
- dlog "going into main loop..."
229
- loop do
230
- sleep 1
231
- end
232
- end
233
-
234
- def test_messages
235
- Thread.new do
236
- loop do
237
- msg = "it worx from robots agent!"
238
- send_msg msg
239
- sleep 1
240
- end
241
- end
242
- end
243
-
264
+
244
265
  #list names of a proxy object
245
266
  def list_names proxy=nil
246
267
  if proxy.nil?
@@ -250,4 +271,13 @@ class Robot
250
271
  dlog "listnames:"
251
272
  proxy.ListNames[0].each { |name| dlog name }
252
273
  end
274
+
275
+ #loop we need to stay tuned,broken
276
+ def dbus_loop
277
+ Thread.new do
278
+ main = DBus::Main.new
279
+ @active_busses.each { |bus| main << bus }
280
+ main.run
281
+ end
282
+ end
253
283
  end
@@ -32,21 +32,22 @@ class Emitter < DBus::Object
32
32
  end
33
33
  end
34
34
 
35
-
36
35
  #hardcoding i don't want in the agent file
37
36
  def fill_servicetypes
38
37
  types = {}
38
+ types["Robots IPC cluster server"] = "robots"
39
39
  types["Workstation"] = "workstation"
40
40
  types["SSH Remote Terminal"] = "ssh"
41
- types["Website"] = "http"
42
- types["Secure Website"] = "https"
43
- types["iChat Presence"] = "presence"
44
- types["PulseAudio Sound Server"] = "pulse-server"
45
- types["Subversion Revision Control"] = "svn"
46
- types["GIT"] = "git"
47
- types["APT Package Repository"] = "apt"
48
- types["WebDAV"] = "webdav"
49
- types["Secure WebDAV"] = "webdavs"
50
- types["Samba"] = "smb"
41
+ #if you like debug output, uncomment the following lines
42
+ #types["Website"] = "http"
43
+ #types["Secure Website"] = "https"
44
+ #types["iChat Presence"] = "presence"
45
+ #types["PulseAudio Sound Server"] = "pulse-server"
46
+ #types["Subversion Revision Control"] = "svn"
47
+ #types["GIT"] = "git"
48
+ #types["APT Package Repository"] = "apt"
49
+ #types["WebDAV"] = "webdav"
50
+ #types["Secure WebDAV"] = "webdavs"
51
+ #types["Samba"] = "smb"
51
52
  return types
52
53
  end
@@ -15,6 +15,7 @@ class RobotsProto
15
15
  attr_accessor :roboname,:config
16
16
 
17
17
  def initialize
18
+ #rofl_enable_trace
18
19
  @roboname = "Robots Prototype" #name used in alive messages
19
20
  configure #configure the robot
20
21
  ilog "#{@roboname} initialized"
@@ -25,9 +26,9 @@ class RobotsProto
25
26
  @config = get_default_config #get the default config from the module
26
27
  #config values
27
28
  @config[:sessiontype] = "robots" #could be session,system or robots (use robots, if you want it to work over multiple hosts)
28
- @config[:daemonhost] = "localhost"
29
+ @config[:daemonhost] = "q"
29
30
  @config[:daemonport] = "2687"
30
- @config[:sshcookie] = false #if you want to connect remotely, you need to get the daemon cookie over ssh
31
+ @config[:sshcookie] = true #if you want to connect remotely, you need to get the daemon cookie over ssh
31
32
  end
32
33
 
33
34
  #method that gets called when a new message arrives
@@ -52,6 +53,7 @@ rp.release_robots #start the robot agent module
52
53
  loop do
53
54
  sleep 1
54
55
  #send an xml message over the system
56
+ puts "send msg"
55
57
  alive_msg = rp.create_xml_alive_msg
56
58
  rp.send_msg alive_msg
57
59
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pangdudu-robots
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3.2
4
+ version: 0.2.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - pangdudu