Beispiel #1
0
def socket_watcher():
    global shutting_down
    _linda_server.serve(getOptions().use_domain, getOptions().port)
    for s in _linda_server.serverSockets():
        if s > 0:
            sockets.append(Connection(s))
            sockets[-1].type = "bound"

    Handler = server.LindaConnection()
    while True:
        socket_lock.acquire()
        try:
            iwtd, owtd, ewtd = select.select(sockets, [], sockets, 1)
        finally:
            socket_lock.release()

        if close:
            break
        elif len(iwtd) == 0 and len(owtd) == 0 and len(ewtd) == 0:
            continue

        for s in iwtd:
            if s.type == "bound":
                 ns = _linda_server.accept(s)
                 if ns == -1:
                    socket_lock.acquire()
                    try:
                        del sockets[sockets.index(s)]
                    finally:
                        socket_lock.release()
                    sys.stderr.write("Error accepting connection on %s\n" % (s, ))
                 else:
                    ns = Connection(ns)
                    ns.type = "CLIENT"
                    sockets.append(ns)
                 continue
            m = _linda_server.recv(s.fileno())
            if m is None:
                if s.type == "MONITOR":
                    if shutting_down:
                        continue
                    print "Server shutting down."
                    shutting_down = True
                    thread_pool.giveJob(target=server.cleanShutdown, args=())
                    continue
                elif s.type == "SERVER":
                    server.removeServer(s.name)
                    connect_lock.acquire()
                    try:
                        del neighbours[str(s.name)]
                    finally:
                        connect_lock.release()
                elif s.type == "CLIENT":
                    server.removeProcess(s.name)
                socket_lock.acquire()
                try:
                    del sockets[sockets.index(s)]
                    continue
                finally:
                    socket_lock.release()
            else:
                msgid, msg = m[0], m[1:]
                if msgid is None:
                    thread_pool.giveJob(target=Handler.handle, args=(s, None, msg))
                elif str(msgid[0]) == server.node_id and str(msgid[1]) == server.node_id:
                    ms_lock.acquire()
                    try:
                        try:
                            message_store[int(msgid[2])] = (msgid, msg)
                        except KeyError:
                            raise KeyError, "%s not found in %s" % (msgid, return_locks.keys())
                        try:
                            return_locks[int(msgid[2])].release()
                        except KeyError:
                            pass # we have recieved the message before the return lock has been created!
                    finally:
                        ms_lock.release()
                elif str(msgid[0]) != server.node_id:
                    sendMessageToNode(msgid[0], msgid, *msg)
                elif str(msgid[0]) == server.node_id:
                    thread_pool.giveJob(target=Handler.handle, args=(s, msgid, msg))
                else:
                    raise SystemError
Beispiel #2
0
def socket_watcher():
    global shutting_down
    _linda_server.serve(getOptions().use_domain, getOptions().port)
    for s in _linda_server.serverSockets():
        if s > 0:
            sockets.append(Connection(s))
            sockets[-1].type = "bound"

    Handler = server.LindaConnection()
    while True:
        socket_lock.acquire()
        try:
            iwtd, owtd, ewtd = select.select(sockets, [], sockets, 1)
        finally:
            socket_lock.release()

        if close:
            break
        elif len(iwtd) == 0 and len(owtd) == 0 and len(ewtd) == 0:
            continue

        for s in iwtd:
            if s.type == "bound":
                ns = _linda_server.accept(s)
                if ns == -1:
                    socket_lock.acquire()
                    try:
                        del sockets[sockets.index(s)]
                    finally:
                        socket_lock.release()
                    sys.stderr.write("Error accepting connection on %s\n" %
                                     (s, ))
                else:
                    ns = Connection(ns)
                    ns.type = "CLIENT"
                    sockets.append(ns)
                continue
            m = _linda_server.recv(s.fileno())
            if m is None:
                if s.type == "MONITOR":
                    if shutting_down:
                        continue
                    print "Server shutting down."
                    shutting_down = True
                    thread_pool.giveJob(target=server.cleanShutdown, args=())
                    continue
                elif s.type == "SERVER":
                    server.removeServer(s.name)
                    connect_lock.acquire()
                    try:
                        del neighbours[str(s.name)]
                    finally:
                        connect_lock.release()
                elif s.type == "CLIENT":
                    server.removeProcess(s.name)
                socket_lock.acquire()
                try:
                    del sockets[sockets.index(s)]
                    continue
                finally:
                    socket_lock.release()
            else:
                msgid, msg = m[0], m[1:]
                if msgid is None:
                    thread_pool.giveJob(target=Handler.handle,
                                        args=(s, None, msg))
                elif str(msgid[0]) == server.node_id and str(
                        msgid[1]) == server.node_id:
                    ms_lock.acquire()
                    try:
                        try:
                            message_store[int(msgid[2])] = (msgid, msg)
                        except KeyError:
                            raise KeyError, "%s not found in %s" % (
                                msgid, return_locks.keys())
                        try:
                            return_locks[int(msgid[2])].release()
                        except KeyError:
                            pass  # we have recieved the message before the return lock has been created!
                    finally:
                        ms_lock.release()
                elif str(msgid[0]) != server.node_id:
                    sendMessageToNode(msgid[0], msgid, *msg)
                elif str(msgid[0]) == server.node_id:
                    thread_pool.giveJob(target=Handler.handle,
                                        args=(s, msgid, msg))
                else:
                    raise SystemError
Beispiel #3
0
#!/usr/bin/python

import sys

import _linda_server

if __name__ == "__main__":
    if not _linda_server.serve():
        sys.exit(-1)

    sd = _linda_server.accept()

    m = _linda_server.recv(sd)
    print m
    if m[0] == "OUT":
        _linda_server.send(sd, ("DONE", ))

    _linda_server.server_disconnect()