# -*- python -*-
#
# Apache mod_wsgi python interface
#
# Copyright (C) 2004-2006 The Trustees of Princeton University
#

import sys
sys.path.append('/usr/share/plc_api')
sys.stdout = sys.stderr
import traceback
from PLC.Debug import log
from PLC.API import PLCAPI

api = PLCAPI()

def application(environ, start_response):
    try:
        status = '200 OK'
        if environ.get('REQUEST_METHOD') != 'POST':
            content_type = 'text/html'
            output = """
<html><head>
<title>PLCAPI WSGI XML-RPC/SOAP Interface</title>
</head><body>
<h1>PLCAPI WSGI XML-RPC/SOAP Interface</h1>
<p>Please use XML-RPC or SOAP to access the PLCAPI.</p>
</body></html>
"""
        else:
            api.environ = environ
            content_type = 'text/xml'
            ip = environ.get('REMOTE_ADDR')
            port = environ.get('REMOTE_PORT')
            output = api.handle((ip,port),  environ.get('wsgi.input').read())
            # Shut down database connection, otherwise up to MaxClients DB
            # connections will remain open.
            api.db.close()
    except Exception, err:
        status = '500 Internal Server Error'
        content_type = 'text/html'
        output = 'Internal Server Error'
        print >> log, err, traceback.format_exc()

    # Write response
    response_headers = [('Content-type', '%s' % content_type),
                       ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    return [output] 


@@ -263,24 +263,8 @@
 PI%0A%0A
-api = PLCAPI()%0A%0A
 def 
@@ -662,16 +662,352 @@
   else:%0A
+            # Thomas Dreibholz %3Cdreibh@simula.no%3E%0A            # Note that this function is called within multiple threads!%0A            # %22api%22 MUST be a local variable instead of a global one.%0A            # Otherwise, this causes concurrent accesses to the same%0A            # object within different threads!%0A            api = PLCAPI()%0A
         

fix for WSGI-based PLCAPI, on behalf of Thomas Dreibholz