Permalink
Browse files

0.2.0: serialized XMLRPC call.

  • Loading branch information...
1 parent 08362f7 commit 962c495fa7b562568efdb06f4dd6d3b7830b7cf6 @mootoh mootoh committed Jun 3, 2008
Showing with 69 additions and 20 deletions.
  1. +30 −18 lib/rubigraph.rb
  2. +2 −2 rubigraph.gemspec
  3. +37 −0 test/test_rubigraph.rb
View
@@ -1,37 +1,38 @@
require 'xmlrpc/client'
require 'pp'
+require 'thread'
#
# Ruby wrapper for Ubigraph.
#
# Call Rubigraph.init at first.
#
module Rubigraph
- VERSION = '0.1.0'
+ VERSION = '0.2.0'
class Vertex
attr_reader :id
def initialize(id = nil)
@id = id ?
- Rubigraph.server.call('ubigraph.new_vertex_w_id', id) :
- Rubigraph.server.call('ubigraph.new_vertex')
+ Rubigraph.call('ubigraph.new_vertex_w_id', id) :
+ Rubigraph.call('ubigraph.new_vertex')
raise 'Rubigraph::Vertex.initialize: cannot create vertex' if @id == -1
end
def remove
- if -1 == Rubigraph.server.call('ubigraph.remove_vertex', @id)
+ if -1 == Rubigraph.call('ubigraph.remove_vertex', @id)
raise "Rubigraph::Vertex#remove: cannot remove vertex #{@id}"
end
end
def set_attribute(att, value)
- Rubigraph.server.call('ubigraph.set_vertex_attribute', @id, att, value.to_s)
+ Rubigraph.call('ubigraph.set_vertex_attribute', @id, att, value.to_s)
end
def set_attributes(attrs)
attrs.each do |k, v|
- Rubigraph.server.call('ubigraph.set_vertex_attribute', @id, k, v.to_s)
+ Rubigraph.call('ubigraph.set_vertex_attribute', @id, k, v.to_s)
end
end
@@ -83,27 +84,25 @@ class Edge
# create an Edge.
# from, to should be Vertex.
def initialize(from, to, id = nil)
- if id
- @id = Rubigraph.server.call('ubigraph.new_edge_w_id', id, from.id, to.id)
- else
- @id = Rubigraph.server.call('ubigraph.new_edge', from.id, to.id)
- end
+ @id = id ?
+ Rubigraph.call('ubigraph.new_edge_w_id', id, from.id, to.id) :
+ Rubigraph.call('ubigraph.new_edge', from.id, to.id)
raise 'Rubigraph::Edge.initialize: cannot create edge' if @id == -1
end
def remove
- if -1 == Rubigraph.server.call('ubigraph.remove_edge', @id)
+ if -1 == Rubigraph.call('ubigraph.remove_edge', @id)
raise "Rubigraph::Edge#remove: cannot remove edge #{@id}"
end
end
def set_attribute(att, value)
- Rubigraph.server.call('ubigraph.set_edge_attribute', @id, att, value.to_s)
+ Rubigraph.call('ubigraph.set_edge_attribute', @id, att, value.to_s)
end
def set_attributes(attrs)
attrs.each do |k, v|
- Rubigraph.server.call('ubigraph.set_edge_attribute', @id, k, v.to_s)
+ Rubigraph.call('ubigraph.set_edge_attribute', @id, k, v.to_s)
end
end
@@ -163,15 +162,28 @@ def showstrain=(s)
# initialize XML-RPC client
def self.init(host='127.0.0.1', port='20738')
@server = XMLRPC::Client.new2("http://#{host}:#{port}/RPC2")
+ @mutex = Mutex.new
end
# clear all vertex, edges
def self.clear
- @server.call('ubigraph.clear')
+ call('ubigraph.clear')
end
- # for internal use.
- def self.server
- @server
+ def self.call(msg, *args)
+ @mutex.synchronize {
+ case args.size
+ when 0
+ @server.call(msg)
+ when 1
+ @server.call(msg, args[0])
+ when 2
+ @server.call(msg, args[0], args[1])
+ when 3
+ @server.call(msg, args[0], args[1], args[2])
+ else
+ raise
+ end
+ }
end
end # Rubigraph
View
@@ -1,12 +1,12 @@
Gem::Specification.new do |s|
s.name = %q{rubigraph}
- s.version = "0.1.0"
+ s.version = "0.2.0"
s.specification_version = 2 if s.respond_to? :specification_version=
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["mootoh"]
- s.date = %q{2008-05-29}
+ s.date = %q{2008-06-04}
s.description = %q{a Ruby wrap for Ubigraph (http://www.ubietylab.net/ubigraph). see http://www.ubietylab.net/ubigraph/content/Docs/index.html to get complete description about API.}
s.email = %q{mootoh@gmail.com}
s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
View
@@ -0,0 +1,37 @@
+require 'test/unit'
+require 'lib/rubigraph'
+
+Rubigraph.init
+
+class RubigraphTest < Test::Unit::TestCase
+ def test_clear
+ Rubigraph.clear
+ end
+
+ def test_vertex
+ v = Rubigraph::Vertex.new
+ v.set_attribute('size', '2.0')
+ v.remove
+ end
+
+ def test_edge
+ v1 = Rubigraph::Vertex.new
+ v2 = Rubigraph::Vertex.new
+ e = Rubigraph::Edge.new(v1, v2)
+ e.label = 'edge'
+ e.remove
+ end
+
+ def test_many
+ vs = []
+ 100.times do |i|
+ v = Rubigraph::Vertex.new
+ v.color = sprintf("#%02d%02d%02d", i, i, i)
+ vs.push v
+ end
+
+ 100.times do |i|
+ e = Rubigraph::Edge.new(vs[i], vs[(i+1)%100])
+ end
+ end
+end

0 comments on commit 962c495

Please sign in to comment.