pangdudu-robots 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,27 @@
1
+ = ROBOTS
2
+
3
+ Start your clocks boys and girls, it's weekend coding time!
4
+
5
+ On monday I want: working messaging, data-mapper/mysql memory agent, visualizer agent with agent introspection, slim agent that requests modules,classes over the net and evals them online.
6
+
7
+ Let's try to built a nice easy to use multiagent framework
8
+ based on all the cool stuff we can get for ruby.
9
+
10
+ rObOts!
11
+
12
+ == Installation
13
+
14
+ sudo apt-get install avahi-daemon avahi-utils libavahi-client-dev libavahi-common-dev libavahi-compat-libdnssd-dev libnss-mdns libdbus-1-dev libdbus-glib-1-dev
15
+ sudo gem install dnssd
16
+ sudo gem install pangdudu-ruby-dbus --source=http://gems.github.com
17
+ sudo gem install pangdudu-rofl --source=http://gems.github.com
18
+
19
+ == Usage
20
+
21
+ == Rule the universe!
22
+
23
+ Robots are coming!
24
+
25
+ == License
26
+
27
+ GPL -> http://www.gnu.org/licenses/gpl.txt
data/bin/robots ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/ruby
2
+ puts "ROBOTS!"
@@ -0,0 +1,33 @@
1
+ <!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
2
+ <!-- place this file under /etc/dbus-1/system.d/ -->
3
+ <busconfig>
4
+ <policy user="root">
5
+ <allow own="org.robots.Service" />
6
+ <allow own="org.ruby.Service" />
7
+ </policy>
8
+ <policy group="netdev">
9
+ <allow own="org.robots.Service" />
10
+ <allow own="org.ruby.Service" />
11
+ </policy>
12
+ <policy group="robots">
13
+ <allow own="org.robots.Service" />
14
+ <allow own="org.ruby.Service" />
15
+ </policy>
16
+ <policy at_console="true">
17
+ <allow own="org.robots.Service" />
18
+ <allow own="org.ruby.Service" />
19
+ </policy>
20
+ <policy context="default">
21
+ <allow send_destination="org.robots.Service"/>
22
+ <allow receive_sender="org.robots.Service"/>
23
+ <allow send_destination="org.ruby.Service"/>
24
+ <allow receive_sender="org.ruby.Service"/>
25
+ <allow send_destination="org.robots.Service" send_interface="org.robots.Service.EmitterInterface" />
26
+ <allow send_destination="org.robots.Service" send_interface="org.robots.Service.SampleInterface" />
27
+ <allow send_destination="org.ruby.Service" send_interface="org.ruby.SampleInterface" />
28
+ <!-- introspection is allowed -->
29
+ <allow send_destination="org.robots.Service" send_interface="org.freedesktop.DBus.Introspectable" />
30
+ <allow send_destination="org.ruby.Service" send_interface="org.freedesktop.DBus.Introspectable" />
31
+ </policy>
32
+ </busconfig>
33
+
data/lib/robots.rb ADDED
@@ -0,0 +1,36 @@
1
+ require 'rubygems'
2
+ require 'rofl' #gem install pangdudu-rofl --source=http://gems.github.com
3
+ #local requires, actually don't need them, just to let you know whats going on
4
+ require 'robots_agent'
5
+
6
+ module Robots
7
+
8
+ attr_accessor :robotsagent,:haz_robots
9
+
10
+ #release the robots
11
+ def release_robots
12
+ check_robotsagent
13
+ end
14
+
15
+ #receives messages from the robots, should be overwritten in the implementing class
16
+ def receive_msg msg
17
+ ilog msg
18
+ end
19
+
20
+ #sends messages to the robots, should be overwritten in the implementing class
21
+ def send_msg msg
22
+ @robotsagent.send_msg msg if @haz_robots
23
+ end
24
+
25
+ #check if robots are set up
26
+ def check_robotsagent
27
+ @haz_robots = @robotsagent.nil?
28
+ if @haz_robots
29
+ @robotsagent = Robot.new
30
+ @robotsagent.run
31
+ @robotsagent.add_listener self
32
+ end
33
+ return @haz_robots
34
+ end
35
+
36
+ end
@@ -0,0 +1,203 @@
1
+ require 'rubygems'
2
+ require 'dbus' #gem install sdague-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
5
+ require 'robots_infrastructure'
6
+
7
+ class Robot
8
+ attr_accessor :avahi
9
+ attr_accessor :emitters,:infrastructure,:listeners
10
+
11
+ def initialize
12
+ dlog "new Robot initialized."
13
+ @robotsservice = "org.robots.Service"
14
+ @emitterpath = "/org/robots/Service/Emitter"
15
+ @emitterinterface = "org.ruby.Service.EmitterInterface"
16
+ @coreservice = nil
17
+ @listeners = [] # listeners for incoming messages are here
18
+ @emitters = {} #holds emitters we use
19
+ @infrastructure = {} #holds infrastructure objects
20
+ @servicetypes = fill_servicetypes
21
+ end
22
+
23
+ #start the mojo
24
+ def run
25
+ setup_robots_service
26
+ register_callbacks
27
+ #test_messages; #main_loop;
28
+ end
29
+
30
+ #setup the robots service
31
+ def setup_robots_service
32
+ robots_running = running? @robotsservice
33
+ dlog "robots service has already been started." if robots_running
34
+ unless robots_running
35
+ ilog "will now setup robots service infrastructure."
36
+ start_core_service @robotsservice
37
+ init_robots_infrastructure
38
+ end
39
+ get_robots_infrastructure
40
+ end
41
+
42
+ #ROBOTS SERVICE INIT START
43
+
44
+ =begin
45
+ the following methods are only called, if the robots service is not already running
46
+ =end
47
+
48
+ #setup the infrastructure objects for communication
49
+ def init_robots_infrastructure
50
+ emitter = Emitter.new(@emitterpath,self)
51
+ @coreservice.export(emitter)
52
+ @infrastructure["emitter"] = emitter
53
+ end
54
+
55
+ #get instances of our infrastructure objects
56
+ def get_robots_infrastructure
57
+ emitter = get_object_from_service @robotsservice,@emitterpath,@emitterinterface
58
+ @emitters["robots"] = emitter
59
+ end
60
+
61
+ #start a service
62
+ def start_core_service service_name=@robotsservice
63
+ bus = DBus::system_bus
64
+ #first of all check if the service we want to launch is already started
65
+ dlog "skipping service: #{service_name} has already been started." if running? service_name
66
+ unless running? service_name
67
+ begin
68
+ @coreservice = bus.request_service(service_name)
69
+ rescue
70
+ elog "service request for #{service_name} denied."
71
+ end
72
+ end
73
+ end
74
+
75
+ #ROBOTS SERVICE INIT END
76
+
77
+ #get a dbus object from a service
78
+ def get_object_from_service service_name,obj_path,default_iface
79
+ begin
80
+ if running? service_name
81
+ bus = DBus::system_bus
82
+ ruby_service = bus.service(service_name)
83
+ obj = ruby_service.object(obj_path)
84
+ obj.introspect
85
+ obj.default_iface = default_iface
86
+ return obj
87
+ end
88
+ return nil
89
+ rescue
90
+ elog "could not get object:#{obj_path} from service: #{service_name}"
91
+ return nil
92
+ end
93
+ end
94
+
95
+ #register callback methods
96
+ def register_callbacks
97
+ bus = DBus::system_bus
98
+ #setup callbacks for infrastructure emitters
99
+ @emitters.each { |name,emitter| register_emitter_callback name,emitter }
100
+ #setup service browser
101
+ proxy = bus.introspect("org.freedesktop.Avahi","/")
102
+ @avahi = proxy["org.freedesktop.Avahi.Server"]
103
+ @servicetypes.each { |name,type| register_service_callback name,type }
104
+ end
105
+
106
+ #register an emitter callback
107
+ def register_emitter_callback name,emitter
108
+ bus = DBus::system_bus
109
+ dlog "registering callback for #{name} on path #{emitter.path}"
110
+ mr = DBus::MatchRule.new
111
+ mr.type = "signal"
112
+ mr.interface = "org.ruby.Service.EmitterInterface"
113
+ mr.path = emitter.path
114
+ bus.add_match(mr) { |msg| emitter_proxy_callback msg }
115
+ end
116
+
117
+ #get called when receiving an remote emitter message
118
+ def emitter_proxy_callback msg
119
+ @listeners.each { |l| l.receive_msg msg.params[0] } unless msg.params[0].nil?
120
+ end
121
+
122
+ #gets called from the exported emitter object
123
+ def emitter_callback msg
124
+ #when other agents use the send_message of the proxy, it will only reach the
125
+ #local non proxy object, so we need to emit a signal and tell everyone
126
+ @infrastructure["emitter"].newMessage(msg) if @infrastructure.has_key? "emitter"
127
+ end
128
+
129
+ #builds a service type specific callback
130
+ def register_service_callback name,type
131
+ bus = DBus::system_bus
132
+ sb = @avahi.ServiceBrowserNew(-1,-1,"_#{type}._tcp","local",0)
133
+ #now we start the match rule definition
134
+ mr = DBus::MatchRule.new
135
+ mr.type = "signal"
136
+ mr.interface = "org.freedesktop.Avahi.ServiceBrowser"
137
+ mr.path = sb.first
138
+ bus.add_match(mr) { |msg| service_callback name,msg }
139
+ end
140
+
141
+ #gets called on a service change
142
+ def service_callback name,msg
143
+ if msg.member.eql? "ItemNew"
144
+ dlog "new #{name}: #{msg.params[2]}"
145
+ end
146
+ if msg.member.eql? "ItemRemoved"
147
+ dlog "removed #{name}: #{msg.params[2]}"
148
+ end
149
+ end
150
+
151
+ #check if a service is running
152
+ def running? service_name
153
+ bus = DBus::system_bus
154
+ return bus.proxy.ListNames[0].include? service_name
155
+ end
156
+
157
+ #functions used by the module
158
+
159
+ #send
160
+ def send_msg msg
161
+ @emitters["robots"].send_message(msg) unless @emitters["robots"].nil?
162
+ end
163
+
164
+ #add a listener
165
+ def add_listener listener
166
+ #check if the method is implemented
167
+ impcheck = defined? listener.receive_msg
168
+ #if it is add to listeners
169
+ @listeners << listener if impcheck.eql? "method"
170
+ #if not inform dev
171
+ unless impcheck.eql? "method"
172
+ elog "Method 'receive_msg string' needs to be implemented! Example:"
173
+ puts "def receive_msg msg\n puts msg\nend"
174
+ end
175
+ end
176
+
177
+ #functions used by the module end here
178
+
179
+ #dbus main loop
180
+ def main_loop
181
+ dlog "going into main loop..."
182
+ loop do
183
+ sleep 1
184
+ end
185
+ end
186
+
187
+ def test_messages
188
+ Thread.new do
189
+ loop do
190
+ msg = "it worx from alpha!"
191
+ send_msg msg
192
+ sleep 1
193
+ end
194
+ end
195
+ end
196
+
197
+ #list names of a proxy object
198
+ def list_names proxy=@dbus.proxy
199
+ dlog "listnames:"
200
+ proxy.ListNames[0].each { |name| dlog name }
201
+ @services.each { |name,service| dlog "service introspection for #{name} introspection: #{service.introspect}" }
202
+ end
203
+ end
@@ -0,0 +1,52 @@
1
+ =begin
2
+ this includes all the necessary dbus objects we need for the robots infrastructure
3
+ =end
4
+ require 'rubygems'
5
+ require 'dbus' #gem install sdague-ruby-dbus --source=http://gems.github.com
6
+ require 'rofl' #gem install pangdudu-rofl --source=http://gems.github.com
7
+
8
+ #basic emitter class
9
+ class Emitter < DBus::Object
10
+
11
+ attr_accessor :listener
12
+
13
+ def initialize path,listener
14
+ super path
15
+ @listener = listener
16
+ dlog "new Emitter initialized at #{path}"
17
+ end
18
+
19
+ #create an dbus interface
20
+ dbus_interface "org.ruby.Service.EmitterInterface" do
21
+ #creates a signal in the interface:
22
+ dbus_signal :newMessage, "msgbody:s"
23
+ #creates an emit method in the interface:
24
+ dbus_method :send_message, "in msg:s" do |msg|
25
+ send_message_callback msg
26
+ end
27
+ end #end of the dbus interface
28
+
29
+ #this method gets called by remote proxy objects calling send_message
30
+ def send_message_callback msg
31
+ @listener.emitter_callback msg
32
+ end
33
+ end
34
+
35
+
36
+ #hardcoding i don't want in the agent file
37
+ def fill_servicetypes
38
+ types = {}
39
+ types["Workstation"] = "workstation"
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"
51
+ return types
52
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pangdudu-robots
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - pangdudu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-17 00:00:00 -07:00
13
+ default_executable: robots
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: dbus
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: robots!
26
+ email: pangdudu@github
27
+ executables:
28
+ - robots
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.rdoc
33
+ files:
34
+ - README.rdoc
35
+ - bin/robots
36
+ - lib/robots.rb
37
+ - lib/robots_agent.rb
38
+ - lib/robots_infrastructure.rb
39
+ - config/org.robots.service.conf
40
+ has_rdoc: true
41
+ homepage: http://github.com/pangdudu/robots
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project: http://github.com/pangdudu/robots
62
+ rubygems_version: 1.2.0
63
+ signing_key:
64
+ specification_version: 2
65
+ summary: more robots!
66
+ test_files: []
67
+