集成客户端对象之间的通信#
如中所示 通过SAMP发送和接收表和图像 vt.的. SAMPIntegratedClient
类可用于与其他启用SAMP的工具进行通信,例如 TOPCAT , SAO DS9 ,或 Aladin Desktop 。
在本节中,我们将介绍如何设置两个 SAMPIntegratedClient
实例和它们之间的通信。
首先,启动SAMP hub,如中所述 启动和停止SAMP Hub服务器 .
接下来,我们创建两个客户端并将它们连接到集线器:
>>> from astropy import samp
>>> client1 = samp.SAMPIntegratedClient(name="Client 1", description="Test Client 1",
... metadata = {"client1.version":"0.01"})
>>> client2 = samp.SAMPIntegratedClient(name="Client 2", description="Test Client 2",
... metadata = {"client2.version":"0.25"})
>>> client1.connect()
>>> client2.connect()
我们现在定义接收通知、调用或响应时调用的函数:
>>> def test_receive_notification(private_key, sender_id, mtype, params, extra):
... print("Notification:", private_key, sender_id, mtype, params, extra)
>>> def test_receive_call(private_key, sender_id, msg_id, mtype, params, extra):
... print("Call:", private_key, sender_id, msg_id, mtype, params, extra)
... client1.ereply(msg_id, samp.SAMP_STATUS_OK, result = {"txt": "printed"})
>>> def test_receive_response(private_key, sender_id, msg_id, response):
... print("Response:", private_key, sender_id, msg_id, response)
我们订阅客户端1 "samp.app.*"
并将其绑定到相关函数:
>>> client1.bind_receive_notification("samp.app.*", test_receive_notification)
>>> client1.bind_receive_call("samp.app.*", test_receive_call)
现在,我们将客户端2接收到的消息标记绑定到适当的函数:
>>> client2.bind_receive_response("my-dummy-print", test_receive_response)
>>> client2.bind_receive_response("my-dummy-print-specific", test_receive_response)
现在我们已经准备好测试客户端和回调函数了。客户端2使用samp.app.echo“通过集线器的消息类型:
>>> client2.enotify_all("samp.app.echo", txt="Hello world!")
['cli#2']
Notification: 0d7f4500225981c104a197c7666a8e4e cli#2 samp.app.echo {'txt':
'Hello world!'} {'host': 'antigone.lambrate.inaf.it', 'user': 'unknown'}
我们还可以找到一个字典来指定当前将接收哪些客户端 samp.app.echo
信息::
>>> print(client2.get_subscribed_clients("samp.app.echo"))
{'cli#2': {}}
客户端2使用 "samp.app.echo"
消息类型使用 "my-dummy-print"
作为消息标记:
>>> print(client2.call_all("my-dummy-print",
... {"samp.mtype": "samp.app.echo",
... "samp.params": {"txt": "Hello world!"}}))
{'cli#1': 'msg#1;;cli#hub;;cli#2;;my-dummy-print'}
Call: 8c8eb53178cb95e168ab17ec4eac2353 cli#2
msg#1;;cli#hub;;cli#2;;my-dummy-print samp.app.echo {'txt': 'Hello world!'}
{'host': 'antigone.lambrate.inaf.it', 'user': 'unknown'}
Response: d0a28636321948ccff45edaf40888c54 cli#1 my-dummy-print
{'samp.status': 'samp.ok', 'samp.result': {'txt': 'printed'}}
客户端2然后使用 "samp.app.echo"
消息类型,将消息标记为 "my-dummy-print-specific"
::
>>> try:
... print(client2.call(client1.get_public_id(),
... "my-dummy-print-specific",
... {"samp.mtype": "samp.app.echo",
... "samp.params": {"txt": "Hello client 1!"}}))
... except samp.SAMPProxyError as e:
... print("Error ({0}): {1}".format(e.faultCode, e.faultString))
msg#2;;cli#hub;;cli#2;;my-dummy-print-specific
Call: 8c8eb53178cb95e168ab17ec4eac2353 cli#2
msg#2;;cli#hub;;cli#2;;my-dummy-print-specific samp.app.echo {'txt': 'Hello
Cli 1!'} {'host': 'antigone.lambrate.inaf.it', 'user': 'unknown'}
Response: d0a28636321948ccff45edaf40888c54 cli#1 my-dummy-print-specific
{'samp.status': 'samp.ok', 'samp.result': {'txt': 'printed'}}
我们现在可以定义一个函数来测试同步调用:
>>> def test_receive_sync_call(private_key, sender_id, msg_id, mtype, params, extra):
... import time
... print("SYNC Call:", sender_id, msg_id, mtype, params, extra)
... time.sleep(2)
... client1.reply(msg_id, {"samp.status": samp.SAMP_STATUS_OK,
... "samp.result": {"txt": "printed sync"}})
我们现在绑定 samp.test
消息类型到 test_receive_sync_call
::
>>> client1.bind_receive_call("samp.test", test_receive_sync_call)
>>> try:
... # Sync call
... print(client2.call_and_wait(client1.get_public_id(),
... {"samp.mtype": "samp.test",
... "samp.params": {"txt": "Hello SYNCRO client 1!"}},
... "10"))
... except samp.SAMPProxyError as e:
... # If timeout expires than a SAMPProxyError is returned
... print("Error ({0}): {1}".format(e.faultCode, e.faultString))
SYNC Call: cli#2 msg#3;;cli#hub;;cli#2;;sampy::sync::call samp.test {'txt':
'Hello SYNCRO Cli 1!'} {'host': 'antigone.lambrate.inaf.it', 'user':
'unknown'}
{'samp.status': 'samp.ok', 'samp.result': {'txt': 'printed sync'}}
最后,我们在最后断开客户端与集线器的连接:
>>> client1.disconnect()
>>> client2.disconnect()