using_perspective_broker
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| using_perspective_broker [2022/11/30 12:32] – serge | using_perspective_broker [2022/12/16 10:42] (Version actuelle) – [Using Perspective Broker] serge | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| ======Using Perspective Broker====== | ======Using Perspective Broker====== | ||
| + | {{ : | ||
| + | |||
| + | {{ ::labo.svg |}} | ||
| + | |||
| <WRAP center round info centeralign 60%> | <WRAP center round info centeralign 60%> | ||
| - | **Niveau Pro et intergalactique !** | + | **Niveau Pro et intergalactique !**\\ |
| + | Cette API: | ||
| + | * a entre 15 et 20 ans | ||
| + | * n'est que vaguement maintenu | ||
| + | * un exemple au moins ne marche pas | ||
| + | * le tranfert de datas simples marche | ||
| + | * le transfert d' | ||
| + | * les exemples ne respectent pas le PEP 8, c'est imbriqué au possible | ||
| </ | </ | ||
| <WRAP center round box 60% > | <WRAP center round box 60% > | ||
| * **{{tagpage> | * **{{tagpage> | ||
| - | * **[[perspective_broker|Page précédente]]** | + | * **[[perspective_broker|Page précédente]]** Python: Twisted Perspective Broker |
| + | * **[[managing_clients_of_perspectives|Page suivante]]** Managing Clients of Perspectives | ||
| + | * **[[https:// | ||
| </ | </ | ||
| Ligne 223: | Ligne 236: | ||
| =====Exemple 4: Echanges bidirectionnels===== | =====Exemple 4: Echanges bidirectionnels===== | ||
| + | Cet exemple est trop confus, il est le mélange de 2 et 3. Il est une étape intermédiaire avec l' | ||
| ====Code==== | ====Code==== | ||
| - | <file python | + | <file python |
| """ | """ | ||
| Serveur: Echange bidirectionnel serveur client | Serveur: Echange bidirectionnel serveur client | ||
| Ligne 269: | Ligne 283: | ||
| </ | </ | ||
| - | <file python | + | <file python |
| """ | """ | ||
| Client: Echange bidirectionnel serveur client | Client: Echange bidirectionnel serveur client | ||
| Ligne 309: | Ligne 323: | ||
| def1.addCallback(got_obj, | def1.addCallback(got_obj, | ||
| reactor.run() | reactor.run() | ||
| + | main() | ||
| + | </ | ||
| + | =====Exemple 5: Un vrai client/ | ||
| + | Le serveur tourne.\\ | ||
| + | Dans une boucle, le client récupére des infos sur le serveur, et lui envoie lui aussi des valeurs. | ||
| + | |||
| + | <file python pb5server.py> | ||
| + | |||
| + | from time import sleep | ||
| + | from threading import Thread | ||
| + | |||
| + | from twisted.internet import reactor | ||
| + | from twisted.spread import pb | ||
| + | |||
| + | class PlayerSimulator(pb.Referenceable): | ||
| + | """ | ||
| + | Il simule un player avec: | ||
| + | - une position de lecture du fichier musical à self.position | ||
| + | - un track à lire imposé par le client | ||
| + | L' | ||
| + | """ | ||
| + | def __init__(self): | ||
| + | self.track = 1 | ||
| + | self.position = 0 | ||
| + | Thread(target=self.thread_update_position).start() | ||
| + | |||
| + | def thread_update_position(self): | ||
| + | n = 0 | ||
| + | while n < 1000: | ||
| + | n += 1 | ||
| + | self.position += 1 | ||
| + | sleep(1) | ||
| + | |||
| + | def remote_newTrack(self, | ||
| + | """ | ||
| + | self.track = track | ||
| + | print(f" | ||
| + | |||
| + | class ObjetPrincipal(pb.Root): | ||
| + | def __init__(self, | ||
| + | self.player = player | ||
| + | |||
| + | def remote_getPlayerSimulator(self): | ||
| + | print(f" | ||
| + | return self.player | ||
| + | |||
| + | def remote_getPosition(self): | ||
| + | p = self.player.position | ||
| + | print(f" | ||
| + | return p | ||
| + | |||
| + | # un object auquel le client pourra accéder | ||
| + | player = PlayerSimulator() | ||
| + | |||
| + | # L' | ||
| + | root_obj = ObjetPrincipal(player) | ||
| + | |||
| + | reactor.listenTCP(8800, | ||
| + | reactor.run() | ||
| + | </ | ||
| + | |||
| + | <file python pb5client.py> | ||
| + | from time import sleep | ||
| + | from threading import Thread | ||
| + | |||
| + | from twisted.internet import reactor | ||
| + | # Interaction réseau en TCP | ||
| + | from twisted.spread import pb # Perspetive Broker de Twisted | ||
| + | |||
| + | class Client: | ||
| + | """ | ||
| + | |||
| + | def __init__(self): | ||
| + | """ | ||
| + | self.root_obj_client = None | ||
| + | self.track = 1 | ||
| + | |||
| + | def on_start(self, | ||
| + | print(f" | ||
| + | self.root_obj_client = obj | ||
| + | # Appel du Player sur le serveur et ajout du callback | ||
| + | self.root_obj_client.callRemote(" | ||
| + | |||
| + | def run(self, args): | ||
| + | Thread(target=self.thread_run).start() | ||
| + | |||
| + | def thread_run(self): | ||
| + | n = 0 | ||
| + | while n < 500: | ||
| + | # Position | ||
| + | n += 1 | ||
| + | self.root_obj_client.callRemote(" | ||
| + | |||
| + | # Changement de track tous les 10 | ||
| + | if n % 10 == 2: | ||
| + | self.track += 1 | ||
| + | self.root_obj_client.callRemote(" | ||
| + | sleep(0.3) | ||
| + | |||
| + | def toto(self, args): | ||
| + | print(" | ||
| + | print(f" | ||
| + | args.callRemote(" | ||
| + | |||
| + | |||
| + | def print_position(self, | ||
| + | print(f" | ||
| + | |||
| + | |||
| + | def main(): | ||
| + | clt = Client() | ||
| + | factory = pb.PBClientFactory() | ||
| + | reactor.connectTCP(" | ||
| + | factory.getRootObject().addCallback(clt.on_start) | ||
| + | reactor.run() | ||
| + | |||
| + | main() | ||
| + | </ | ||
| + | |||
| + | =====Transfert d'une image ou d'un json===== | ||
| + | On peut transférer n' | ||
| + | <file python > | ||
| + | from time import sleep | ||
| + | from threading import Thread | ||
| + | |||
| + | import numpy as np | ||
| + | import cv2 | ||
| + | |||
| + | from twisted.internet import reactor | ||
| + | from twisted.spread import pb | ||
| + | |||
| + | class MyImage: | ||
| + | def __init__(self): | ||
| + | self.img = cv2.imread(' | ||
| + | img = cv2.resize(self.img, | ||
| + | encode_param=[int(cv2.IMWRITE_JPEG_QUALITY), | ||
| + | result, imgencode = cv2.imencode(' | ||
| + | data = np.array(imgencode) | ||
| + | print(data.shape) | ||
| + | self.stringData = data.tobytes() | ||
| + | |||
| + | |||
| + | class ObjetPrincipal(pb.Root): | ||
| + | def __init__(self): | ||
| + | self.mi = MyImage() | ||
| + | def remote_image(self): | ||
| + | print(" | ||
| + | return self.mi.stringData | ||
| + | |||
| + | # L' | ||
| + | root_obj = ObjetPrincipal() | ||
| + | |||
| + | reactor.listenTCP(8800, | ||
| + | reactor.run() | ||
| + | </ | ||
| + | |||
| + | <file python > | ||
| + | import numpy as np | ||
| + | import cv2 | ||
| + | import base64 | ||
| + | from twisted.internet import reactor | ||
| + | from twisted.spread import pb | ||
| + | |||
| + | |||
| + | class GetImage(pb.Copyable): | ||
| + | def get_image(self, | ||
| + | image = np.asarray(bytearray(stringData), | ||
| + | image = cv2.imdecode(image, | ||
| + | print(image.shape) | ||
| + | cv2.imshow(' | ||
| + | cv2.waitKey(0) | ||
| + | cv2.destroyAllWindows() | ||
| + | |||
| + | class RemoteGetImage(pb.RemoteCopy): | ||
| + | pass | ||
| + | |||
| + | pb.setUnjellyableForClass(GetImage, | ||
| + | |||
| + | class Client: | ||
| + | """ | ||
| + | def __init__(self): | ||
| + | """ | ||
| + | self.root_obj_client = None | ||
| + | self.gi = GetImage() | ||
| + | def on_start(self, | ||
| + | print(f" | ||
| + | self.root_obj_client = obj | ||
| + | # Transfert d'une image | ||
| + | self.root_obj_client.callRemote(" | ||
| + | | ||
| + | def main(): | ||
| + | clt = Client() | ||
| + | factory = pb.PBClientFactory() | ||
| + | reactor.connectTCP(" | ||
| + | factory.getRootObject().addCallback(clt.on_start) | ||
| + | reactor.run() | ||
| + | |||
| main() | main() | ||
| </ | </ | ||
| - | =====Page suivante: | + | =====Page suivante: |
| - | * **[[twisted_perspective_broker_et_kivy|Twisted Perspective Broker et Kivy]]** | + | * **[[pb_copyable_passing_complex_types|PB Copyable Passing Complex Types]]** |
| {{tag> | {{tag> | ||
using_perspective_broker.1669811531.txt.gz · Dernière modification : de serge
