~george-edison55/audataserver-proxy/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#! /usr/bin/python

'''
Proxy server for providing local searches to the Unity lens
'''

# Copyright (c) 2011, Nathan Osman <nathan.osman@gmail.com>
# Copyright (c) 2011, Stefano Palazzo <stefano.palazzo@gmail.com>

# 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 3 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, see <http://www.gnu.org/licenses/>.

# On Debian systems, the complete text of the GNU General
# Public License version 3 can be found in "/usr/share/common-licenses/GPL-3".


from audataserver import threaded_aulibrary
from getopt       import getopt, GetoptError
from json         import dumps
from os           import fork, getpid, remove
from sys          import argv, exit, stderr
import cherrypy


class DataServerApp (object):
    
    @cherrypy.expose
    def index(self):
        return "[Instructions and configuration go here.]"
    
    @cherrypy.expose
    def similar(self, title):
        cherrypy.response.headers["Content-Type"] = "application/json"
        return json.dumps(cherrypy.engine.data_manager.search(title)).encode()


class DataManager (cherrypy.process.plugins.SimplePlugin):
    
    data_dump = '/opt/se-data-dumps/askubuntu.ddi'
    
    def start(self):
        cherrypy.log('\33[33mSpawning data manager thread...\33[m')
        self.data_manager = threaded_aulibrary(self.data_dump)
    
    def stop(self):
        cherrypy.log('\33[33mShutting down data manager thread...\33[m')
        self.data_manager.shutdown()
        cherrypy.log('\33[32mData manager thread terminated.\33[m')
    
    def search(self, search_terms):
        return self.data_manager.search(search_terms)


# This program is run as a daemon, so we need to store
# our process ID in a file - this will be specified on the
# command line by the init.d script when applicable.
try:
    options, args = getopt(argv[1:], 'p:q', ['pid-file=', 'quiet'])
except GetoptError as e:
    print >> stderr, str(e)
    exit(1)

# Parse the command line options
pid_filename = None
quiet        = False
for option, value in options:
    if option in ['-p', '--pid-file']:
        pid_filename = value
    if option in ['-q', '--quiet']:
        quiet = True

if not pid_filename is None:
    # Check to see if the file exists
    # We have encountered an error if it does
    try:
        pid_file = open(pid_filename, 'w')
        if fork():
            exit(0)
        pid_file.write(str(getpid()))
        pid_file.close()
    except IOError as e:
        print >> stderr, '''The file %s could not be opened for writing. This likely indicates that the application is already running. If this is not the case, please delete the lock file.''' % pid_filename
        exit(1)

cherrypy.engine.data_manager = DataManager(cherrypy.engine)
cherrypy.quickstart(DataServerApp(), config={'global': {
    'tools.encode.encoding':  'utf-8',
    'server.socket_host':     '0.0.0.0',
    'server.socket_port':     19115,
    'log.screen':             not quiet,
    'engine.data_manager.on': True,
}})

# If we were given a command line parameter to store the PID
# of this process somewhere, we need to delete it afterward.
if not pid_filename is None:
    remove(pid_filename)