|
20 | 20 | import depends
|
21 | 21 | depends.check_dependencies()
|
22 | 22 |
|
| 23 | +import ctypes |
| 24 | +import getopt |
23 | 25 | # Used to capture a Ctrl-C keypress so that Bitmessage can shutdown gracefully.
|
24 | 26 | import signal
|
25 |
| -# The next 3 are used for the API |
26 |
| -from singleinstance import singleinstance |
27 |
| -import errno |
28 | 27 | import socket
|
29 |
| -import ctypes |
| 28 | +from datetime import datetime |
30 | 29 | from struct import pack
|
31 | 30 | from subprocess import call
|
32 | 31 | from time import sleep
|
33 |
| -from random import randint |
34 |
| -import getopt |
35 | 32 |
|
36 |
| -from api import MySimpleXMLRPCRequestHandler, StoppableXMLRPCServer |
37 | 33 | from helper_startup import (
|
38 | 34 | isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections
|
39 | 35 | )
|
| 36 | +from singleinstance import singleinstance |
40 | 37 |
|
41 | 38 | import defaults
|
42 | 39 | import shared
|
|
65 | 62 | from network.downloadthread import DownloadThread
|
66 | 63 |
|
67 | 64 | # Helper Functions
|
68 |
| -import helper_bootstrap |
69 | 65 | import helper_generic
|
70 | 66 | import helper_threading
|
71 | 67 |
|
@@ -155,53 +151,6 @@ def inet_pton(family, host):
|
155 | 151 | socket.IPV6_V6ONLY = 27
|
156 | 152 |
|
157 | 153 |
|
158 |
| -# This thread, of which there is only one, runs the API. |
159 |
| -class singleAPI(threading.Thread, helper_threading.StoppableThread): |
160 |
| - def __init__(self): |
161 |
| - threading.Thread.__init__(self, name="singleAPI") |
162 |
| - self.initStop() |
163 |
| - |
164 |
| - def stopThread(self): |
165 |
| - super(singleAPI, self).stopThread() |
166 |
| - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
167 |
| - try: |
168 |
| - s.connect(( |
169 |
| - BMConfigParser().get('bitmessagesettings', 'apiinterface'), |
170 |
| - BMConfigParser().getint('bitmessagesettings', 'apiport') |
171 |
| - )) |
172 |
| - s.shutdown(socket.SHUT_RDWR) |
173 |
| - s.close() |
174 |
| - except: |
175 |
| - pass |
176 |
| - |
177 |
| - def run(self): |
178 |
| - port = BMConfigParser().getint('bitmessagesettings', 'apiport') |
179 |
| - try: |
180 |
| - from errno import WSAEADDRINUSE |
181 |
| - except (ImportError, AttributeError): |
182 |
| - errno.WSAEADDRINUSE = errno.EADDRINUSE |
183 |
| - for attempt in range(50): |
184 |
| - try: |
185 |
| - if attempt > 0: |
186 |
| - port = randint(32767, 65535) |
187 |
| - se = StoppableXMLRPCServer( |
188 |
| - (BMConfigParser().get( |
189 |
| - 'bitmessagesettings', 'apiinterface'), |
190 |
| - port), |
191 |
| - MySimpleXMLRPCRequestHandler, True, True) |
192 |
| - except socket.error as e: |
193 |
| - if e.errno in (errno.EADDRINUSE, errno.WSAEADDRINUSE): |
194 |
| - continue |
195 |
| - else: |
196 |
| - if attempt > 0: |
197 |
| - BMConfigParser().set( |
198 |
| - "bitmessagesettings", "apiport", str(port)) |
199 |
| - BMConfigParser().save() |
200 |
| - break |
201 |
| - se.register_introspection_functions() |
202 |
| - se.serve_forever() |
203 |
| - |
204 |
| - |
205 | 154 | # This is a list of current connections (the thread pointers at least)
|
206 | 155 | selfInitiatedConnections = {}
|
207 | 156 |
|
@@ -338,19 +287,9 @@ def start(self):
|
338 | 287 | shared.reloadBroadcastSendersForWhichImWatching()
|
339 | 288 |
|
340 | 289 | # API is also objproc dependent
|
341 |
| - if BMConfigParser().safeGetBoolean( |
342 |
| - 'bitmessagesettings', 'apienabled'): |
343 |
| - try: |
344 |
| - apiNotifyPath = BMConfigParser().get( |
345 |
| - 'bitmessagesettings', 'apinotifypath') |
346 |
| - except: |
347 |
| - apiNotifyPath = '' |
348 |
| - if apiNotifyPath != '': |
349 |
| - with shared.printLock: |
350 |
| - print('Trying to call', apiNotifyPath) |
351 |
| - |
352 |
| - call([apiNotifyPath, "startingUp"]) |
353 |
| - singleAPIThread = singleAPI() |
| 290 | + if BMConfigParser().safeGetBoolean('bitmessagesettings', 'apienabled'): |
| 291 | + import api |
| 292 | + singleAPIThread = api.singleAPI() |
354 | 293 | # close the main program even if there are threads left
|
355 | 294 | singleAPIThread.daemon = True
|
356 | 295 | singleAPIThread.start()
|
|
0 commit comments