from threading import Thread, Lock from flask.ext.socketio import emit as bare_emit from bobby import sio, app, curry_emit from remote import fics_r, fics_w from logger import slog # , clog, log sock_lock = None emit = curry_emit(bare_emit)('message') # each one of the following corresponds to client event @sio.on('connect') def handle_connected(): slog('Client connected') emit('Connected to web socket server.\n') global sock_lock if sock_lock is None: sock_lock = Lock() reader = Thread(target=fics_r, args=(sock_lock,)); reader.daemon = True; reader.start() writer = Thread(target=fics_w, args=(sock_lock,)); writer.daemon = True; writer.start() slog('started reader and writer') @sio.on('command') def handle_command(command): slog('`{}\''.format(command['data'])) app.command_queue.put(command['data'])
import telnetlib from re import match from time import sleep from Queue import Empty from traceback import format_exc from collections import defaultdict from bobby import app, sio, curry_emit from config import FICS_HOST, FICS_PORT from logger import clog # , slog, log from bobby.servers.fics import PRELIMINARY_COMMANDS, s12_state, g1_state, game_state # EMIT = curry_emit(sio.emit)("message") EMIT_BOARD = curry_emit(sio.emit)("board-state") EMIT_GAME = curry_emit(sio.emit)("game-state") TN = None CONFIGURED = False TIMEOUT = 3 def fics_r(lock): def process_line(line): if match(r"<12>", line): return EMIT_BOARD, s12_state(line) elif match(r"<g1>", line): return EMIT_BOARD, g1_state(line) elif match(r"{Game", line): return EMIT_GAME, game_state(line) else: