views:

326

answers:

0

[EDIT NOTE: Noticed I had put the mutex creation in the constructor. Moved it and noticed no change.]

[EDIT NOTE 2: I changed the call to app.exec in a trial run to

while TRUE do
    app.processEvents()
    puts '."
end

I noticed that once the Soap4r service started running no process events ever got called again]

[EDIT NOTE 3: Created an associated question here: http://stackoverflow.com/questions/1597145/thread-lockup-in-ruby-with-soap4r%5D

I'm attempting to write a ruby program that receives SOAP commands to draw on a monitor (thus allowing remote monitor access). I've put together a simple test app to prototype the idea. The graphic toolkit is QT. I'm having what I assume is a problem with locking. I've added calls to test the methods in the server in the code shown. The server side that I'm testing right now is:

require 'rubygems'
require 'Qt4'
require 'thread'
require 'soap/rpc/standaloneserver'

class Box < Qt::Widget
 def initialize(parent = nil)
  super
  setPalette(Qt::Palette.new(Qt::Color.new(250,0,0)))
  setAutoFillBackground(true)
  show
 end
end

class SOAPServer < SOAP::RPC::StandaloneServer
    @@mutex = Mutex.new

 def initialize(* args)
  super

  # Exposed methods
  add_method(self, 'createWindow', 'x', 'y', 'width', 'length')

 end

 def createWindow(x, y, width, length)
  puts 'received call'
  windowID = 0
  puts @boxList.length
  puts @parent

  @@mutex.synchronize do
   puts 'in lock'
   box = Box.new(@parent)
   box.setGeometry(x, y, width, length)

   windowID = @boxList.push(box).length
   print "This:", windowID, "--\n"
  end
  puts 'out lock'
  return windowID
 end

 def postInitialize (parent)
  @parent = parent
  @boxList = Array.new
 end
end


windowSizeX = 400
windowSizeY = 300

app = Qt::Application.new(ARGV)
mainwindow = Qt::MainWindow.new
mainwindow.resize(windowSizeX, windowSizeY)
mainwindow.show

puts 'Attempting server start'

myServer = SOAPServer.new('monitorservice', 'urn:ruby:MonitorService', 'localhost', 4004)
myServer.postInitialize(mainwindow)
Thread.new do
 puts 'Starting?'
 myServer.start
 puts 'Started?'
end

Thread.new do
 myServer.createWindow(10,0,10,10)
 myServer.createWindow(10,30,10,10)
 myServer.createWindow(10,60,10,10)
 myServer.createWindow(10,90,10,10)
end

myServer.createWindow(10,10,10,10)

Thread.new do
 app.exec
end

gets

Now when I run this I get the following output:

Attempting server start
Starting?
received call
0
#<Qt::MainWindow:0x60fea28>
in lock
received call
0
#<Qt::MainWindow:0x60fea28>
This:1--
in lock
This:2--
out lock

At that point I hang rather than recieving the total of five additions I expect. Qt does display the squares defined by "createWindow(10,0,10,10)" and "createWindow(10,10,10,10)". Given that "This:1--" and "This:2--" show within a nexted in/out lock pair I'm assuming I'm using mutex horribly wrong. This is my first time with threading in Ruby.