class TcpServerComp(object): """Component TCP/IP server. Attributes ---------- server_cmd : TcpServer Server of the command and event. server_tel : TcpServer Server of the telemetry. """ def __init__(self): self.server_cmd = TcpServer() self.server_tel = TcpServer() async def main(self, ip_addr, port_cmd, port_tel): """Main function. Parameters ---------- ip_addr : str IP address. port_cmd : int Port of command to connect. port_tel : int Port of telemetry to connect. """ await asyncio.gather( self.server_cmd.main(ip_addr, port_cmd, MsgType.CMDEVL), self.server_tel.main(ip_addr, port_tel, MsgType.TEL), )
def __init__(self): super().__init__() self.read_config_json() self.setupUi(self) self.setup_new_game() self.tcpServer = TcpServer(1234, self.gotTcpServerData) self.tcpClient = TcpClient(1234, self.gotTcpClientData) self.tablica_1.enable(False) self.dlgGraczSieciowyA = None #self.setCentralWidget(self.tablica_1) #self.setCentralWidget(self.tablica_2) #self.setCentralWidget(self.centralwidget) # greenBrush = QBrush(Qt.green) # blueBrush = QBrush(Qt.blue) # blackPen = QPen(Qt.black) # blackPen.setWidth(5) self.new_game_button.clicked.connect(self.new_game) self.save_button.clicked.connect(self.save_state) self.load_button.clicked.connect(self.load_state) self.exit_button.clicked.connect(sys.exit) self.btReplay.clicked.connect(self.start_animation) self.undo_button.clicked.connect(self.undo) # self.exit_button.clicked.connect(sys.exit) # self.save_button.clicked.connect(self.plansza.save_state) # self.load_button.clicked.connect(self.plansza.load_state) # for x in range (rozmiarPlanszy) : # for y in range(rozmiarPlanszy): # hex = Hexagon(scene, x, y, size) # pola.append([hex, x, y]) #self.graphicsView_1.setScene(self.scene) # self.view = QGraphicsView(self.scene, self) # self.view.setGeometry(0, 0, 640, 520) # self.setWindowTitle("nazwa") # self.setGeometry(300, 200, 640, 520) # (położenie okna x, położenie okna y, szerokość, wysokość) self.typGry = 0 self.updateTypGry() self.show() self.animationStep = -1 self.timer = QTimer(self) self.timer.timeout.connect(self.timer_tick) self.timer.start(1000)
def main(): # main function kafkaClient = None tcpServer = None try: if confBox.getKafkaServerAddress( ) != "" and confBox.getKafkaServerPort() != 0: kafkaClient = KafkaClient(confBox.getKafkaServerAddress(), confBox.getKafkaServerPort(), confBox.getKafkaServerTopic()) kafkaClient.start() if confBox.getTcpServerAddress() != "" and confBox.getTcpServerPort( ) != 0: tcpServer = TcpServer( confBox.getTcpServerAddress(), \ confBox.getTcpServerPort() ) tcpServer.start() except Exception as e: logger.error(e) if kafkaClient != None: kafkaClient.stop() if tcpServer != None: tcpServer.stop() sys.exit(1) if kafkaClient != None: kafkaClient.join() if tcpServer != None: tcpServer.join()
def __init__(self, port=1234, clear_users=True): Thread.__init__(self) self.users = [] self.active_users = {} self.tcp_server = TcpServer(port, self) self.tcp_server.start() self.is_listening = True self.accepted_img_formats = ['png', 'jpg', 'jpeg'] try: os.mkdir('users') except FileExistsError: if clear_users: shutil.rmtree('users') os.mkdir('users')
class FanEntity(Agent): def __init__(self, **kwargs): super(FanEntity, self).__init__(**kwargs) self.speed = 'slow' self.tcpServer = TcpServer(config.get('port'), self) Thread(target=self.tcpServer.startServer).start() def process_data(self, msg): components = msg.split('&') data = {} # Extract individual fields from the message and create to dictionary for publishing for component in components: if '=' in component: (key, value) = component.split('=') data.update({key: value}) self.status_push(data) @Core.periodic(settings.HEARTBEAT_PERIOD) def publish_status(self): self.status_push({'speed': self.speed}) def status_push(self, data): prefix = settings.TYPE_PREFIX + '/' + AGENT_ID + '/data' headers = { headers_mod.FROM: AGENT_ID, headers_mod.CONTENT_TYPE: headers_mod.CONTENT_TYPE.JSON, headers_mod.DATE: datetime.datetime.today().isoformat(), "Location": config.get('location') } self.publish_all(data, prefix, headers) _log.debug("publishing status: %s", str(data)) def publish_all(self, data, prefix, headers): for item in data.keys(): topic = prefix + '/' + item self.vip.pubsub.publish('pubsub', topic, headers, jsonapi.dumps(data[item])) def set_speed(self, speed): self.speed = speed if self.tcpServer.isClientConnected(): self.tcpServer.sendData("speed={value}".format(value=SPEED_MAPPINGS[speed])) @PubSub.subscribe('pubsub', settings.TYPE_PREFIX + '/' + AGENT_ID + '/operations/speed') def on_set_speed(self, peer, sender, bus, topic, headers, message): print 'Fan Entity got\nTopic: {topic}, {headers}, Message: {message}'.format(topic=topic, headers=headers, message=message) self.set_speed(jsonapi.loads(message))
def __init__(self): self.server_cmd = TcpServer() self.server_tel = TcpServer()
class Server(Thread): def __init__(self, port=1234, clear_users=True): Thread.__init__(self) self.users = [] self.active_users = {} self.tcp_server = TcpServer(port, self) self.tcp_server.start() self.is_listening = True self.accepted_img_formats = ['png', 'jpg', 'jpeg'] try: os.mkdir('users') except FileExistsError: if clear_users: shutil.rmtree('users') os.mkdir('users') def run(self): while self.is_listening: pass self.tcp_server.is_listening = False def get_user_by_id(self, user_id): if user_id >= len(self.users): raise exception.NonExistingUser return self.users[user_id] def get_post_by_id(self, post_id): user = self.get_user_by_id(int(post_id / lpn)) if post_id % lpn >= len(user.posts): raise exception.NonExistingPost return user.posts[post_id % lpn] def generate_salt(self, string_length=10): components = string.ascii_letters + string.digits + string.punctuation ans = '' for i in range(string_length): ans = ans + random.choice(components) return ans def register(self, username, password): for user in self.users: if user.username == username: raise exception.UsedUsername new_user = User(username, password, len(self.users), self) self.users.append(new_user) def login(self, username, password, ip): for user in self.users: if user.username == username: if user.generate_hash(password) == user.password_hash: self.active_users[ip] = user return raise exception.IncorrectLogin raise exception.IncorrectLogin def like(self, post_id, ip): if ip not in self.active_users: raise exception.NotLoggedIn self.active_users[ip].like(post_id) def send_friend_request(self, ip, target_id): if ip not in self.active_users: raise exception.NotLoggedIn self.active_users[ip].send_friend_request(target_id) def accept_friend_request(self, ip, fr_index): if ip not in self.active_users: raise exception.NotLoggedIn self.active_users[ip].accept_friend_request(fr_index) def deny_friend_request(self, ip, fr_index): if ip not in self.active_users: raise exception.NotLoggedIn self.active_users[ip].deny_friend_request(fr_index) def begin_post(self, caption, file_format, addr): ip = addr[0] if ip not in self.active_users: raise exception.NotLoggedIn if file_format not in self.accepted_img_formats: raise exception.NotSupportedFormat return self.active_users[ip].begin_post(caption, file_format, addr) def request_post(self, number, addr): ip = addr[0] if ip not in self.active_users: raise exception.NotLoggedIn return self.active_users[ip].request_post(number, addr) def number_of_posts(self, ip): if ip not in self.active_users: raise exception.NotLoggedIn return self.active_users[ip].number_of_posts() def number_of_friend_requests(self, ip): if ip not in self.active_users: raise exception.NotLoggedIn return self.active_users[ip].number_of_friend_requests() def get_fr(self, position, ip): if ip not in self.active_users: raise exception.NotLoggedIn return self.active_users[ip].get_fr(position)
def __init__(self, **kwargs): super(FanEntity, self).__init__(**kwargs) self.speed = 'slow' self.tcpServer = TcpServer(config.get('port'), self) Thread(target=self.tcpServer.startServer).start()
class Window(QMainWindow, Ui_MainWindow): def __init__(self): super().__init__() self.read_config_json() self.setupUi(self) self.setup_new_game() self.tcpServer = TcpServer(1234, self.gotTcpServerData) self.tcpClient = TcpClient(1234, self.gotTcpClientData) self.tablica_1.enable(False) self.dlgGraczSieciowyA = None #self.setCentralWidget(self.tablica_1) #self.setCentralWidget(self.tablica_2) #self.setCentralWidget(self.centralwidget) # greenBrush = QBrush(Qt.green) # blueBrush = QBrush(Qt.blue) # blackPen = QPen(Qt.black) # blackPen.setWidth(5) self.new_game_button.clicked.connect(self.new_game) self.save_button.clicked.connect(self.save_state) self.load_button.clicked.connect(self.load_state) self.exit_button.clicked.connect(sys.exit) self.btReplay.clicked.connect(self.start_animation) self.undo_button.clicked.connect(self.undo) # self.exit_button.clicked.connect(sys.exit) # self.save_button.clicked.connect(self.plansza.save_state) # self.load_button.clicked.connect(self.plansza.load_state) # for x in range (rozmiarPlanszy) : # for y in range(rozmiarPlanszy): # hex = Hexagon(scene, x, y, size) # pola.append([hex, x, y]) #self.graphicsView_1.setScene(self.scene) # self.view = QGraphicsView(self.scene, self) # self.view.setGeometry(0, 0, 640, 520) # self.setWindowTitle("nazwa") # self.setGeometry(300, 200, 640, 520) # (położenie okna x, położenie okna y, szerokość, wysokość) self.typGry = 0 self.updateTypGry() self.show() self.animationStep = -1 self.timer = QTimer(self) self.timer.timeout.connect(self.timer_tick) self.timer.start(1000) def setup_new_game(self): self.tablica_1 = Ui_Form(self.wymiar, "Gracz A:", "A", self) self.tablica_2 = Ui_Form(self.wymiar, "Gracz B:", "B", self) self.tablica_1.setupUi(self, 0) self.tablica_2.setupUi(self, 400) self.history = [] self.history.clear() self.add_to_history() def update_step_info(self, p1, p2): self.lbStepInfo.setText(str(p1) + "/" + str(p2)) def add_to_history(self): self.history.append([ self.tablica_1.plansza.get_pola_copy(), self.tablica_2.plansza.get_pola_copy() ]) self.update_step_info(len(self.history), len(self.history)) def movement(self, idGracza, typRuchu): print(idGracza, ":", typRuchu) self.add_to_history() if self.typGry == 0 or self.typGry == 1: return self.tablica_1.enable(idGracza == "B") self.tablica_2.enable(idGracza == "A") def print(self, color, msg): print(color[0] + msg + Colors.ENDC[0]) self.teLog.append(color[1] + msg) return def keyPressEvent(self, event): if event.key() == Qt.Key_Q: self.plansza.moveNWFull() if event.key() == Qt.Key_E: self.plansza.moveNEFull() if event.key() == Qt.Key_A: self.plansza.moveWFull() if event.key() == Qt.Key_D: self.plansza.moveEFull() if event.key() == Qt.Key_Z: self.plansza.moveSWFull() if event.key() == Qt.Key_C: self.plansza.moveSEFull() if event.key() == Qt.Key_N: self.plansza.new_game() if event.key() == Qt.Key_P: self.plansza.save_state() if event.key() == Qt.Key_L: self.plansza.load_state() def mousePressEvent(self, event: PySide2.QtGui.QMouseEvent) -> None: super().mousePressEvent(event) self.tablica_1.mouse_pressed(event.x(), event.y()) self.tablica_2.mouse_pressed(event.x(), event.y()) def mouseReleaseEvent(self, event: PySide2.QtGui.QMouseEvent) -> None: super().mouseReleaseEvent(event) self.tablica_1.mouse_released(event.x(), event.y()) self.tablica_2.mouse_released(event.x(), event.y()) def gotTcpClientData(self, data): return def gotTcpServerData(self, data): print("gotTcpServerData:", data) if self.dlgGraczSieciowyA != None: self.dlgGraczSieciowyA.close() self.tablica_1.enable(True) def updateTypGry(self): self.tablica_1.enable(False) self.tablica_2.enable(False) if self.typGry == 1: self.tablica_1.enable(True) elif self.typGry == 2: self.tablica_1.enable(True) self.tablica_2.enable(True) elif self.typGry == 3: self.tcpServer.startListen() self.dlgGraczSieciowyA = GraczSieciowyADlg() self.dlgGraczSieciowyA.exec() self.dlgGraczSieciowyA = None #self.tablica_1.enable(True) def save_config_json(self, wymiar): configObj = Config() configObj.wymiar = wymiar text_file = open("config.json", "w") text_file.write(configObj.toJSON()) text_file.close() def read_config_json(self): try: text_file = open("config.json", "r") json_str = text_file.read() #print(json_str) text_file.close() self.wymiar = int(re.sub("[^0-9]", "", json_str)) except: print("nie udało się odczytać konfiguacji") self.wymiar = 3 def new_game(self): dlgWyborGryDlg = WyborGryDlg(self) dlgWyborGryDlg.exec() self.typGry = dlgWyborGryDlg.typGry self.wymiar = dlgWyborGryDlg.wymiar() self.save_config_json(self.wymiar) self.updateTypGry() self.tablica_1.new_game(self.wymiar) self.tablica_2.new_game(self.wymiar) self.history = [] self.history.clear() self.add_to_history() def save_state(self): root = tk.Tk() root.withdraw() file = filedialog.asksaveasfile(filetypes=[("Xml files", "*.xml")]) xml_doc = ET.Element('root') product = ET.SubElement(xml_doc, 'wymiar').text = str(self.wymiar) product = ET.SubElement(xml_doc, 'tableAEnabled').text = str( self.tablica_1.enabled) product = ET.SubElement(xml_doc, 'tableBEnabled').text = str( self.tablica_2.enabled) product = ET.SubElement(xml_doc, 'typGry').text = str(self.typGry) product = ET.SubElement(xml_doc, 'saved_state').text = repr(self.history) tree = ET.ElementTree(xml_doc) tree.write(file.name, encoding='UTF-8', xml_declaration=True) # self.logFunction("\n"+self.print_prefix+"ZAPISANO") print("zapisano") def load_state(self): root = tk.Tk() root.withdraw() file_path = filedialog.askopenfilename(filetypes=[("Xml files", "*.xml")]) try: with open(file_path, 'rt') as f: fileContent = f.read() sets = re.findall(r'<wymiar>(.*)</wymiar>', fileContent) self.wymiar = int(sets[0]) sets = re.findall(r'(\[.*\])', fileContent) self.history = ast.literal_eval(sets[0]) self.print(Colors.RED, "wczytano plik :" + file_path) length = len(self.history) self.print(Colors.RED, "wczytano ruchów:" + str(length)) sets = re.findall(r'<tableAEnabled>(.*)</tableAEnabled>', fileContent) self.tablica_1.enable(sets[0] == "True") sets = re.findall(r'<tableBEnabled>(.*)</tableBEnabled>', fileContent) self.tablica_2.enable(sets[0] == "True") sets = re.findall(r'<typGry>(.*)</typGry>', fileContent) self.typGry = int(sets[0]) self.tablica_1.new_game(self.wymiar) self.tablica_2.new_game(self.wymiar) self.tablica_1.plansza.set_pola_copy(self.history[length - 1][0]) self.tablica_2.plansza.set_pola_copy(self.history[length - 1][1]) self.tablica_1.create_scene() self.tablica_2.create_scene() self.update_step_info(len(self.history), len(self.history)) except: self.print( Colors.RED, "Plik z zapisaną grą nie został znaleziony lub błąd odczytu!") # # # def mouseReleaseEvent(self, event): # #super(Window, self).mouseReleaseEvent(event) # delta_y = event.y() - self.tempy # delta_x = event.x() - self.tempx # vect_len = math.sqrt(delta_x**2+delta_y**2) # angle = 180 + math.atan2(delta_y, -delta_x) * 180 / math.pi # margin = 20 # if vect_len > 40: # if angle > 360 - margin or angle < 0 + margin: # self.plansza.moveEFull() # if 60 - margin < angle < 60 + margin: # self.plansza.moveNEFull() # if 120 - margin < angle < 120 + margin: # self.plansza.moveNWFull() # if 180 - margin < angle < 180 + margin: # self.plansza.moveWFull() # if 240 - margin < angle < 240 + margin: # self.plansza.moveSWFull() # if 300 - margin < angle < 300 + margin: # self.plansza.moveSEFull() def update_screen_to_history_step(self, idx): self.tablica_1.plansza.set_pola_copy(self.history[idx][0]) self.tablica_2.plansza.set_pola_copy(self.history[idx][1]) self.tablica_1.create_scene() self.tablica_2.create_scene() self.update_step_info(idx + 1, len(self.history)) def timer_tick(self): if self.animationStep == -1: return self.update_screen_to_history_step(self.animationStep) self.animationStep += 1 if self.animationStep == len(self.history): print("AnimationStep2 ", self.animationStep) self.animationStep = -1 self.update_step_info(len(self.history), len(self.history)) self.tablica_1.enable(self.tablica1WasEnabled) self.tablica_2.enable(self.tablica2WasEnabled) self.new_game_button.setEnabled(True) self.save_button.setEnabled(True) self.load_button.setEnabled(True) self.undo_button.setEnabled(True) self.autoplay_button.setEnabled(True) def start_animation(self): self.tablica1WasEnabled = self.tablica_1.enabled self.tablica2WasEnabled = self.tablica_2.enabled self.tablica_1.enable(False) self.tablica_2.enable(False) self.new_game_button.setEnabled(False) self.save_button.setEnabled(False) self.load_button.setEnabled(False) self.undo_button.setEnabled(False) self.autoplay_button.setEnabled(False) self.animationStep = 0 def undo(self): if len(self.history) > 1: self.history.remove(self.history[-1]) self.update_screen_to_history_step(len(self.history) - 1) self.tablica_1.enable(not self.tablica_1.enabled) self.tablica_2.enable(not self.tablica_2.enabled) if len(self.history) == 1: self.tablica_1.enable(True) self.tablica_2.enable(True)