access variables inside a block ...

Discussion in 'Ruby' started by Karl-Heinz Wild, Oct 3, 2005.

  1. I'm playing with the class TcpServer from the
    package ldap-server.

    There is an interesting way for me of running a tcp server.

    -----------------------------

    t = GAME::Server.tcpserver:)port=>1110, :nodelay=>true) do
    print "+OK I am a fake POP3 server\r\n"
    while line = gets
    case line
    when /^quit/i
    break
    else
    print "-ERR I don't understand #{line}"
    end
    end
    print "+OK bye\r\n"
    end
    t.join
    end

    ----------------------------

    inside the GAME:Server.tcpserver it looks like

    ----------------------------

    def self.tcpserver(opt, &blk)
    server = TCPServer.new(opt[:bindaddr] || "0.0.0.0", opt[:port])
    server.listen(opt[:listen]) if opt[:listen]

    Thread.new do
    while true
    begin
    session = server.accept
    Thread.new(session) do |s|
    s.instance_eval(&blk)
    end
    rescue Interrupt
    server.close if server and not server.closed?
    break
    end
    end
    end
    end

    ------------------------------

    Now, how can I get access to the "session" variables.
    I need some information from TCP inside the block.
    But I couldn't find out how.

    Thanks for reading the mail and maybe an answer :)

    regards
    Karl-Heinz
    Karl-Heinz Wild, Oct 3, 2005
    #1
    1. Advertising

  2. Karl-Heinz Wild wrote:
    > I'm playing with the class TcpServer from the
    > package ldap-server.
    >
    > There is an interesting way for me of running a tcp server.
    >
    > -----------------------------
    >
    > t = GAME::Server.tcpserver:)port=>1110, :nodelay=>true) do
    > print "+OK I am a fake POP3 server\r\n"
    > while line = gets
    > case line
    > when /^quit/i
    > break
    > else
    > print "-ERR I don't understand #{line}"
    > end
    > end
    > print "+OK bye\r\n"
    > end
    > t.join
    > end
    >
    > ----------------------------
    >
    > inside the GAME:Server.tcpserver it looks like
    >
    > ----------------------------
    >
    > def self.tcpserver(opt, &blk)
    > server = TCPServer.new(opt[:bindaddr] || "0.0.0.0", opt[:port])
    > server.listen(opt[:listen]) if opt[:listen]
    >
    > Thread.new do
    > while true
    > begin
    > session = server.accept
    > Thread.new(session) do |s|
    > s.instance_eval(&blk)
    > end
    > rescue Interrupt
    > server.close if server and not server.closed?
    > break
    > end
    > end
    > end
    > end
    >
    > ------------------------------
    >
    > Now, how can I get access to the "session" variables.
    > I need some information from TCP inside the block.
    > But I couldn't find out how.
    >
    > Thanks for reading the mail and maybe an answer :)
    >
    > regards
    > Karl-Heinz


    t = GAME::Server.tcpserver:)port=>1110, :nodelay=>true) do
    session = self
    session....

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
    Joel VanderWerf, Oct 3, 2005
    #2
    1. Advertising

  3. On 04.10.2005, at 00:28, Joel VanderWerf wrote:

    > Karl-Heinz Wild wrote:
    >
    >> I'm playing with the class TcpServer from the
    >> package ldap-server.
    >>
    >> There is an interesting way for me of running a tcp server.
    >>
    >> -----------------------------
    >>
    >> t = GAME::Server.tcpserver:)port=>1110, :nodelay=>true) do
    >> print "+OK I am a fake POP3 server\r\n"
    >> while line = gets
    >> case line
    >> when /^quit/i
    >> break
    >> else
    >> print "-ERR I don't understand #{line}"
    >> end
    >> end
    >> print "+OK bye\r\n"
    >> end
    >> t.join
    >> end
    >>
    >> ----------------------------
    >>
    >> inside the GAME:Server.tcpserver it looks like
    >>
    >> ----------------------------
    >>
    >> def self.tcpserver(opt, &blk)
    >> server = TCPServer.new(opt[:bindaddr] || "0.0.0.0", opt[:port])
    >> server.listen(opt[:listen]) if opt[:listen]
    >>
    >> Thread.new do
    >> while true
    >> begin
    >> session = server.accept
    >> Thread.new(session) do |s|
    >> s.instance_eval(&blk)
    >> end
    >> rescue Interrupt
    >> server.close if server and not server.closed?
    >> break
    >> end
    >> end
    >> end
    >> end
    >>
    >> ------------------------------
    >>
    >> Now, how can I get access to the "session" variables.
    >> I need some information from TCP inside the block.
    >> But I couldn't find out how.

    >
    > t = GAME::Server.tcpserver:)port=>1110, :nodelay=>true) do
    > session = self
    > session....


    Great. It works.
    Thanks.
    Karl-Heinz Wild, Oct 4, 2005
    #3
  4. Karl-Heinz Wild <> wrote:
    > I'm playing with the class TcpServer from the
    > package ldap-server.
    >
    > There is an interesting way for me of running a tcp server.
    >
    > -----------------------------
    >
    > t = GAME::Server.tcpserver:)port=>1110, :nodelay=>true) do
    > print "+OK I am a fake POP3 server\r\n"
    > while line = gets
    > case line
    > when /^quit/i
    > break
    > else
    > print "-ERR I don't understand #{line}"
    > end
    > end
    > print "+OK bye\r\n"
    > end
    > t.join
    > end
    >
    > ----------------------------
    >
    > inside the GAME:Server.tcpserver it looks like
    >
    > ----------------------------
    >
    > def self.tcpserver(opt, &blk)
    > server = TCPServer.new(opt[:bindaddr] || "0.0.0.0", opt[:port])
    > server.listen(opt[:listen]) if opt[:listen]
    >
    > Thread.new do
    > while true
    > begin
    > session = server.accept
    > Thread.new(session) do |s|
    > s.instance_eval(&blk)
    > end
    > rescue Interrupt
    > server.close if server and not server.closed?
    > break
    > end
    > end
    > end
    > end
    >
    > ------------------------------
    >
    > Now, how can I get access to the "session" variables.
    > I need some information from TCP inside the block.
    > But I couldn't find out how.
    >
    > Thanks for reading the mail and maybe an answer :)
    >
    > regards
    > Karl-Heinz


    I'd prefer to hand over the session to the block as first parameter. That
    way you don't hide the self of the block's calling context (it might be
    necessary to access that) and you don't loose anything:

    def self.tcpserver(opt, &blk)
    server = TCPServer.new(opt[:bindaddr] || "0.0.0.0", opt[:port])
    server.listen(opt[:listen]) if opt[:listen]

    Thread.new do
    while true
    begin
    Thread.new(server.accept, &blk)
    rescue Interrupt
    server.close if server and not server.closed?
    break
    end
    end
    end
    end

    t = GAME::Server.tcpserver:)port=>1110, :nodelay=>true) do |session|
    session.print "+OK I am a fake POP3 server\r\n"
    while line = session.gets
    case line
    when /^quit/i
    break
    else
    session.print "-ERR I don't understand #{line}"
    end
    end
    session.print "+OK bye\r\n"
    end
    t.join
    end

    Btw, are you sure you want that t.join there? Looks like it was joining
    with its creator thread - doesn't sound like a good idea because typically
    the creator joins on all its children.

    Kind regards

    robert
    Robert Klemme, Oct 4, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Eshtry
    Replies:
    0
    Views:
    624
    Andy Eshtry
    Mar 1, 2004
  2. nail
    Replies:
    0
    Views:
    1,537
  3. nobody
    Replies:
    1
    Views:
    531
    Venkatesh
    Apr 26, 2006
  4. morrell
    Replies:
    1
    Views:
    946
    roy axenov
    Oct 10, 2006
  5. Jacob Rubynovice
    Replies:
    4
    Views:
    124
    Rick DeNatale
    May 30, 2008
Loading...

Share This Page