예제 #1
0
def exitWithServerMessage(userSelf):
    message_send = protocol_message(protocol_message.TYPE_CLIENT_EXIT,
                                    userSelf.userID, 0, str(0))
    server.send(message_send.collapsed())
    shutdown_client("Quitting...goodbye!")
예제 #2
0
def main(stdscr):
    global z
    z = 0  #DEBUG
    global maxColorNum
    port = getPort()

    #server setup
    global server
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    offline = False
    cursesInit()
    stdscr.nodelay(True)
    clientName = startScreen(stdscr)
    #board setup
    Height = 5
    Width = 10
    canvas = board(Width, Height)

    #user and userlist setup
    myUserList = userList()
    userSelf = user()
    userSelf.updateUsername(clientName)

    try:
        server.connect(('127.0.0.1', port))
    except (socket.error, OverflowError):
        exitmsg = "Unable to connect to port " + str(port) + "...quitting."
        shutdown_client(exitmsg)

    #send new user message, sends the username and 0 as the id
    dataSend = protocol_message(protocol_message.TYPE_USER_JOIN, 0,
                                len(clientName), clientName)
    server.send(dataSend.collapsed())

    while True:
        try:
            rlist, wlist, xlist = select.select([server], [], [], 0)
        except (socket.error, KeyboardInterrupt):
            exitmsg = "Select failed...quitting."
            shutdown_client(exitmsg)
        for item in rlist:
            if item == server:
                try:
                    dataRec = server.recv(1024)
                except socket.error as msg:
                    exitmsg = "Connection reset by peer...quitting."
                    shutdown_client(exitmsg)

                message_rec = protocol_message.message_from_collapsed(dataRec)

                if (message_rec.type == protocol_message.TYPE_WELCOME_NEW):
                    #populate the userlist with information from the server
                    myUserList.stringToUserList(message_rec.message)
                    userSelf = userInitialization(clientName, myUserList,
                                                  canvas)
                    printBoardClient(canvas, myUserList, userSelf, stdscr)

                if (message_rec.type == protocol_message.TYPE_WELCOME_BACK):
                    #populate the userlist with information from the server
                    myUserList.stringToUserList(message_rec.message)
                    userSelf = userInitialization(clientName, myUserList,
                                                  canvas)
                    printBoardClient(canvas, myUserList, userSelf, stdscr)

                if (message_rec.type == protocol_message.TYPE_SERVER_UPDATE_POS
                    ):
                    if (not offline):
                        myUserList.stringToUserList(message_rec.message)
                        printBoardClient(canvas, myUserList, userSelf, stdscr)

        #get user input through the keyboard
        key = getInput(stdscr, userSelf)

        if (key == ord("w") or key == ord("s") or key == ord("a")
                or key == ord("d") or key == curses.KEY_UP
                or key == curses.KEY_DOWN or key == curses.KEY_LEFT
                or key == curses.KEY_RIGHT):
            #update our position and send it
            newPos = posDelta(key)
            canvas.moveUser(newPos, userSelf)
            userSelf.pos = canvas.userPosition
            posToSend = userSelf.toString()
            if (not offline):
                message_send = protocol_message(
                    protocol_message.TYPE_CLIENT_UPDATE_POS, userSelf.userID,
                    len(posToSend), posToSend)
                server.send(message_send.collapsed())
            else:
                myUserList.updateUserPosition(userSelf, userSelf.pos.x,
                                              userSelf.pos.y)
                printBoardClient(canvas, myUserList, userSelf, stdscr)
        if (key == ord("o")):
            offline = not offline
예제 #3
0
def main():
    port = getPort()
    #socket setup
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind(('', port))
    server.listen(5)

    Height = 5
    Width = 10
    masterBoard = board(Width, Height)
    masterBoard.addUserServer()
    recvBoard = board(Width, Height)
    clients = []
    message_id = 0

    client_info_list = []
    num_users = 0

    while True:
        Connections, wlist, xlist = select.select([server], [], [], 0.05)

        for Connection in Connections:
            client, Informations = Connection.accept()
            clients.append(client)
            client_info_list.append({'user_id': num_users, 'connection': client, 'connected': True})
            num_users += 1
            print clients
            # Test peer to peer mode init on every new client
            print "P2P"
            p2p_mode(client_info_list)

        clientsList = []
        try:
            clientsList, wlist, xlist = select.select(clients, [], [], 0.05)
        except select.error:
            pass
        else:
            for clientInList in clientsList:
                dataRec = clientInList.recv(1024)
                #print "Got: "
                #print dataRec
                message_rec = protocol_message.message_from_collapsed(dataRec)
                if(message_rec.type == protocol_message.TYPE_NEW_USER):
                    user_id_index = next(index for (index, d) in enumerate(client_info_list) if d['connection'] == clientInList)
                    print "User id:"
                    print client_info_list[user_id_index]['user_id']
                    dataSend = protocol_message.construct_welcome_message_data(client_info_list[user_id_index]['user_id'])
                    message_send = protocol_message(protocol_message.TYPE_WELCOME, len(dataSend), dataSend)
                    clientInList.send(message_send.collapsed())

                    masterBoard.addUser(client_info_list[user_id_index]['user_id'])

                if(message_rec.type == protocol_message.TYPE_CUR_BOARD):
                    curBoardState = protocol_message(protocol_message.TYPE_CUR_BOARD, len(masterBoard.boardToString()), masterBoard.boardToString())
                    clientInList.send(curBoardState.collapsed())
                    

                if(message_rec.type == protocol_message.TYPE_UPDATE_BOARD):
                    print("Got update board")
                    print(message_rec.message)
                    recvBoard.stringToBoard(message_rec.message)
                    recvBoard.findMyUser()
                    masterBoard.mergeBoards(recvBoard)
                    #send the board back
                    dataSend = masterBoard.boardToString()
                    message_send = protocol_message(protocol_message.TYPE_UPDATE_BOARD, len(dataSend), dataSend)
                    notify_all_clients(clients, message_send)
                if(message_rec.type == protocol_message.SENTINEL):
                    clients.remove(clientInList)
                    # TODO add this client to a waiting to be reconnected list
    clientInList.close()
    server.close()
예제 #4
0
def main():
    port = getPort()
    #socket setup
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        server.bind(('', port))
    except socket.error:
        print "Address already in use...quitting"
        exit()
    server.listen(5)

    clients = []
    message_id = 0

    client_info_list = []
    masterClientList = userList()
    offlineClientList = userList()
    num_users = 0

    while True:
        try:
            Connections, wlist, xlist = select.select([server], [], [], 0.05)
        except (select.error, KeyboardInterrupt):
            print "\nQuitting..."
            exit()

        for Connection in Connections:
            client, Informations = Connection.accept()
            clients.append(client)

        clientsList = []
        try:
            clientsList, wlist, xlist = select.select(clients, [], [], 0.05)
        except (select.error, KeyboardInterrupt):
            print "\nQuitting..."
            exit()
        else:
            for clientInList in clientsList:
                dataRec = clientInList.recv(1024)
                message_rec = protocol_message.message_from_collapsed(dataRec)
                if (message_rec.type == protocol_message.TYPE_USER_JOIN):

                    username = message_rec.message
                    print "user " + username + " joined"
                    #have we seen this client before
                    if (offlineClientList.userExists(username)):
                        print "welcome back " + username + " with ID " + str(
                            masterClientList.getUserNum(username))
                        masterClientList.addOrUpdateUser(
                            offlineClientList.getUserByName(username))
                        offlineClientList.removeUser(username)
                        dataSend = masterClientList.listToString()
                        print "master list: " + dataSend
                        message_send = protocol_message(
                            protocol_message.TYPE_WELCOME_BACK,
                            protocol_message.SERVER, len(dataSend), dataSend)
                    else:
                        print "never before seen client " + username + "...assigned ID " + str(
                            num_users)
                        #add new user to the list and associate their name with a user num
                        masterClientList.addUserDefault(username, num_users)
                        num_users += 1
                        dataSend = masterClientList.listToString()
                        print "master list: " + dataSend
                        message_send = protocol_message(
                            protocol_message.TYPE_WELCOME_NEW,
                            protocol_message.SERVER, len(dataSend), dataSend)

                    clientInList.send(message_send.collapsed())

                if (message_rec.type == protocol_message.TYPE_CLIENT_UPDATE_POS
                    ):
                    #a client updated their position
                    msgFromUser = user()
                    msgFromUser.fromString(message_rec.message)
                    masterClientList.addOrUpdateUser(msgFromUser)
                    #now send the updated client list out
                    dataSend = masterClientList.listToString()
                    print "master list: " + dataSend
                    message_send = protocol_message(
                        protocol_message.TYPE_SERVER_UPDATE_POS,
                        protocol_message.SERVER, len(dataSend), dataSend)
                    notify_all_clients(clients, message_send)

                if (message_rec.type == protocol_message.SENTINEL):
                    print "Client left"
                    clients.remove(clientInList)

                if (message_rec.type == protocol_message.TYPE_CLIENT_EXIT):
                    tempusr = user()
                    tempusr = masterClientList.getUserByID(message_rec.user)
                    print "Client " + tempusr.name + " left gracefully"
                    offlineClientList.addOrUpdateUser(tempusr)
                    masterClientList.removeUser(tempusr.name)
                    clients.remove(clientInList)

    clientInList.close()
    server.close()
예제 #5
0
def main(stdscr):

    port = getPort()
    cursesInit()
    stdscr.nodelay(True)
    #server setup
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.connect(('127.0.0.1', port))

    #send new user message
    dataString = "New User"
    dataSend = protocol_message(protocol_message.TYPE_NEW_USER,
                                len(dataString), dataString)
    server.send(dataSend.collapsed())
    z = 0
    while True:
        rlist, wlist, xlist = select.select([server], [], [], 0)
        for item in rlist:
            if item == server:
                dataRec = server.recv(1024)
                message_rec = protocol_message.message_from_collapsed(dataRec)

                if (message_rec.type == protocol_message.TYPE_WELCOME):
                    #board set up
                    userNum = message_rec.welcome_message_user_id()
                    Height = 5
                    Width = 10
                    canvas = board(
                        Width, Height)  #TODO get width and height from server
                    canvas.addUser(userNum)
                    startScreen(canvas, stdscr)
                    #printBoardClient(canvas, stdscr)

                if (message_rec.type == protocol_message.TYPE_CUR_BOARD):
                    #server sent the master board
                    canvas.stringToBoard(message_rec.message)
                    printBoardClient(canvas, stdscr)

                if (message_rec.type == protocol_message.TYPE_UPDATE_BOARD):
                    canvas.stringToBoard(message_rec.message)
                    printBoardClient(canvas, stdscr)
                    # if(canvas.stringToBoardFromServer(message_rec.message) == -1):
                    #         debugMsg("error recv board", z, stdscr)
                    #         z += 1
                    #printBoardClient(canvas, stdscr)

        #get user input through the keyboard
        key = getInput(stdscr)
        if (key == -1):
            continue

        if (key == ord("w") or key == ord("s") or key == ord("a")
                or key == ord("d") or key == curses.KEY_UP
                or key == curses.KEY_DOWN or key == curses.KEY_LEFT
                or key == curses.KEY_RIGHT):
            #update our board and send it
            newPos = posDelta(key)
            canvas.moveUser(newPos)
            boardString = canvas.boardToString()
            message_send = protocol_message(protocol_message.TYPE_UPDATE_BOARD,
                                            len(boardString), boardString)
            server.send(message_send.collapsed())

        if (key == ord("u")):
            s = "hello"
            message_send = protocol_message(protocol_message.TYPE_CUR_BOARD,
                                            len(s), s)
            server.send(message_send.collapsed())