Skip to content

Commit bd5bdda

Browse files
committed
Moved class singleAPI to api module and import api below config check
1 parent ef54bca commit bd5bdda

File tree

2 files changed

+75
-68
lines changed

2 files changed

+75
-68
lines changed

src/api.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
"""
99

1010
import base64
11+
import errno
1112
import hashlib
1213
import json
14+
import random # nosec
15+
import socket
16+
import subprocess
17+
import threading
1318
import time
1419
from binascii import hexlify, unhexlify
1520
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer
@@ -23,6 +28,7 @@
2328
import defaults
2429
import helper_inbox
2530
import helper_sent
31+
import helper_threading
2632

2733
import state
2834
import queues
@@ -60,6 +66,68 @@ def serve_forever(self):
6066
self.handle_request()
6167

6268

69+
# This thread, of which there is only one, runs the API.
70+
class singleAPI(threading.Thread, helper_threading.StoppableThread):
71+
def __init__(self):
72+
threading.Thread.__init__(self, name="singleAPI")
73+
self.initStop()
74+
75+
def stopThread(self):
76+
super(singleAPI, self).stopThread()
77+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
78+
try:
79+
s.connect((
80+
BMConfigParser().get('bitmessagesettings', 'apiinterface'),
81+
BMConfigParser().getint('bitmessagesettings', 'apiport')
82+
))
83+
s.shutdown(socket.SHUT_RDWR)
84+
s.close()
85+
except:
86+
pass
87+
88+
def run(self):
89+
port = BMConfigParser().getint('bitmessagesettings', 'apiport')
90+
try:
91+
from errno import WSAEADDRINUSE
92+
except (ImportError, AttributeError):
93+
errno.WSAEADDRINUSE = errno.EADDRINUSE
94+
for attempt in range(50):
95+
try:
96+
if attempt > 0:
97+
port = random.randint(32767, 65535)
98+
se = StoppableXMLRPCServer(
99+
(BMConfigParser().get(
100+
'bitmessagesettings', 'apiinterface'),
101+
port),
102+
MySimpleXMLRPCRequestHandler, True, True)
103+
except socket.error as e:
104+
if e.errno in (errno.EADDRINUSE, errno.WSAEADDRINUSE):
105+
continue
106+
else:
107+
if attempt > 0:
108+
BMConfigParser().set(
109+
"bitmessagesettings", "apiport", str(port))
110+
BMConfigParser().save()
111+
break
112+
se.register_introspection_functions()
113+
114+
apiNotifyPath = BMConfigParser().safeGet(
115+
'bitmessagesettings', 'apinotifypath')
116+
117+
if apiNotifyPath:
118+
logger.info('Trying to call %s', apiNotifyPath)
119+
try:
120+
subprocess.call([apiNotifyPath, "startingUp"])
121+
except OSError:
122+
logger.warning(
123+
'Failed to call %s, removing apinotifypath setting',
124+
apiNotifyPath)
125+
BMConfigParser().remove_option(
126+
'bitmessagesettings', 'apinotifypath')
127+
128+
se.serve_forever()
129+
130+
63131
# This is one of several classes that constitute the API
64132
# This class was written by Vaibhav Bhatia.
65133
# Modified by Jonathan Warren (Atheros).

src/bitmessagemain.py

Lines changed: 7 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,20 @@
2020
import depends
2121
depends.check_dependencies()
2222

23+
import ctypes
24+
import getopt
2325
# Used to capture a Ctrl-C keypress so that Bitmessage can shutdown gracefully.
2426
import signal
25-
# The next 3 are used for the API
26-
from singleinstance import singleinstance
27-
import errno
2827
import socket
29-
import ctypes
28+
from datetime import datetime
3029
from struct import pack
3130
from subprocess import call
3231
from time import sleep
33-
from random import randint
34-
import getopt
3532

36-
from api import MySimpleXMLRPCRequestHandler, StoppableXMLRPCServer
3733
from helper_startup import (
3834
isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections
3935
)
36+
from singleinstance import singleinstance
4037

4138
import defaults
4239
import shared
@@ -65,7 +62,6 @@
6562
from network.downloadthread import DownloadThread
6663

6764
# Helper Functions
68-
import helper_bootstrap
6965
import helper_generic
7066
import helper_threading
7167

@@ -155,53 +151,6 @@ def inet_pton(family, host):
155151
socket.IPV6_V6ONLY = 27
156152

157153

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-
205154
# This is a list of current connections (the thread pointers at least)
206155
selfInitiatedConnections = {}
207156

@@ -338,19 +287,9 @@ def start(self):
338287
shared.reloadBroadcastSendersForWhichImWatching()
339288

340289
# 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()
354293
# close the main program even if there are threads left
355294
singleAPIThread.daemon = True
356295
singleAPIThread.start()

0 commit comments

Comments
 (0)