comparison old/server/utils.py @ 203:11a1b59b0624

Move old stuff to its own place
author Matt Johnston <matt@ucc.asn.au>
date Sun, 30 Mar 2014 20:21:56 +0800
parents server/utils.py@51d889ad39a3
children
comparison
equal deleted inserted replaced
202:6dd157a12035 203:11a1b59b0624
1 import os
2 import sys
3 #import ctypes
4 import time
5 import select
6 import logging
7
8 L = logging.info
9 W = logging.warning
10 E = logging.error
11
12 DEFAULT_TRIES = 3
13 READLINE_SELECT_TIMEOUT = 1
14
15 __all__ = ('monotonic_time', 'retry')
16
17 clock_gettime = None
18 no_clock_gettime = True
19 def monotonic_time():
20 global clock_gettime
21 global no_clock_gettime
22 if no_clock_gettime:
23 return time.time()
24
25 class timespec(ctypes.Structure):
26 _fields_ = [
27 ('tv_sec', ctypes.c_long),
28 ('tv_nsec', ctypes.c_long)
29 ]
30 if not clock_gettime:
31 try:
32 librt = ctypes.CDLL('librt.so.0', use_errno=True)
33 clock_gettime = librt.clock_gettime
34 clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)]
35 except:
36 W("No clock_gettime(), using fake fallback.")
37 no_clock_gettime = True
38 return time.time()
39
40 t = timespec()
41 CLOCK_MONOTONIC = 1 # see <linux/time.h>
42
43 if clock_gettime(CLOCK_MONOTONIC, ctypes.pointer(t)) != 0:
44 errno_ = ctypes.get_errno()
45 raise OSError(errno_, os.strerror(errno_))
46 return t.tv_sec + t.tv_nsec * 1e-9
47
48 # decorator, tries a number of times, returns None on failure, sleeps between
49 # Must be used as "@retry()" if arguments are defaulted
50 def retry(retries=DEFAULT_TRIES, try_time = 1):
51 def inner(func):
52 def new_f(*args, **kwargs):
53 for i in range(retries):
54 d = func(*args, **kwargs)
55 if d is not None:
56 return d
57 time.sleep(try_time)
58 return None
59
60 new_f.func_name = func.func_name
61 return new_f
62 return inner
63
64 def readline(sock):
65 timeout = READLINE_SELECT_TIMEOUT
66 buf = ''
67 while True:
68 (rlist, wlist, xlist) = select.select([sock], [], [], timeout)
69 if sock not in rlist:
70 # hit timeout
71 return None
72
73 c = sock.recv(1)
74 if c == '':
75 # lightblue timeout
76 return None
77 if c == '\r':
78 continue
79
80 buf += c
81 if c == '\n':
82 return buf
83
84 # from http://blog.stalkr.net/2011/04/pctf-2011-32-thats-no-bluetooth.html
85 def crc16(buff, crc = 0, poly = 0x8408):
86 l = len(buff)
87 i = 0
88 while i < l:
89 ch = ord(buff[i])
90 uc = 0
91 while uc < 8:
92 if (crc & 1) ^ (ch & 1):
93 crc = (crc >> 1) ^ poly
94 else:
95 crc >>= 1
96 ch >>= 1
97 uc += 1
98 i += 1
99 return crc
100
101 def cheap_daemon():
102 L("Daemonising.")
103 sys.stdout.flush()
104 sys.stderr.flush()
105 out = file('/dev/null', 'a+')
106 os.dup2(out.fileno(), sys.stdout.fileno())
107 os.dup2(out.fileno(), sys.stderr.fileno())
108
109 try:
110 pid = os.fork()
111 if pid > 0:
112 sys.exit(0)
113 except OSError, e:
114 E("Bad fork()")
115 sys.exit(1)
116
117 os.setsid()
118
119 try:
120 pid = os.fork()
121 if pid > 0:
122 sys.exit(0)
123 except OSError, e:
124 E("Bad fork()")
125 sys.exit(1)
126
127