#!/usr/bin/env python
#
# Copyright (C) 2014-2016  Leo Singer
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
"""
Rudimentary GCN server, for testing purposes. Serves just one connection at a
time, and repeats the same payloads in order, repeating, for each connection.
"""
__author__ = "Leo Singer <leo.singer@ligo.org>"

# Imports
import argparse
import logging
import gcn
from gcn.cmdline import HostPortAction

# Command line interface
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--host', dest='addr',
                    default='127.0.0.1:8099', action=HostPortAction,
                    help='Server host and port (default: %(default)s)')
parser.add_argument('--retransmit-timeout', '-t', metavar='SECONDS',
                    type=int, default=1,
                    help='Delay between packets (default: %(default)s)')
parser.add_argument('payloads', nargs='+', metavar='PAYLOAD.xml')
parser.add_argument('--version', action='version',
                    version='pygcn ' + gcn.__version__)
args = parser.parse_args()

# Set up logger
logging.basicConfig(level=logging.INFO)

# Serve GCN notices (until interrupted or killed)
gcn.serve(args.payloads, host=args.addr.host, port=args.addr.port,
          retransmit_timeout=args.retransmit_timeout)
