Beispiel #1
0
class Gamepad(threading.Thread):
    def __init__(self, window):
        threading.Thread.__init__(self)
        self.done = False
        self.window = window
        self.socket = None
        self.socket = SocketClient('localhost', 10000, self.window.set_status)

    def run(self):
        self.socket.connect()

        if self.socket.is_connected:
            pygame.init()
            pygame.joystick.init()

            while not self.done:
                pygame.event.get()
                joystick_count = pygame.joystick.get_count()

                if joystick_count > 0:
                    joystick = pygame.joystick.Joystick(0)
                    joystick.init()

                    self.send_value(Gamepad.sanitize_digital(joystick.get_axis(1)), self.window.update_left_analog)
                    self.send_value(Gamepad.sanitize_digital(joystick.get_axis(3)), self.window.update_right_analog)
                    self.send_value(joystick.get_hat(0), self.window.update_digital)

                sleep(0.05)
            pygame.quit()

    @staticmethod
    def sanitize_digital(value):
        rounded = round(value * -10)
        if rounded == 1 or rounded == -1:
            return 0
        return rounded

    def send_value(self, value, callback):
        message = [value, int(round(time.time() * 1000))]
        self.socket.send(message, callback)

    def stop(self):
        self.socket.disconnect()
        self.window.set_status('Disconnected')
        self.done = True
Beispiel #2
0
def init(clid):
    global client_socket
    global client_id
    client_socket = SocketClient(sc.ser_ip, sc.ser_port)
    # sign in
    recv_msg_1 = client_socket.send({"signal": "sign", "client_id": clid})
    if recv_msg_1.get("client_id"):
        client_id = recv_msg_1.get("client_id")
        logging.info("client_id: %s" % client_id)
    else:
        sys.exit(recv_msg_1.get("erro"))
    logging.info("Line 44: client_id = %s" % client_id)
Beispiel #3
0
    'MAX_SIZE_THREADPOOL': 3,
    'CRAWL_DELAY': 2
}

# db manager
dbmanager = MongoRedisUrlManager()

start_time = time.time()
threads = {}
webdrivers = {}

socket_client = SocketClient('localhost', 20100)

register_request = {}
register_request[pc.MSG_TYPE] = pc.REGISTER
client_id = socket_client.send(json.dumps(register_request))

run_heartbeat = True
server_status = pc.STATUS_RUNNING

re_compiled_obj = re.compile('\d{7}')
hbase = HBaseManager(host='54.223.92.169')

thread_pool_drivers = {}

if __name__ == '__main__':
    # start heartbeat thread
    try:
        t = threading.Thread(target=heartbeat, name=None)
        # set daemon so main thread can exit when receives ctrl-c
        t.setDaemon(True)
Beispiel #4
0
raw_text = "raffles|31|5|7|9"

auth = ServerAuth()

# cipher_text = auth.encrypt_text(raw_text, key)
# print(cipher_text)
#
# decoded_text = auth.decrypt_text(cipher_text, key)
# print(decoded_text)

socket_client = SocketClient('localhost', 8888)
socket_client.connect()

# Final_eval_server command:
# python final_eval_server_5moves.py localhost 8888 group_num_123

user_inp = ''
while 1:
    print('Input: ')
    user_inp = input()
    try:
        if user_inp == '1':
            # msg = auth.encrypt_text(raw_text, key)
            socket_client.send(raw_text)
            print('sent')
            print()
        elif user_inp == '2':
            break
    except KeyboardInterrupt:
        break
Beispiel #5
0
class Client:
    sc = None
    id = None
    token = None
    _self = None
    _users = []

    def __init__(self, host: str) -> None:
        addr, port = host.split(':')
        host = (addr, int(port))
        self.sc = SocketClient(host)

    def check_login(self):
        if self.id is None:
            raise Exception("Must login first")

    def login(self, username, password):
        req = LoginRequest()
        req.username = username
        req.password = password
        rsp = self.sc.send(req, LoginResponse)
        if not rsp.success:
            raise Exception('Failed to login: %s' % rsp.info)
        self.token = rsp.token
        self.sc.token = rsp.token
        self.id = rsp.id

    def logout(self):
        self.check_login()
        req = LogoutRequest()
        req.senderID = self.id
        rsp = self.sc.send(req, Response)
        if not rsp.success:
            raise Exception('Failed to logout: %s' % rsp.info)

    def signup(self, username, password):
        req = SignupRequest()
        req.username = username
        req.password = password
        rsp = self.sc.send(req, Response)
        if not rsp.success:
            raise Exception('Failed to signup: %s' % rsp.info)

    def get_users(self) -> [UserInfo]:
        self.check_login()
        req = GetUserInfosRequest()
        req.senderID = self.id
        rsp = self.sc.send(req, GetUserInfosResponse)
        self._users = rsp.users
        self._self = [u for u in self._users if u.id == self.id][0]
        return rsp.users

    def get_self(self) -> UserInfo:
        if self._self is None:
            self.get_users()
        return self._self

    def get_messages(self, afterTime=0) -> [ChatMessage]:
        self.check_login()
        req = GetMessagesRequest()
        req.senderID = self.id
        req.afterTimeUnix = int(afterTime)
        rsp = self.sc.send(req, GetMessagesResponse)
        return rsp.messages

    def get_user(self, username) -> UserInfo:
        self.check_login()
        users = self.get_users()
        users = [u for u in users if u.username == username]
        if len(users) == 0:
            raise Exception('No such user \'%s\'' % username)
        return users[0]

    def make_friend_with(self, username):
        self.check_login()
        req = MakeFriendRequest()
        req.senderID = self.id
        req.targetID = self.get_user(username).id
        rsp = self.sc.send(req, Response)
        if not rsp.success:
            raise Exception('Failed to make friend: %s' % rsp.info)

    def send_message(self, targetId, text):
        self.check_login()
        req = ChatMessage()
        req.senderID = self.id
        req.targetID = targetId
        # req.timeUnix = 0
        req.text = text
        rsp = self.sc.send(req, Response)
        if not rsp.success:
            raise Exception('Failed to send message: %s' % rsp.info)

    def send_file(self, targetId, filepath):
        self.check_login()
        req = ChatMessage()
        req.senderID = self.id
        req.targetID = targetId
        # req.timeUnix = 0
        file = open(filepath, 'rb')
        data = file.read()
        file.close()
        req.file = data
        rsp = self.sc.send(req, Response)
        if not rsp.success:
            raise Exception('Failed to send file: %s' % rsp.info)

    def recv_message(self) -> [ChatMessage]:
        self.check_login()
        req = GetMessagesRequest()
        req.senderID = self.id
        rsp = self.sc.send(req, GetMessagesResponse)
        return rsp.messages