From: Robert Klemme Date: 2005-10-13T17:26:54+09:00 Subject: Re: event based model - best way to implement? snacktime wrote: > I've gotten a ways on this project and now have another hurdle to > cross. > > Right now I have a server that connects to the asterisk manager > interface (a simple tcp line based protocol) and stays connected, > acting as a kind of proxy for connecting clients instead of making a > new connection to asterisk for each request. > > One thread constantly reads events from asterisk. Each event is stuck > into a hash and the hash is pushed onto an array. Using the array > like this can change, it just happens to be what I've been using so > far. Usually one would use a Queue instead of the array because the Queue is thread safe. You'll find one shipped in "thread". However, in this case I'd probably use something hash like. > Clients connect to the server via drb with a request which is sent to > asterisk. The client then waits until a response is available, or > until a timeout is reached. Each client request is tagged with a > unique id when it is sent to asterisk, and asterisk returns that > unique id in the response. > > > > So basically the (abbreviated) code structure is like this, with > some_request_method being the method that is called from the drb > client. What I'm not sure about is how some_request_method will be > able to know when the response is available, or actually what would > be the right way to do this. some_request_method should block until a > response is available. Yep. You can achieve this by using a ConditionVariable. You can see sample usage in my self grown sample queue implementation here http://www.rubygarden.org/ruby?MultiThreading I'd encapsulate this in a class. Something along the lines of this class ResultRepository def initialize @results = {} @cond = ConditionVariable.new @mutex = Mutex.new end def put_result(id, result) @mutex.synchronize do @results[id]=result @cond.signal end end # will block def get_result(id) @mutex.synchronize do @cond.wait(mutex) until @results.contains_key id return @results.delete id end end end Kind regards robert