Hello, First of all I''m new to working with sockets, so the problems I''m having are probably because of my lack of understanding. What I''m trying to accomplish is to create a mac os x client gui to ruby-debug. When a application that includes ruby-debug is started, it starts listening for 2 connections; 8989, 8990 The connection is made by calling the connectToApp method. This sets up 2 connections, which works. I had to place them in a Thread, because the gui obviously has to resume during the event loop of EventMachine. When the other app that has the debugger server gets to a breakpoint, it sends data to the client. This works as it should, the receive_data method of the DebuggerClient module gets called and sends the data to the received_client_data method of the Controller class. This method then assigns the data to the gui. So far so good.... However, sending data back to the debugger is a problem. What happens is this; the gui calls the send_client_command method of the Controller class with the data/command. It then gets passed to the method send_command of the connection instance that is connected to port 8989. Which is: @connections[:client] It gets there as it should, because in the console I see the message: "Trying to send data: some data" However, the data never gets there... Now I read somewhere in the EventMachine source, that a send_data call should be send within the scope of a receive_data method. Is this true, is it impossible for me to send data whenever I want to?? Maybe this isn''t the way that EventMachine should be used at all?? Is it even possible?? I''ve started by using the gem that''s available on rubyforge, after a lot of trying stuff out I''ve build the 0.7.0 version from the trunk. I hope the info I provided is enough, if not please let me know and I''ll try to explain myself further. Kind regards, Eloy. PS: I hate the global $DEBUG_OUTPUT, but I can''t figure out a way to assign the Controller object in another way. If anyone has another suggestion...... EXAMPLE CODE: require ''osx/cocoa'' require ''eventmachine'' module DebuggerClient def receive_data(data) $DEBUG_OUTPUT.received_client_data data end def send_command(data) puts "Trying to send data: #{data}" send_data data end end module DebuggerController def receive_data(data) $DEBUG_OUTPUT.received_controller_data data end def send_command(data) puts "Trying to send data: #{data}" send_data data end end class Controller < OSX::NSObject ib_outlets :commandClient, :commandController, :outputClient, :outputController def connectToApp(sender) puts ''INFO: Connecting'' # This global var is set to the Controller instance, # this way the connection instances can send the data back to the controller.... $DEBUG_OUTPUT = self begin @connections = Thread.new do EventMachine::run { Thread.current[:client] = EventMachine::connect "localhost", 8989, DebuggerClient Thread.current[:controller] = EventMachine::connect "localhost", 8990, DebuggerController } end rescue @outputClient.setString ''Unable to connect...'' @outputController.setString ''Unable to connect...'' end end # A data_received event has occured def received_client_data(data) @outputClient.setString data end def received_controller_data(data) @outputController.setString data end # Send the command to the debugger def send_client_command(command) @connections[:client].send_command command.to_s end def send_controller_command(command) @connections[:controller].send_command command.to_s end end
Hello, First of all I''m new to working with sockets, so the problems I''m having are probably because of my lack of understanding. What I''m trying to accomplish is to create a mac os x client gui to ruby-debug. When a application that includes ruby-debug is started, it starts listening for 2 connections; 8989, 8990 The connection is made by calling the connectToApp method. This sets up 2 connections, which works. I had to place them in a Thread, because the gui obviously has to resume during the event loop of EventMachine. When the other app that has the debugger server gets to a breakpoint, it sends data to the client. This works as it should, the receive_data method of the DebuggerClient module gets called and sends the data to the received_client_data method of the Controller class. This method then assigns the data to the gui. So far so good.... However, sending data back to the debugger is a problem. What happens is this; the gui calls the send_client_command method of the Controller class with the data/command. It then gets passed to the method send_command of the connection instance that is connected to port 8989. Which is: @connections[:client] It gets there as it should, because in the console I see the message: "Trying to send data: some data" However, the data never gets there... Now I read somewhere in the EventMachine source, that a send_data call should be send within the scope of a receive_data method. Is this true, is it impossible for me to send data whenever I want to?? Maybe this isn''t the way that EventMachine should be used at all?? Is it even possible?? I''ve started by using the gem that''s available on rubyforge, after a lot of trying stuff out I''ve build the 0.7.0 version from the trunk. I hope the info I provided is enough, if not please let me know and I''ll try to explain myself further. Thanks in advance! Kind regards, Eloy. PS: I hate the global $DEBUG_OUTPUT, but I can''t figure out a way to assign the Controller object in another way. If anyone has another suggestion...... EXAMPLE CODE: require ''osx/cocoa'' require ''eventmachine'' module DebuggerClient def receive_data(data) $DEBUG_OUTPUT.received_client_data data end def send_command(data) puts "Trying to send data: #{data}" send_data data end end module DebuggerController def receive_data(data) $DEBUG_OUTPUT.received_controller_data data end def send_command(data) puts "Trying to send data: #{data}" send_data data end end class Controller < OSX::NSObject ib_outlets :commandClient, :commandController, :outputClient, :outputController def connectToApp(sender) puts ''INFO: Connecting'' # This global var is set to the Controller instance, # this way the connection instances can send the data back to the controller.... $DEBUG_OUTPUT = self begin @connections = Thread.new do EventMachine::run { Thread.current[:client] = EventMachine::connect "localhost", 8989, DebuggerClient Thread.current[:controller] = EventMachine::connect "localhost", 8990, DebuggerController } end rescue @outputClient.setString ''Unable to connect...'' @outputController.setString ''Unable to connect...'' end end # A data_received event has occured def received_client_data(data) @outputClient.setString data end def received_controller_data(data) @outputController.setString data end # Send the command to the debugger def send_client_command(command) @connections[:client].send_command command.to_s end def send_controller_command(command) @connections[:controller].send_command command.to_s end end
On 11/3/06, Eloy Duran <eloy.de.enige at gmail.com> wrote:> Hello, > > First of all I''m new to working with sockets, > so the problems I''m having are probably because of my lack of understanding. > > What I''m trying to accomplish is to create a mac os x client gui to ruby-debug. > When a application that includes ruby-debug is started, > it starts listening for 2 connections; 8989, 8990 > The connection is made by calling the connectToApp method. > This sets up 2 connections, which works. > I had to place them in a Thread, because the gui obviously > has to resume during the event loop of EventMachine.What happens if you put the whole event-loop in a thread: Thread.new { EventMachine.run { ..... } }
> What happens if you put the whole event-loop in a thread: > > Thread.new { > EventMachine.run { > ..... > } > }Hi Francis, I''m sorry, but I don''t really understand what you mean to put inside the "whole event-loop"? Right now I have this: @connections = Thread.new do EventMachine.run { Thread.current[:client] = EventMachine::connect "localhost", 8989, DebuggerClient Thread.current[:controller] = EventMachine::connect "localhost", 8990, DebuggerController } end Do you mean that I should put the module code inside the thread? Cheers, Eloy.
On 11/5/06, Eloy Duran <eloy.de.enige at gmail.com> wrote:> > What happens if you put the whole event-loop in a thread: > > > > Thread.new { > > EventMachine.run { > > ..... > > } > > } > > Hi Francis, > > I''m sorry, but I don''t really understand what > you mean to put inside the "whole event-loop"? > > Right now I have this: > @connections = Thread.new do > EventMachine.run { > Thread.current[:client] = EventMachine::connect > "localhost", 8989, DebuggerClient > Thread.current[:controller] = EventMachine::connect > "localhost", 8990, DebuggerController > } > end > > Do you mean that I should put the module code inside the thread? > > Cheers, > Eloy. > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk >AFAIK..its not true that send_data can be called within scope of receive_data method only. All, you need is the object of ConnectionController or Debug Controller and then from anywhere in your code you can do. @that_object.send_data "Here i go\n" -- There was only one Road; that it was like a great river: its springs were at every doorstep, and every path was its tributary.
> AFAIK..its not true that send_data can be called within scope of > receive_data method only. All, you need is the object of > ConnectionController or Debug Controller and then from anywhere in > your code you can do. > > @that_object.send_data "Here i go\n"Well I do have a reference to the object, which is @connections[:client] If I call the method send_command on it, I see the puts output in the console so it should have arrived. Then it calls send_data, but nothing happens.... If I output the result of the call to the log I only get back the length of the string. E.g.: p @connections[:client].send_command(''whatever'') #=> 8 I don''t know if that''s a good thing or not?
Hahaha, what a stupid mistake on my side :) I took another look at the string you send in your example and finally noticed the new line.... I was used to using puts on a socket, which probably adds the new line itself. So it works now! Thank you very much. Eloy.