[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

problem with eventmachine

hemant

9/19/2006 4:14:00 AM

I have following code, the problem is, when i start a connection to
the socket through telnet, the data send by the server i get as
usual, but when i try to do the same through ruby code, the data send
by the server won't come to the client..until the server is shutdown.


def receive_data data
data = data.chomp.strip
if is_symbol?(data)
add_symbol(data)
local_connection = self
start_send_thread(local_connection)
end
end

def start_send_thread(local_connection)
unless @send_started
p "Trying to send the data"
@send_started = true
@local_thread = Thread.new do
loop do
broadcast_data(local_connection)
sleep(0.1)
p "I am running"
end
end
end
end

def broadcast_data(local_connection)
for x in @local_symbols
local_connection.send_data "#{x}"+rand(10).to_s
end
end

def unbind
p "Client closed the connection"
Thread.kill(@local_thread)
end



--
There was only one Road; that it was like a great river: its springs
were at every doorstep, and every path was its tributary.

4 Answers

hemant

9/19/2006 4:35:00 AM

0

On 9/19/06, hemant <gethemant@gmail.com> wrote:
> I have following code, the problem is, when i start a connection to
> the socket through telnet, the data send by the server i get as
> usual, but when i try to do the same through ruby code, the data send
> by the server won't come to the client..until the server is shutdown.
>

Ok that was one stupid wtf....
i forgot to put "\n" in the end of the string, i guess.

--
There was only one Road; that it was like a great river: its springs
were at every doorstep, and every path was its tributary.

Francis Cianfrocca

9/19/2006 11:08:00 AM

0

On 9/19/06, hemant <gethemant@gmail.com> wrote:
> I have following code, the problem is, when i start a connection to
> the socket through telnet, the data send by the server i get as
> usual, but when i try to do the same through ruby code, the data send
> by the server won't come to the client..until the server is shutdown.
>
>

Two points:
First, if you're connecting to this program using telnet, the behavior
will depend on the platform that you run telnet on. The telnet client
on Windows sends data byte-by-byte as you type it in. On Linux, Mac or
other Unix, telnet buffers the data line by line.

Second, and extremely important: you do *not* need to spin a thread to
send your data. The whole point of EventMachine is that it allows you
to handle many connections at once, without using threads. Threads
slow your program down and make it harder to debug.

If you really want to hammer your telnet client with random data based
on @local_symbols, use EventMachine#add_periodic_timer rather than
spinning a thread.

hemant

9/19/2006 12:04:00 PM

0

On 9/19/06, Francis Cianfrocca <garbagecat10@gmail.com> wrote:
> Two points:
> First, if you're connecting to this program using telnet, the behavior
> will depend on the platform that you run telnet on. The telnet client
> on Windows sends data byte-by-byte as you type it in. On Linux, Mac or
> other Unix, telnet buffers the data line by line.
>
> Second, and extremely important: you do *not* need to spin a thread to
> send your data. The whole point of EventMachine is that it allows you
> to handle many connections at once, without using threads. Threads
> slow your program down and make it harder to debug.
>
> If you really want to hammer your telnet client with random data based
> on @local_symbols, use EventMachine#add_periodic_timer rather than
> spinning a thread.
>
>

Thanks for replying.
Random values were just the placeholders, the actual data would be
more important i guess. But my understanding of add_periodic_timer
says that, it would be global to entire program. But what i want is
basically, a method which would be client specific. By being client
specific, I mean...each connection, should have a callback, which
would start sending the data. Please correct me, If i am wrong.

Basically what i want is...push the data to the clients
asynchronously. Using recieve_data I can change @local_symbols and
hence the data pushed to the client should change accordingly.

So, How do i do this?




--
There was only one Road; that it was like a great river: its springs
were at every doorstep, and every path was its tributary.

Francis Cianfrocca

9/19/2006 5:49:00 PM

0

Sorry if I missed you, Hemant- I was hoping to get a bit more
information on your home-grown data-vending server. If the protocol is
appropriate for EM, then you can implement your push server without
spinning any threads.