[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

EventMachine or GServer?

Arturas Šlajus

5/17/2009 9:52:00 PM

I wrote my nifty server in GServer, but just stumbled on EventMachine.
Is EM better than my GServer implementation (i guess it is :))

class GameServer < GServer
def initialize(*args)
super(*args)
end

def serve(io)
log "Registering #{io} to Dispatcher."
dispatcher = Dispatcher.singleton
dispatcher.register io

loop do
begin
log "Reading from #{io}"
str = nil
Timeout.timeout(CONFIG['read_timeout']) { str = io.gets }
until str.nil?
str.strip!
dispatcher.receive io, JSON.load(str)

Timeout.timeout(CONFIG['read_timeout']) { str = io.gets }
end
rescue Timeout::Error
end

log "Dispatching outgoing messages..."
dispatcher.each_outgoing_for(io) do |message|
io.write JSON.dump(message)
io.write "\n"
io.flush
end

log "Sleeping..."
sleep CONFIG['sleep_timeout']
end
rescue JSON::ParserError
io.write "Unknown protocol, aborting.\n"
rescue Exception => e
log "Exception for #{io}: #{e.inspect}"
end
end
--
Posted via http://www.ruby-....

4 Answers

Arturas Šlajus

5/17/2009 10:34:00 PM

0

Hm, the EM implementation...

module GameServer
def post_init
puts "Registering #{self} to Dispatcher."
Dispatcher.singleton.register self
end

def receive_data(data)
Dispatcher.singleton.receive self, JSON.load(data) if data != ""
rescue JSON::ParserError
send_data "Unknown protocol, aborting.\n"
close_connection_after_writing
end

def send_message(message)
send_data "%s\n" % JSON.dump(message)
end

def unbind
Dispatcher.singleton.unregister self
end
end

So I guess EM handles loads better (as it's C) and is easier to use?
--
Posted via http://www.ruby-....

Chuck Remes

5/18/2009 11:43:00 AM

0


On May 17, 2009, at 5:34 PM, Art=C5=ABras =C5=A0lajus wrote:

> Hm, the EM implementation...
>
> module GameServer
> def post_init
> puts "Registering #{self} to Dispatcher."
> Dispatcher.singleton.register self
> end
>
> def receive_data(data)
> Dispatcher.singleton.receive self, JSON.load(data) if data !=3D ""
> rescue JSON::ParserError
> send_data "Unknown protocol, aborting.\n"
> close_connection_after_writing
> end
>
> def send_message(message)
> send_data "%s\n" % JSON.dump(message)
> end
>
> def unbind
> Dispatcher.singleton.unregister self
> end
> end
>
> So I guess EM handles loads better (as it's C) and is easier to use?

EM is certainly very fast but I don't know if it is easier to use. The =20=

documentation is hard to understand and the examples are all so =20
simplistic it is often difficult to see how to write a complex client =20=

or server.

I suggest downloading projects that have used EM (like Thin) and =20
learning good techniques from that code.

cr



Arturas Šlajus

5/18/2009 3:14:00 PM

0

Chuck Remes wrote:
> EM is certainly very fast but I don't know if it is easier to use. The
> documentation is hard to understand and the examples are all so
> simplistic it is often difficult to see how to write a complex client
> or server.
Well, it's good enough for me. I guess I'll stick to EM then :)
--
Posted via http://www.ruby-....

Francis Cianfrocca

5/18/2009 4:31:00 PM

0

On Mon, May 18, 2009 at 7:42 AM, Chuck Remes <cremes.devlist@mac.com> wrote=
:

>
> On May 17, 2009, at 5:34 PM, Art=FBras =D0lajus wrote:
>
> Hm, the EM implementation...
>>
>> module GameServer
>> def post_init
>> puts "Registering #{self} to Dispatcher."
>> Dispatcher.singleton.register self
>> end
>>
>> def receive_data(data)
>> Dispatcher.singleton.receive self, JSON.load(data) if data !=3D ""
>> rescue JSON::ParserError
>> send_data "Unknown protocol, aborting.\n"
>> close_connection_after_writing
>> end
>>
>> def send_message(message)
>> send_data "%s\n" % JSON.dump(message)
>> end
>>
>> def unbind
>> Dispatcher.singleton.unregister self
>> end
>> end
>>
>> So I guess EM handles loads better (as it's C) and is easier to use?
>>
>
> EM is certainly very fast but I don't know if it is easier to use. The
> documentation is hard to understand and the examples are all so simplisti=
c
> it is often difficult to see how to write a complex client or server.
>
> I suggest downloading projects that have used EM (like Thin) and learning
> good techniques from that code.
>
> cr
>
>
>
>
I'd be very open to hearing suggestions on how to improve the EM
documentation. Feel free to email me directly.