For variables that need to be defined outside of the module/class that is called as an argument to start_server, is there any other way to pass objects to that class that were created before calling start_server, or should I just use global variables? For example the object that holds the database pool, or other variables that won''t change for the life of the server, but need to be instantiated only once. Chris
On 8/23/06, snacktime <snacktime at gmail.com> wrote:> > For variables that need to be defined outside of the module/class that > is called as an argument to start_server, is there any other way to > pass objects to that class that were created before calling > start_server, or should I just use global variables? For example the > object that holds the database pool, or other variables that won''t > change for the life of the server, but need to be instantiated only > once.Yes. You can use a class instead of a module to handle your protocol requests. Using modules is preferred in general because it''s so simple, there''s almost nothing to do. Something like this should solve it for you, and there is more information about this in the Rdoc. class YourClass < EventMachine::Connection # here, add in all the normal methods you would have put in a module end EventMachine.run { EventMachine.start_server( addr, port, YourClass ) {|newconnection| # newconnection is an instance of YourClass, and this block is # called inside its constructor. Use instance_eval to add variables # to it. } Is that closer to what you wanted? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20060823/0585024b/attachment.html
> > class YourClass < EventMachine::Connection > # here, add in all the normal methods you would have put in a module > end > > EventMachine.run { > EventMachine.start_server( addr, port, YourClass ) {|newconnection| > # newconnection is an instance of YourClass, and this block is > # called inside its constructor. Use instance_eval to add variables > # to it. > } >Ya that''s it, I was already using a class instead of a module, but I didn''t see how to pass an instance of the class instead of the class name. Chris
I''m missing something there. How do I set an instance or class variable via instance_eval? If I do something like the following @dbh and @@dbh are both nil. class YourClass < EventMachine::Connection @@ dbh = nil def initialize *args super end end def receive_data p @dbh p @@ dbh end end EventMachine.run { EventMachine.start_server( addr, port, YourClass ) {|newconnection| newconnection.instance_eval {@dbh = ''test'';@@dbh=''test''} }
I don''t think the block is being run. If I just put a print statement in it I see nothing.
Checking..... On 8/23/06, snacktime <snacktime at gmail.com> wrote:> I don''t think the block is being run. If I just put a print statement > in it I see nothing. > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk >
Doesn''t look like start_server takes a block when providing a class as the handler. def EventMachine::start_server server, port, handler=nil klass = if (handler and handler.is_a?(Class)) handler else Class.new( Connection ) {handler and include handler} end s = start_tcp_server server, port @acceptors[s] = klass end
On 8/23/06, snacktime <snacktime at gmail.com> wrote:> Doesn''t look like start_server takes a block when providing a class as > the handler. > > def EventMachine::start_server server, port, handler=nil > klass = if (handler and handler.is_a?(Class)) > handler > else > Class.new( Connection ) {handler and include handler} > end > > s = start_tcp_server server, port > @acceptors[s] = klass > end > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk >You''re right. Just fixed it, please sync to the latest and try it. Nice catch.