summaryrefslogtreecommitdiffstatsabout
path: root/thinblue.py
blob: 89d3f8b41d58190eb36c1fb08250cf63e961ef46 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
##########################################################################
# ThinBlueWeb writen by MarioDebian <mario.izquierdo@thinetic.es>
#
#    Python-ThinBlue              
#
# Copyright (c) 2009 Mario Izquierdo <mario.izquierdo@thinetic.es>
#
# 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, 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., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
###########################################################################

import os
import sys
import getopt
import time

import threading

import thinblue.config

if "--debug" in sys.argv:
    thinblue.config.debug = True

if "--nodaemon" in sys.argv:
    thinblue.config.debug = True
    thinblue.config.daemon = False
else:
    thinblue.config.daemon = True


if "--help" in sys.argv:
    print """
thinblue
        --start    - start daemon
        --stop     - stop daemon
        --status   - return status (1 running 0 stoped)
        
        --debug    - enable debug logging
        --nodaemon - no fork process
"""
    sys.exit(0)

def drop_privileges():
    #    >>> for u in pwd.getpwall():
    #...   if u[0]=="www-data":
    #...       print u
    #... 
    #('www-data', 'x', 33, 33, 'www-data', '/var/www', '/bin/sh')
    # get www-data user id
    pass


import thinblue
thinblue.init()

import thinblue.logger as lg

def run():
    import thinblue.search
    #import thinblue.send
    import thinblue.obexftpsend

    # device updater thread
    import thinblue.update_device

    thinblue.load_devices()
    
    lg.info("init threads...", __name__)
    
    th_search = threading.Thread(target = thinblue.search.main, verbose = thinblue.config.thread_verbose)
    th_search.start()
    
    th_send = threading.Thread(target = thinblue.obexftpsend.main, verbose = thinblue.config.thread_verbose)
    th_send.start()

    th_update = threading.Thread(target = thinblue.update_device.main, verbose = thinblue.config.thread_verbose)
    th_update.start()
    
    lg.info("threads started...", __name__)
    
    while not thinblue.is_stoping(dosql = True):
        try:
            time.sleep(2)
        except KeyboardInterrupt:
            lg.info("KeyboardInterrupt, exiting...", __name__)
            break
    thinblue.do_stop()
    lg.info("stopper::exiting... PLEASE WAIT....", __name__)


if __name__ == '__main__':
    try:
        OPTS, ARGS = getopt.getopt(sys.argv[1:], ":hd", ["help", "debug", "start", "stop", "status", "nodaemon"])
    except getopt.error, msg:
        print msg
        print "for command line options use thinblue --help"
        sys.exit(2)
    
    for o, a in OPTS:
        if o == "--start":
            lg.old_stderr=sys.stderr
            lg.old_stdout=sys.stdout
            if not "--nodaemon" in sys.argv:
                import thinblue.daemonize
                thinblue.daemonize.start_server()
                sys.stderr = lg.stderr()
                sys.stdout = lg.stdout()
            lg.debug("run now", __name__)
            run()
        
        elif o == "--stop":
            # set stoping
            thinblue.do_stop()
            # read PID
            if not os.path.isfile(thinblue.config.DAEMON_PID_FILE):
                sys.exit(0)
            # wait for pid if exists
            pid = open(thinblue.config.DAEMON_PID_FILE, 'r').read().strip()
            print("waiting for pid %s"%pid)
            while os.path.isdir("/proc/%s"%pid):
                #print ( " wating for pid %s... "%pid )
                time.sleep(0.5)
            os.remove(thinblue.config.DAEMON_PID_FILE)
        
        elif o == "--status":
            pass