# 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 Library 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Modified by Dan Williams <dcbw@redhat.com>

import os, sys
import SSLCommon
import urllib
import xmlrpclib

__version__='0.12'

class PlgSSL_Transport(xmlrpclib.Transport):

    user_agent = "pyOpenSSL_XMLRPC/%s - %s" % (__version__, xmlrpclib.Transport.user_agent)

    def __init__(self, ssl_context):
        self.ssl_ctx=ssl_context

    def make_connection(self, host):
        # Handle username and password.
        try:
            host, extra_headers, x509 = self.get_host_info(host)
        except AttributeError:
            # Yay for Python 2.2
            pass
        _host, _port = urllib.splitport(host)
        return SSLCommon.PlgHTTPS(_host, int(_port), ssl_context=self.ssl_ctx)


class PlgXMLRPCServerProxy(xmlrpclib.ServerProxy):
    def __init__(self, uri, certs):
        if certs and len(certs) > 0:
            self.ctx = SSLCommon.CreateSSLContext(certs)
            xmlrpclib.ServerProxy.__init__(self, uri, PlgSSL_Transport(ssl_context=self.ctx))
        else:
            xmlrpclib.ServerProxy.__init__(self, uri)



###########################################################
# Testing stuff
###########################################################


import threading
import time
import random

client_start = False

_lock = threading.Lock()

class TestClient(threading.Thread):
    def __init__(self, certs):
        self.server = PlgXMLRPCServerProxy("https://127.0.0.1:8886", certs)
        threading.Thread.__init__(self)

    def run(self):
        while not client_start:
            time.sleep(0.05)
        i = 0
        while i < 5:
#            _lock.acquire()
            print self.server.ping()
            time.sleep(0.05)
#            _lock.release()
            i = i + 1


if __name__ == '__main__':
    if len(sys.argv) < 4:
        print "Usage: python XMLRPCServerProxy.py key_and_cert ca_cert peer_ca_cert"
        sys.exit(1)

    certs = {}
    certs['key_and_cert'] = sys.argv[1]
    certs['ca_cert'] = sys.argv[2]
    certs['peer_ca_cert'] = sys.argv[3]

    i = 100
    while i > 0:
        t = TestClient(certs)
        print "Created thread %d." % i
        t.start()
        i = i - 1

    time.sleep(3)
    print "Unleashing threads."
    client_start = True
    while True:
        try:
            time.sleep(0.25)
        except KeyboardInterrupt:
            os._exit(0)

