Commit fbb2148a authored by Alexander Pace's avatar Alexander Pace

Adding sample listener program

parent 0579d448
Pipeline #60130 passed with stages
in 5 minutes and 5 seconds
#!/usr/bin/env python
usage = "lvaler_listener"
description = "a framework application to listen to LVAlert using the \
author = "Alexander Pace ("
# Import the ligo.lvalert and some other helper libraries.
from ligo.lvalert import LVAlertClient
import json
import time
def main(client=None):
# This is the main listener program. Many of the options are
# hardcoded to try out, but you should get clever with input
# arguments, etc.
# Specify the LVAlert server:
#SERVER = ''
#SERVER = ''
# Specify your username. This is an optional step since by
# default LVAlert should be able to pull the username from
# the .netrc file.
USERNAME = 'albert.einstein'
# Initialize the sleekxmpp session. Here we're only specifying
# the username and server, but the LVAlert documentation has the
# full set of inputs.
client = LVAlertClient(server=SERVER,username=USERNAME)
# Define the nodes to listen to. There's a check later on in the
# code that will get the list of nodes, and then check that the node
# exists, and then subscribe to the node.
NODES = ['lvalert-playground_monitor_ping', 'cbc_gstlal']
# Start the main listening loop. Note: this in a try:..except:
# block that detects SystemExits and KeyboardInterrupt. This gives
# the listener the opportunity to abort() the XMPP connection and
# shutdown a little more elegantly.
print("Connecting to: %s" % SERVER)
if client.connect():
print("Connected to: %s" % SERVER)
# Get nodes and then subscribe to the requested nodes:
lvalert_nodes = client.get_nodes()
for node in NODES:
if node in lvalert_nodes:
print("Subscribed to node: %s" % node)
print("No node %s on %s" % (node, SERVER))
# Start the "on_alert" listener class:
# Listen for alerts. Note: on the line below, the 'process_alert'
# method is part of the 'on_alert' class.
# Loop indefnitely and wait for messages.
while True: time.sleep(5)
except (KeyboardInterrupt, SystemExit):
# Kill the client upon exiting the loop:
print("Disconnecting from: %s" % SERVER)
# callback class to respond to lvalert messages
class on_alert(object):
#--- Initialize ---#
def __init__(self):
# This is where you initalize stuff for the on_alert class.
# Get creative with passing in options when you initialize it!
# You can pass around data from method to method with the "self.*"
# variable.
print("Initialized on_alert class.")
self.ID = "This computer"
#--- Injest an alert ---#
def process_alert(self, node=None, payload=None):
# This is what gets called when an alert is received. The node name
# that received the alert is creatively called "node" and the payload
# is called "payload". Note that alerts coming from GraceDB are going
# to be JSON-formatted. So, to do work with the data in the alert, there
# needs to be a json.loads().
# Note: I'm putting everything in a try:..except:... block. The reasons
# are two-fold: 1) to actually trap and print and print the traceback for
# debugging purposes, and 2) to prevent the XMPP stream from ingesting the
# error and breaking subscriptions. Extra note: this shouldn't be an issue
# when clients are running ligo-lvalert-1.5.6, but I'm just a worrier, I
# guess. That's why my friends call me whiskers.
print("%s recieved an alert from node %s" % (self.ID, node))
self.alert_data = json.loads(payload)
# Do something with the LVAlert data:
self.gid = self.alert_data["uid"]
print("Alert came from event ID: %s" % self.gid)
except Exception as e: print(e)
if __name__ == '__main__':
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment