basilisp icon indicating copy to clipboard operation
basilisp copied to clipboard

Proxies

Open chrisrink10 opened this issue 5 years ago • 2 comments

Clojure.org's guide on Java Interop discusses proxies

construct-proxy
get-proxy-class
init-proxy
proxy
proxy-call-with-super
proxy-mappings
proxy-name
proxy-super
update-proxy

The following were handled in #550:

reify

chrisrink10 avatar Jul 28 '19 19:07 chrisrink10

Hi,

Any suggestion how to implement MyTCPHandler below in basilisp in the absence of proxy? i.e. how to override class methods in basilisp

https://docs.python.org/3/library/socketserver.html#socketserver-tcpserver-example

import socketserver

class MyTCPHandler(socketserver.BaseRequestHandler):
    """
    The request handler class for our server.

    It is instantiated once per connection to the server, and must
    override the handle() method to implement communication to the
    client.
    """

    def handle(self):
        # self.request is the TCP socket connected to the client
        self.data = self.request.recv(1024).strip()
        print("{} wrote:".format(self.client_address[0]))
        print(self.data)
        # just send back the same data, but upper-cased
        self.request.sendall(self.data.upper())

if __name__ == "__main__":
    HOST, PORT = "localhost", 9999

    # Create the server, binding to localhost on port 9999
    with socketserver.TCPServer((HOST, PORT), MyTCPHandler) as server:
        # Activate the server; this will keep running until you
        # interrupt the program with Ctrl-C
        server.serve_forever()

With proxy available it would have looked something like I guess

(proxy [socketserver/BaseRequestHandler]
     (handle
       ([self] (println :hi))))

Thanks

ikappaki avatar Aug 19 '23 10:08 ikappaki

Using python/type seems to have done the trick

(let [handler (python/type (name (gensym "mytcphandler")) (python/tuple [socketserver/BaseRequestHandler])
                             #py {"handle" #(println :connected %)})
        server (socketserver/TCPServer (python/tuple ["127.0.0.1" 0]) handler)]
    (println :addr (.-server_address server))
    (.serve_forever server))

ikappaki avatar Aug 19 '23 21:08 ikappaki