From: Alan Schwarz Date: 2012-07-14T14:04:37+09:00 Subject: Re: Need some help to implement something in eventmachine. --f46d044481d914001904c4c322fe Content-Type: text/plain; charset=ISO-8859-1 Hi, I have the happy circumstance of having used EventMachine recently and I think I can answer your question below. On Thu, Jul 12, 2012 at 2:37 PM, Eliezer Croitoru wrote: > I wrote my server in pure eventmachine source at: > https://github.com/elico/**echelon > > i am struggling to understand something. > in eventmachine i have "receive_data" method with a "data" variable and > the send_data(data) method. > > i have implemented the server logic as : > ## > def post_init > cleanup_all_data > end > > def receive_data(data) > #data parsing code > send_data(response) > cleanup_all_data > end > ## > > but do i really need to cleanup data? if it's a tcp socket and non > persistent connection then it's a dedicated connection and when it's closed > all the data is dead? or it's an endless loop i need to cleanup the data > for the next connection? > > post_init is used to initialize variables after instantiating the server, so all the instance variables will not point to nil. Secondly, in the EventMachine rdoc describing receive_data, EventMachine *knows absolutely nothing about the protocol which your code implements*. What it means is that you must define how receive_data will separate the data stream because it otherwise will not know where the stream ends. Notice that the code is using pos =~ /\r\n\r\n/ followed by @icap_header[:data] = @data[0..pos+1] to cap the stream and then calling cleanup to start over for a new stream. This all happens on the same connection. A new connection would use a new server instance with its own instance variables. You could try instead using the BufferedTokenizer class, which will split the received data into an array of tokenized entities using a delimiter (the default is "\n", but in this case you would use "\r\n\r\n"). You can then iterate through the array using the each method, passing your code as a block. Lastly, there is a forum for EventMachine at https://groups.google.com/forum/?fromgroups#!forum/eventmachine > Thanks, > Eliezer > > > -- > Eliezer Croitoru > https://www1.ngtech.co.il > IT consulting for Nonprofit organizations > eliezer ngtech.co.il > > > Alan --f46d044481d914001904c4c322fe Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hi,

I have the happy circumstance of having used EventMachine recent= ly and I think I can answer your question below.

On Thu, Jul 12, 2012 at 2:37 PM, Eliezer Croitoru <eliezer@ngte= ch.co.il> wrote:
I wrote my server in pure eventmachine sourc= e at: https:= //github.com/elico/echelon

i am struggling to understand something.
in eventmachine i have "receive_data" method with a "data&qu= ot; variable and the send_data(data) method.

i have implemented the server logic as :
##
def post_init
=A0 =A0 =A0 =A0 cleanup_all_data
end

def receive_data(data)
=A0 =A0 =A0 =A0 #data parsing code
=A0 =A0 =A0 =A0 send_data(response)
=A0 =A0 =A0 =A0 cleanup_all_data
end
##

but do i really need to cleanup data? if it's a tcp socket and non pers= istent connection then it's a dedicated connection and when it's cl= osed all the data is dead? or it's an endless loop i need to cleanup th= e data for the next connection?


post_init is used to initialize variables after i= nstantiating the server, so all the instance variables will not point to ni= l.

Secondly, in the EventMachine rdoc describing receive_data, Event= Machine *knows absolutely nothing about the protocol which your code implem= ents*. What it means is that you must define how receive_data will separate= the data stream because it otherwise will not know where the stream ends. = Notice that the code is using pos =3D~ /\r\n\r\n/ followed by @icap_header[= :data] =3D @data[0..pos+1] to cap the stream and then calling cleanup to st= art over for a new stream.

This all happens on the same connection. A new connection would use a n= ew server instance with its own instance variables.

You could try in= stead using the BufferedTokenizer class, which will split the received data= into an array of tokenized entities using a delimiter (the default is &quo= t;\n", but in this case you would use "\r\n\r\n"). You can t= hen iterate through the array using the each method, passing your code as a= block.

Lastly, there is a forum for EventMachine at https://groups.google.com= /forum/?fromgroups#!forum/eventmachine
=A0
Thanks,
Eliezer


--
Eliezer Croitoru
https://www1.ngtech= .co.il
IT consulting for Nonprofit organizations
eliezer <at> ngtech= .co.il


Alan
--f46d044481d914001904c4c322fe--