def on_connect(self, con): area = self.create_area(self.addr) area.bind('<Destroy>', lambda event: send_cmd(con, 'QUIT :vy rules!'), add=True) Stdin(con) Stdout(con) Terminator(con) Irc(con) self.misc = Misc(con) xmap(con, CLOSE, lambda con, err: lose(con)) xmap(con, '*JOIN', self.create_channel) xmap( con, Terminator.FOUND, lambda con, data: area.append('%s\n' % data.decode(self.encoding))) xmap(con, 'PMSG', self.e_pmsg) xmap(con, '376', lambda con, *args: send_cmd(con, self.irccmd)) xmap(con, '376', self.auto_join) xmap( con, 'PING', lambda con, prefix, servaddr: send_cmd(con, 'PONG :%s' % servaddr)) send_cmd(con, 'NICK %s' % self.nick) send_cmd(con, 'USER %s' % self.user)
def send_ident(self, con): # Basic untwisted protocols required by fics protocol. Stdin(self.con) Stdout(self.con) Terminator(self.con, '\n\r') # Finally we install fics protocol. fics.install(self.con) # If it happens of the server closing # the connection then we just close the socket # and destroy it. self.con.add_map(CLOSE, lambda con, err: lose(con)) # Whenever it comes data we print it on our console. self.con.add_map(Terminator.FOUND, self.update_text) # The '<12>' is an event issued by fics protocol # it means you are either playing a game or examining a # game. In both case we need to update the state of the # board. self.con.add_map('<12>', self.update_state) # It waits for the user sending login # when the session starts we can send style 12. self.username, = yield con, fics.START_SESSION self.con.dump('set style 12\r\n')
def create_client_ssl(addr, port): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) context = ssl.create_default_context() wrap = context.wrap_socket(sock, do_handshake_on_connect=False, server_hostname=addr) # First attempt to connect otherwise it leaves # an unconnected spin instance in the reactor. wrap.connect_ex((addr, port)) con = SSL(wrap) ClientSSL(con) con.add_map(SSL_CONNECT, install_ssl_handles) con.add_map(SSL_CONNECT_ERR, lambda con, err: lose(con)) con.add_map(CONNECT_ERR, lambda con, err: lose(con)) con.add_map(SSL_CERTIFICATE_ERR, lambda con, err: lose(con)) return con
def __init__(self, spin): self.timer = Timer(PersistentConnection.TIMEOUT, lambda: lose(spin)) self.count = 0 xmap(spin, TmpFile.DONE, self.process) xmap(spin, DUMPED, self.install_timeout) xmap(spin, TransferHandle.DONE, lambda spin, request, data: self.timer.cancel()) spin.add_header(('connection', 'keep-alive')) spin.add_header(('keep-alive', 'timeout=%s, max=%s' % ( PersistentConnection.TIMEOUT, PersistentConnection.MAX)))
def __init__(self, spin): self.timer = Timer(PersistentConnection.TIMEOUT, lambda: lose(spin)) self.count = 0 xmap(spin, TmpFile.DONE, self.process) xmap(spin, DUMPED, self.install_timeout) xmap(spin, TransferHandle.DONE, lambda spin, request, data: self.timer.cancel()) spin.add_header(('connection', 'keep-alive')) spin.add_header( ('keep-alive', 'timeout=%s, max=%s' % (PersistentConnection.TIMEOUT, PersistentConnection.MAX)))
def handle_accept(self, local, spin): Stdin(spin) Stdout(spin) AccUntil(spin) TransferHandle(spin) RequestHandle(spin) MethodHandle(spin) # must be improved. Locate(spin) # InvalidRequest(client) xmap(spin, CLOSE, lambda con, err: lose(con))
def locate(self, spin, request): path = join(spin.app.app_dir, spin.app.static_dir, basename(request.path)) if not isfile(path): return # Where we are going to serve files. # I might spawn an event like FILE_NOT_FOUND. # So, users could use it to send appropriate answers. type_file, encoding = guess_type(path) default_type = 'application/octet-stream' spin.add_header(('Content-Type', type_file if type_file else default_type), ('Content-Length', getsize(path))) spin.send_headers() xmap(spin, OPEN_FILE_ERR, lambda con, err: lose(con)) drop(spin, path)
def plug(self): # If it is connected then we have to unplug. if self.con: self.unplug() host, port = self.ask_address() # We create our connection socket. sock = socket(AF_INET, SOCK_STREAM) # It wraps the socket so we can install protocols into it. self.con = Spin(sock) Client(self.con) # It maps CONNECT to send_ident. self.con.add_map(CONNECT, self.send_ident) self.con.add_map(CONNECT_ERR, lambda con, err: self.update_text(con, 'Connection failed.')) self.con.add_map(CONNECT_ERR, lambda con, err: lose(con)) self.con.connect_ex((host, port))
def locate(self, spin, request): path = join(spin.app.app_dir, spin.app.static_dir, basename(request.path)) if not isfile(path): return # Where we are going to serve files. # I might spawn an event like FILE_NOT_FOUND. # So, users could use it to send appropriate answers. type_file, encoding = guess_type(path) default_type = 'application/octet-stream' spin.add_header( ('Content-Type', type_file if type_file else default_type), ('Content-Length', getsize(path))) spin.send_headers() xmap(spin, OPEN_FILE_ERR, lambda con, err: lose(con)) drop(spin, path)
def __init__(self, spin): xmap(spin, DUMPED, lambda con: lose(con))
def on_connect(con, host): StdinSSL(con) StdoutSSL(con) con.dump("GET / HTTP/1.0\r\nHost: %s\r\n\r\n" % host) xmap(con, LOAD, put) xmap(con, CLOSE, lambda con, err: lose(con))
def install_ssl_handles(con): StdinSSL(con) StdoutSSL(con) con.add_map(CLOSE, lambda con, err: lose(con))
def setup(server, con): Stdin(con) Stdout(con) xmap(con, CLOSE, lambda con, err: lose(con)) xmap(con, LOAD, lambda con, data: sys.stdout.write('%s\r\n' % data))
def install_timeout(self, spin): self.timer = Timer(PersistentConnection.TIMEOUT, lambda: lose(spin))