def __init__(self, daemon=True, name=None): self.client = sampy.SAMPIntegratedClient(metadata={ "samp.name": "Gavi client" if name is None else name, "samp.description.text": "Gavi client" if name is None else name, "gavi.samp.version": "0.01" }, callable=True) # sampy doesn't make this thread Daeamon, so the python process never stops on the cmd line # this fixes that def _myrun_client(): if self.client.client._callable: self.client.client._thread = threading.Thread( target=self.client.client._serve_forever) self.client.client._thread.setDaemon(True) self.client.client._thread.start() if daemon: self.client.client._run_client = _myrun_client connected = False try: self.client.connect() connected = True except sampy.SAMPHubError as e: print(("error connecting to hub", e)) if connected: #self.client.client._thread.setDaemon(False) logger.info("connected to SAMP hub") logger.info("binding events") self.client.bindReceiveCall("table.load.votable", self._onTableLoadVotable) self.client.bindReceiveNotification("table.load.votable", self._onTableLoadVotable) #self.client.bindReceiveNotification ("table.highlight.row", self._onSampNotification) #self.client.bindReceiveMessage("table.load.votable", self._onSampCall) #self.client.bindReceiveResponse("table.load.votable", self._onSampCall) #self.client.bindReceiveCall("samp.*", self._onSampCall) #self.client.bindReceiveNotification("samp.*", self._onSampNotification) #self.client.bindReceiveCall("table.*", self._onSampCall) #self.client.bindReceiveNotification("table.*", self._onSampNotification) #self.client.bindReceiveMessage("table.*", self._onSampCall) #self.client.bindReceiveResponse("table.*", self._onSampCall) #self.client.bindReceiveMessage("table.votable.*", self._onSampCall) #self.client.bindReceiveResponse("table.votable.*", self._onSampCall) #def connect(self): # self.client.connect() self.tableLoadCallbacks = []
def setUpClass(cls): cls.hub = samp.SAMPHubServer() cls.hub.start() time.sleep(5) thread.start_new_thread(mtypes.main, ()) cls.cli = samp.SAMPIntegratedClient() cls.cli.connect() time.sleep(5)
def __init__(self, addr='localhost', hub=True): # Before we start, let's kill off any zombies if hub: # sampy seems to fall over sometimes if 'localhost' isn't specified # even though it shouldn't self.hub = sampy.SAMPHubServer(addr=addr) self.hub.start() else: self.hub = None self.metadata = { 'samp.name': 'Python Session', 'samp.icon.url': 'http://docs.scipy.org/doc/_static/img/scipyshiny_small.png', 'samp.description.text': 'Python Samp Module', 'client.version': '0.1a' } self.client = sampy.SAMPIntegratedClient(metadata=self.metadata, addr=addr) self.client.connect() atexit.register(self.client.disconnect) # Bind interaction functions - we will register that we want to listen # for table.highlight.row (the SAMP highlight protocol), and all the # typical SAMP-stuff We could include this to listen for other sorts of # subscriptions like pointAt(), etc. #self.client.bindReceiveNotification('table.highlight.row', self.highlightRow) #self.client.bindReceiveNotification('table.select.rowList', self.getrows) self.client.bindReceiveNotification('table.highlight.row', self.getrows) self.client.bindReceiveNotification('table.load.fits', self.receiveNotification) self.client.bindReceiveNotification('samp.app.*', self.receiveNotification) self.client.bindReceiveCall('samp.app.*', self.receiveCall) self.client.bindReceiveCall('table.load.fits', self.receiveCall) self.client.bindReceiveCall('table.select.rowList', self.receiveCall) self.tables = {} self.rowlist = {} self.crow = None self.lastMessage = None tmpdir = os.getenv('HOME') + '/tempo/samptables' if not os.path.exists(tmpdir): os.makedirs(tmpdir) else: for f in os.listdir(tmpdir): os.remove(tmpdir + '/' + f) # maybe ask first before deleting ? self.tmpdir = tmpdir
def __init__(self, callback, check_connection_period=1): self.callback = callback self.dirname = os.path.dirname(os.path.abspath(__file__)) self.timer = None self.samp_hub = None self.samp_client = None self.metadata = { 'samp.name': 'iSpec', 'samp.description.text': 'iSpec', 'samp.icon.url': 'file://' + self.dirname + '/images/iSpec.png', } self.samp_client = sampy.SAMPIntegratedClient(metadata=self.metadata, addr='localhost') self.check_connection_period = check_connection_period # seconds self.__check_connection() signal.signal(signal.SIGINT, self.__signal_handler)
def __check_connection(self): status = "Status: " if not self.is_connected(): if self.samp_client is not None and self.samp_client.isConnected(): # Destroy old client to completely reset the connection status del self.samp_client self.samp_client = sampy.SAMPIntegratedClient( metadata=self.metadata, addr='localhost') logging.info("SAMP Connection lost") status += "Disconnected" self.__connect() else: status += "Connected" pass logging.debug(status) self.timer = threading.Timer(self.check_connection_period, self.__check_connection) self.timer.start()
def __init__(self, parent, name, description): self.root = parent self.name = name """ application name """ self.description = description """ aplication description """ self.sentTableCount = 0 """ nuimber of table sent """ self.client = s.SAMPIntegratedClient() """ samp client """ self.statusVar = tk.StringVar() """ contains connection status, on or off """ self.__connectionDisabled() self.frame = tk.Frame(parent) statusLabel = tk.Label(self.frame, textvariable=self.statusVar) statusLabel.pack() self.frame.pack()
def main(): info("Starting " + __name__) global cli cli = samp.SAMPIntegratedClient(metadata, addr='localhost') try: global __serving last_ping = time.time() while __serving: try: time.sleep(1.5) if cli.hub.getRunningHubs() and cli.isConnected(): if not connected: info("Successful ping at: " + str(last_ping)) connected = True last_ping = time.time() else: connected = False if not connected: info("not connected") try: info("trying connection") cli.connect() info("trying registration") register() info("registered") except samp.SAMPHubError as e: warn("got SAMPHubError") logging.exception(e) if time.time() > last_ping + 60: warn("giving up") raise KeyboardInterrupt("Ping timeout") except Exception, e: warn("Got exception during main monitor thread") logging.exception(e) finally: info("stopping") stop()
#/usr/bin/env python # -*- coding: utf-8 -*- import sys import sampy describeTesterMetadata = { 'samp.name': 'movoir.describe.mtype.tester', 'samp.description.text': 'Tester application for the movoir.describe.mtype message.', 'movoir.version': '0.1' } describeTester = sampy.SAMPIntegratedClient(describeTesterMetadata) describeTester.connect() clientsToTest = describeTester.getSubscribedClients("movoir.describe.mtype") for testClient in clientsToTest: messageToSend = { 'samp.mtype': 'movoir.describe.mtype', 'samp.params': { 'mtype': 'table.load.votable', 'verbose': 'true' } } try: testClientMetadata = describeTester.getMetadata(testClient) resultMap = describeTester.callAndWait(testClient, messageToSend, '30') assert resultMap.has_key('samp.status'), "Falta samp.status" assert resultMap[ 'samp.status'] == 'samp.ok', "samp.status no es samp.ok" assert resultMap.has_key('samp.result'), "Falta samp.result"
'samp.result': {}, 'samp.error': { 'samp.errortxt': 'No url parameter provided.' } } vot2csvClient.reply(msg_id, replyMsg) def responseHandler(private_key, sender_id, msg_id, response): # Response received, typically an OK response, # we should not be receiving none of these print("Receiving response", private_key, sender_id, msg_id, response) # Start binding behaviours vot2csvClient = sampy.SAMPIntegratedClient(vot2csvMD) log = sampy.SAMPLog(level=sampy.SAMPLog.OFF) try: hub = sampy.SAMPHubServer(addr='127.0.0.1', port=50000, log=log) except sampy.SAMPHubError, err: print err print "Possibly, already working hub." try: vot2csvClient.connect() except sampy.SAMPHubError, err: if err.value == 'Unable to find a running SAMP Hub.': try: hub.start() vot2csvClient.connect() except:
#!/usr/bin/python import sampy import time def get_notification(private_key, sender_id, mtype, params, extra): print "Notification %s received" % mtype cli = sampy.SAMPIntegratedClient(metadata = {"samp.name": "Disconnect test"}) cli.connect() cli.bindReceiveNotification("samp.hub.disconnect", get_notification) try: while True: time.sleep(0.01) except KeyboardInterrupt: try: cli.disconnect() except: pass
#!/usr/bin/python import sampy import time def receive_notify(private_key, sender_id, mtype, params, extra): print "Extra:", extra cli1 = sampy.SAMPIntegratedClient(metadata={"samp.name": "Example 1"}) cli2 = sampy.SAMPIntegratedClient(metadata={"samp.name": "Example 2"}) #cli1.connect(user = "******", password = "******") #cli2.connect(user = "******", password = "******") cli1.connect() cli2.connect() cli2.bindReceiveNotification("test.notify", receive_notify) cli1.enotify(cli2.getPublicId(), "test.notify") try: while True: time.sleep(0.01) except KeyboardInterrupt: try: cli1.disconnect()
'samp.result': {}, 'samp.error': { 'samp.errortxt': 'No url parameter provided.' } } vot2fitsClient.reply(msg_id, replyMsg) def responseHandler(private_key, sender_id, msg_id, response): # Response received, typically an OK response, # we should not be receiving none of these print("Receiving response", private_key, sender_id, msg_id, response) # Start binding behaviours vot2fitsClient = sampy.SAMPIntegratedClient(vot2fitsMD) try: vot2fitsClient.connect() except sampy.SAMPHubError, err: if err.value == 'Unable to find a running SAMP Hub.': try: log = sampy.SAMPLog(level=sampy.SAMPLog.OFF) hub = sampy.SAMPHubServer(addr='127.0.0.1', port=50000, log=log) hub.start() vot2fitsClient.connect() except: raise vot2fitsClient.bindReceiveNotification("table.load.votable", notificationHandler)
print "In the error branch" replyMsg = {'samp.status': sampy.SAMP_STATUS_ERROR, 'samp.result': {}, 'samp.error': {'samp.errortxt': 'No url parameter provided.'}} amigaCSclient.reply(sender_id, msg_id, replyMsg) def responseHandler(private_key, sender_id, msg_id, response): # Response received, typically an OK response, # we should not be receiving none of these print ("Receiving response", private_key, sender_id, msg_id, response) # Start binding behaviours amigaCSclient = sampy.SAMPIntegratedClient(amigaCSmd) amigaCSclient.connect() amigaCSclient.bindReceiveNotification(coordMessage, notificationHandler) amigaCSclient.bindReceiveCall(coordMessage, callHandler) amigaCSclient.bindReceiveResponse("clickAMIGA", responseHandler) try: while True: time.sleep(0.01) except KeyboardInterrupt: print "SIGINT received" try: amigaCSclient.disconnect() except: pass
#!/usr/bin/env python import sampy cli = sampy.SAMPIntegratedClient()
filename=logfile, # FIXME: user permissions! filemode='a') info = logger.info warn = logger.warning error = logger.error metadata = { "samp.name": "Sherpa", "samp.description.text": "Sherpa SAMP Interface", "cli1.version": "0.3.1", "samp.description.html": "http://cxc.harvard.edu/sherpa", "samp.documentation.url": "http://cxc.harvard.edu/sherpa" } cli = samp.SAMPIntegratedClient(metadata, addr='localhost') _fitting_tasks = [] _confidence_tasks = [] def _sig_handler(signum, frame): info("Got termination signal") if cli is not None: try: cli.disconnect() except Exception as e: warn("Could not disconnect cleanly") logging.exception(e) if signum == signal.SIGINT: info("Signal is SIGINT")