#!/usr/bin/python3
# -*- coding: utf-8; -*-
#
# (c) 2004-2007 Linbox / Free&ALter Soft, http://linbox.com
# (c) 2007-2009 Mandriva, http://www.mandriva.com
#
# $Id$
#
# This file is part of Mandriva Management Console (MMC).
#
# MMC 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.
#
# MMC 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 MMC.  If not, see <http://www.gnu.org/licenses/>.


import sys
import socket
import time
import os.path
from twisted.web.xmlrpc import Proxy
from twisted.web.client import downloadPage
from twisted.internet import reactor
from twisted.internet.task import LoopingCall

def usage():
    print """\
This little tool asks to a Pulse Package Server its packages list every 5
seconds.

Usage: pserver-plist <url>

Example: pserver-plist http://192.168.0.1:9990
"""
    sys.exit(1)

class Benchmark:

    def __init__(self, URL, sleep):
        self.url = URL
        self.sleep = sleep
        self.mapi = Proxy('%s/rpc' % self.url)
        self.inprogress = 0

    def schedule(self):
        LoopingCall(self.start).start(SLEEP)

    def start(self):
        self.mapi.callRemote('getMirror', {'uuid' : '0'}).addCallbacks(self.getPackageAPI, self.onError)

    def getPackageAPI(self, mirror):
        self.mirror = Proxy('%s://%s:%s%s' % (mirror['protocol'], mirror['server'], mirror['port'], mirror['mountpoint']))
        self.mapi.callRemote('getApiPackage', {'uuid' : '0'}).addCallbacks(self.getPackages, self.onError)

    def getPackages(self, papis):
        self.papis = papis
        self.papi = Proxy('%s%s' % (self.url, self.papis[0]['mountpoint']))
        self.papi.callRemote('getAllPackages', {'uuid' : '0'}).addCallbacks(self.startBenchLoop, self.onError)

    def startBenchLoop(self, packages):
        if not packages:
            print "Package server has no package"
        for pkg in packages:
            print pkg['id']

    def onError(self, error):
        print 'error', error
        reactor.stop()

if __name__ == '__main__':
    if len(sys.argv) != 2:
        usage()

    URL = sys.argv[1]
    SLEEP = 5
    benchmark = Benchmark(URL, SLEEP)
    benchmark.schedule()
    reactor.run()
