[Xotcl] object method as (fileevent) callback

Vladimir vladimir.novikov.k at yandex.ru
Wed Feb 1 19:47:36 CET 2023


Sorry for ugly formatting. I do not know what is happened. Source of example in attachment.

От: Vladimir
Отправлено: 1 февраля 2023 г. в 21:44
Кому: xotcl at alice.wu.ac.at
Тема: object method as (fileevent) callback

Hello, I have a question.

It is a simple Server example TclOO implementaiton below. I mark with yellow two lines, where I can use mymethod command to set private methods of class as callbacks. Both of this callbacks execute in class instance context.
Is it possible do same thing with nx? I want incapsulate server logic inside nx::Class and set private method as callback to accept connection and reading data.

#!/usr/bin/env tclsh

package require oo::util ;# for 'mymethod' command
package require logger

logger::initNamespace proj::Server debug

oo::class create proj::mock::Server {

    constructor { port } {
        my variable projVersion_
        my variable port_
        my variable socket_

        set projVersion_ "0.1"
        set port_ $port

        ::logger::tree::proj::Server::debug "Init mock Server $projVersion_"

        # it works!
        set socket_ [socket -server [mymethod Accept_clb] $port]
    } ;# constructor

    method Accept_clb { sock addr port } {
        my variable clients_

        ::logger::tree::proj::Server::debug "Accepting connection with $addr $port"
        set clients_(addr,$sock) [list $addr $port]

        fconfigure $sock -translation binary -buffering none
        fileevent $sock readable [mymethod Echo_clb $sock] ;# it works too!
        ::logger::tree::proj::Server::debug "Setting callback private method"
    } ;# Accept_clb

    method Echo_clb { sock } {
        my variable clients_

        ::logger::tree::proj::Server::debug "Readable callback call"

        if { [eof $sock] } {
            ::logger::tree::proj::Server::debug "No more data in socket"

            close $sock
            unset clients_(addr,$sock)
        } else {
            set binary_msg [read $sock 2]
            binary scan $binary_msg "H*" string_msg

            ::logger::tree::proj::Server::debug "Readed 2 bytes"

            if [string length $string_msg] {
                ::logger::tree::proj::Server::debug "Sending 2 bytes left"

                puts -nonewline $sock $binary_msg
            }
        }
    } ;# Echo_clb

    destructor {
        my variable socket_
        close $socket_

        ::logger::tree::proj::Server::debug "Destroing Server"
    } ;# destructor

} ;# dms::mock::server

set s [proj::mock::Server new 1950]

vwait forever






More information about the Xotcl mailing list