# XMLRPC and Sockets

**URL:** <https://www.ruby-forum.com/t/xmlrpc-and-sockets/121627>\
**Category:** Ruby\
**Created:** [November 29, 2007, 1:39pm UTC](https://www.ruby-forum.com/t/xmlrpc-and-sockets/121627 "2007-11-29T13:39:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![robintw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ec8ea/32.png) [@robintw](https://www.ruby-forum.com/u/robintw)\
**Post date:** [November 29, 2007, 1:39pm UTC](https://www.ruby-forum.com/t/xmlrpc-and-sockets/121627/1 "2007-11-29T13:39:23Z")

</div>

Hi everyone,

I’m currently researching the capabilities of a SmartDust network, my  
goal is to build an application that will get statistics from a network  
management device.

The device is connected to my networkcontroller, I can request for  
information using xml-rpc. This part of the application works (more or  
less), but here’s the problem.

The device can also detect network events, and spam them back (xml) to  
the pc. The procedure is like this:

1. Connect to the management device
2. Login with credentials
3. Ask for a event-port
4. Listen on this port for incoming data

Asking the device for stats was quite straightforward, but I don’t have  
a clue how to capture/parse the incoming data. (threads? xml-rpc  
server?)

I have included some (read: most) information about the device as  
attachment, and copy-pasted my basestation-class below. Sorry for the  
long-ish post. (EDIT: to bad, the pdf file was slightly too large…)

Everyone told me to go Java, but stubborn me decided to go Ruby, so I  
now have to prove Im not a total idiot after all =) Thanks in advance,  
'cause I’m really stuck at this one … =(

#required libraries  
require ‘xmlrpc/client’  
require ‘socket’  
require ‘pp’

class BaseStation

#constructor that will create a token that can be  
#used in other xml-rpc requests or an error message  
#if the logincredentials are incorrect  
def initialize(username, password, url = “192.168.99.100”, port =  
“4445”)  
@url = url;  
@port = port;  
@username = username;  
@password = password

```
begin
  @client = XMLRPC::Client.new3({ :port => port, :host => url })
  result = @client.call "login", username, password
  @token = result
rescue
  puts "Something went wrong"
  false
end

```

end

#method that will generate a configuration  
#request, the return value depends on the  
#querystring  
def get\_config(depth, query)  
begin  
result = @client.call “getConfig”, @token, depth, query  
rescue  
puts “Something went wrong”  
false  
end  
end

#method that will subscribe user to all notifications  
#the return value is a portnumber where these notifications  
#will occur  
def get\_notification\_port(notification = “all”)  
begin  
result = @client.call “subscribe”, @token, notification  
result[1]  
rescue  
puts “Something went wrong”  
false  
end  
end

#method that will spam the notifications generated by the  
#base-station directly to the terminal  
def start\_listening\_for\_notifications(port = 0)  
port = get\_notification\_port()  
pp port  
server = TCPserver.new(“0.0.0.0”, get\_notification\_port())  
loop do  
Thread.start(server.accept) do |s|  
print(server, " is accepted\n")  
s.write(Time.now)  
print(server, " is gone\n")  
s.close  
end  
end  
end

#getter for the token variable  
def login\_token  
@token  
end

end

---

<div class="post-metadata">

**Author:** ![robintw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ec8ea/32.png) [@robintw](https://www.ruby-forum.com/u/robintw)\
**Post date:** [November 30, 2007, 9:28am UTC](https://www.ruby-forum.com/t/xmlrpc-and-sockets/121627/2 "2007-11-30T09:28:51Z")

</div>

ehh… nobody? Anyone?
