Beispiel #1
0
    def __init__(self, name, ip, port):
        #msg = dict()
        #msg['op'] = 'OPEN'
        #msg['docfn'] = name
        #msg = json.dumps(msg)

        #sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        #sk.connect((ADDR, PORT))

        #msgr.safe_send(sk, msg)
        #res = json.loads(msgr.safe_recv(sk))

        #sk.close()

        self.sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sk.connect((ip, port))

        msg = json.loads(msgr.safe_recv(self.sk))

        self.pid = msg['pid']
        self.revision = msg['rev']
        self.initial_state = msgr.safe_recv(self.sk)

        client_path = "/Users/gio/projects/ot_vim/python/client"
        self.engine = sp.Popen(
            [client_path, str(self.pid),
             str(self.revision)],
            stdin=sp.PIPE,
            stdout=sp.PIPE)

        return
Beispiel #2
0
    def __init__(self, name, ip, port):
        #msg = dict()
        #msg['op'] = 'OPEN'
        #msg['docfn'] = name
        #msg = json.dumps(msg)

        #sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        #sk.connect((ADDR, PORT))

        #msgr.safe_send(sk, msg)
        #res = json.loads(msgr.safe_recv(sk))

        #sk.close()

        self.sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sk.connect((ip, port))

        msg = json.loads(msgr.safe_recv(self.sk))

        self.pid = msg['pid']
        self.revision = msg['rev']
        self.docbuf = list(map(ord, list(msgr.safe_recv(self.sk))))

        self.engine = sp.Popen(
            ['./client', str(self.pid),
             str(self.revision)],
            stdin=sp.PIPE,
            stdout=sp.PIPE)

        return
Beispiel #3
0
def main():
    revision = 0
    docbuf = []

    # start engine
    engine = sp.Popen(['./server'], stdin=sp.PIPE, stdout=sp.PIPE)

    lsk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    lsk.bind(('', 4444))
    lsk.listen(10)

    sockets = [lsk]

    while True:
        rlist, _, _ = select.select(sockets, [], [])

        ct = 0

        for rsk in rlist:
            if rsk == lsk:
                conn, addr = lsk.accept()

                msg = dict()
                msg['rev'] = revision
                msg['pid'] = next_pid()

                msgr.safe_send(conn, json.dumps(msg))
                msgr.safe_send(conn, ''.join(map(chr, docbuf)))

                sockets.append(conn)
            else:
                packet = msgr.safe_recv(rsk)
                if 0 == len(packet):
                    sockets.remove(rsk)
                else:
                    msg = json.loads(packet)
                    buf = '{},{},{},{},{}\n'.format(msg['pid'], msg['rev'],
                                                msg['type'], msg['c'], msg['pos']).encode()
                    engine.stdin.write(buf)
                    engine.stdin.flush()

                    ct += 1

        for i in range(ct):
            revision += 1

            data = engine.stdout.readline().decode()
            msg = line2msg(data)
            docbuf = op_perform(docbuf, msg)

            for sk in sockets[1:]:
                msgr.safe_send(sk, json.dumps(msg))

        print(''.join(map(chr, docbuf)))
Beispiel #4
0
    def recv_ops(self):
        ops = []
        messages = []

        while True:
            rlist = [self.sk]
            rlist, _, _ = select.select(rlist, [], [], 0.001)
            if 0 == len(rlist):
                break
            messages.append(json.loads(msgr.safe_recv(self.sk)))

        for msg in messages:
            buf = '{},{},{},{},{}\n'.format(msg['pid'], msg['rev'],
                                            msg['type'], msg['c'],
                                            msg['pos']).encode()
            self.engine.stdin.write(buf)
        self.engine.stdin.flush()
        messages.clear()

        while True:
            rlist = [self.engine.stdout]
            rlist, _, _ = select.select(rlist, [], [], 0.001)
            if 0 == len(rlist):
                break
            data = self.engine.stdout.readline().decode().split(',')
            msg = dict()
            msg['pid'] = int(data[0])
            msg['rev'] = int(data[1])
            msg['type'] = int(data[2])
            msg['c'] = int(data[3])
            msg['pos'] = int(data[4])

            if -1 == msg['pid']:
                ops.append((msg['type'], msg['c'], msg['pos']))
                self.op_perform(msg)
            else:
                messages.append(msg)

        for msg in messages:
            msgr.safe_send(self.sk, json.dumps(msg))

        return ops