Moved class singleAPI to api module and import api
below config check
This commit is contained in:
parent
ef54bcaf82
commit
bd5bdda009
68
src/api.py
68
src/api.py
|
@ -8,8 +8,13 @@ This is not what you run to run the Bitmessage API. Instead, enable the API
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import base64
|
import base64
|
||||||
|
import errno
|
||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
|
import random # nosec
|
||||||
|
import socket
|
||||||
|
import subprocess
|
||||||
|
import threading
|
||||||
import time
|
import time
|
||||||
from binascii import hexlify, unhexlify
|
from binascii import hexlify, unhexlify
|
||||||
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer
|
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer
|
||||||
|
@ -23,6 +28,7 @@ from bmconfigparser import BMConfigParser
|
||||||
import defaults
|
import defaults
|
||||||
import helper_inbox
|
import helper_inbox
|
||||||
import helper_sent
|
import helper_sent
|
||||||
|
import helper_threading
|
||||||
|
|
||||||
import state
|
import state
|
||||||
import queues
|
import queues
|
||||||
|
@ -60,6 +66,68 @@ class StoppableXMLRPCServer(SimpleXMLRPCServer):
|
||||||
self.handle_request()
|
self.handle_request()
|
||||||
|
|
||||||
|
|
||||||
|
# This thread, of which there is only one, runs the API.
|
||||||
|
class singleAPI(threading.Thread, helper_threading.StoppableThread):
|
||||||
|
def __init__(self):
|
||||||
|
threading.Thread.__init__(self, name="singleAPI")
|
||||||
|
self.initStop()
|
||||||
|
|
||||||
|
def stopThread(self):
|
||||||
|
super(singleAPI, self).stopThread()
|
||||||
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
try:
|
||||||
|
s.connect((
|
||||||
|
BMConfigParser().get('bitmessagesettings', 'apiinterface'),
|
||||||
|
BMConfigParser().getint('bitmessagesettings', 'apiport')
|
||||||
|
))
|
||||||
|
s.shutdown(socket.SHUT_RDWR)
|
||||||
|
s.close()
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
port = BMConfigParser().getint('bitmessagesettings', 'apiport')
|
||||||
|
try:
|
||||||
|
from errno import WSAEADDRINUSE
|
||||||
|
except (ImportError, AttributeError):
|
||||||
|
errno.WSAEADDRINUSE = errno.EADDRINUSE
|
||||||
|
for attempt in range(50):
|
||||||
|
try:
|
||||||
|
if attempt > 0:
|
||||||
|
port = random.randint(32767, 65535)
|
||||||
|
se = StoppableXMLRPCServer(
|
||||||
|
(BMConfigParser().get(
|
||||||
|
'bitmessagesettings', 'apiinterface'),
|
||||||
|
port),
|
||||||
|
MySimpleXMLRPCRequestHandler, True, True)
|
||||||
|
except socket.error as e:
|
||||||
|
if e.errno in (errno.EADDRINUSE, errno.WSAEADDRINUSE):
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
if attempt > 0:
|
||||||
|
BMConfigParser().set(
|
||||||
|
"bitmessagesettings", "apiport", str(port))
|
||||||
|
BMConfigParser().save()
|
||||||
|
break
|
||||||
|
se.register_introspection_functions()
|
||||||
|
|
||||||
|
apiNotifyPath = BMConfigParser().safeGet(
|
||||||
|
'bitmessagesettings', 'apinotifypath')
|
||||||
|
|
||||||
|
if apiNotifyPath:
|
||||||
|
logger.info('Trying to call %s', apiNotifyPath)
|
||||||
|
try:
|
||||||
|
subprocess.call([apiNotifyPath, "startingUp"])
|
||||||
|
except OSError:
|
||||||
|
logger.warning(
|
||||||
|
'Failed to call %s, removing apinotifypath setting',
|
||||||
|
apiNotifyPath)
|
||||||
|
BMConfigParser().remove_option(
|
||||||
|
'bitmessagesettings', 'apinotifypath')
|
||||||
|
|
||||||
|
se.serve_forever()
|
||||||
|
|
||||||
|
|
||||||
# This is one of several classes that constitute the API
|
# This is one of several classes that constitute the API
|
||||||
# This class was written by Vaibhav Bhatia.
|
# This class was written by Vaibhav Bhatia.
|
||||||
# Modified by Jonathan Warren (Atheros).
|
# Modified by Jonathan Warren (Atheros).
|
||||||
|
|
|
@ -20,23 +20,20 @@ sys.path.insert(0, app_dir)
|
||||||
import depends
|
import depends
|
||||||
depends.check_dependencies()
|
depends.check_dependencies()
|
||||||
|
|
||||||
|
import ctypes
|
||||||
|
import getopt
|
||||||
# Used to capture a Ctrl-C keypress so that Bitmessage can shutdown gracefully.
|
# Used to capture a Ctrl-C keypress so that Bitmessage can shutdown gracefully.
|
||||||
import signal
|
import signal
|
||||||
# The next 3 are used for the API
|
|
||||||
from singleinstance import singleinstance
|
|
||||||
import errno
|
|
||||||
import socket
|
import socket
|
||||||
import ctypes
|
from datetime import datetime
|
||||||
from struct import pack
|
from struct import pack
|
||||||
from subprocess import call
|
from subprocess import call
|
||||||
from time import sleep
|
from time import sleep
|
||||||
from random import randint
|
|
||||||
import getopt
|
|
||||||
|
|
||||||
from api import MySimpleXMLRPCRequestHandler, StoppableXMLRPCServer
|
|
||||||
from helper_startup import (
|
from helper_startup import (
|
||||||
isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections
|
isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections
|
||||||
)
|
)
|
||||||
|
from singleinstance import singleinstance
|
||||||
|
|
||||||
import defaults
|
import defaults
|
||||||
import shared
|
import shared
|
||||||
|
@ -65,7 +62,6 @@ from network.addrthread import AddrThread
|
||||||
from network.downloadthread import DownloadThread
|
from network.downloadthread import DownloadThread
|
||||||
|
|
||||||
# Helper Functions
|
# Helper Functions
|
||||||
import helper_bootstrap
|
|
||||||
import helper_generic
|
import helper_generic
|
||||||
import helper_threading
|
import helper_threading
|
||||||
|
|
||||||
|
@ -155,53 +151,6 @@ def _fixSocket():
|
||||||
socket.IPV6_V6ONLY = 27
|
socket.IPV6_V6ONLY = 27
|
||||||
|
|
||||||
|
|
||||||
# This thread, of which there is only one, runs the API.
|
|
||||||
class singleAPI(threading.Thread, helper_threading.StoppableThread):
|
|
||||||
def __init__(self):
|
|
||||||
threading.Thread.__init__(self, name="singleAPI")
|
|
||||||
self.initStop()
|
|
||||||
|
|
||||||
def stopThread(self):
|
|
||||||
super(singleAPI, self).stopThread()
|
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
||||||
try:
|
|
||||||
s.connect((
|
|
||||||
BMConfigParser().get('bitmessagesettings', 'apiinterface'),
|
|
||||||
BMConfigParser().getint('bitmessagesettings', 'apiport')
|
|
||||||
))
|
|
||||||
s.shutdown(socket.SHUT_RDWR)
|
|
||||||
s.close()
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def run(self):
|
|
||||||
port = BMConfigParser().getint('bitmessagesettings', 'apiport')
|
|
||||||
try:
|
|
||||||
from errno import WSAEADDRINUSE
|
|
||||||
except (ImportError, AttributeError):
|
|
||||||
errno.WSAEADDRINUSE = errno.EADDRINUSE
|
|
||||||
for attempt in range(50):
|
|
||||||
try:
|
|
||||||
if attempt > 0:
|
|
||||||
port = randint(32767, 65535)
|
|
||||||
se = StoppableXMLRPCServer(
|
|
||||||
(BMConfigParser().get(
|
|
||||||
'bitmessagesettings', 'apiinterface'),
|
|
||||||
port),
|
|
||||||
MySimpleXMLRPCRequestHandler, True, True)
|
|
||||||
except socket.error as e:
|
|
||||||
if e.errno in (errno.EADDRINUSE, errno.WSAEADDRINUSE):
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
if attempt > 0:
|
|
||||||
BMConfigParser().set(
|
|
||||||
"bitmessagesettings", "apiport", str(port))
|
|
||||||
BMConfigParser().save()
|
|
||||||
break
|
|
||||||
se.register_introspection_functions()
|
|
||||||
se.serve_forever()
|
|
||||||
|
|
||||||
|
|
||||||
# This is a list of current connections (the thread pointers at least)
|
# This is a list of current connections (the thread pointers at least)
|
||||||
selfInitiatedConnections = {}
|
selfInitiatedConnections = {}
|
||||||
|
|
||||||
|
@ -338,19 +287,9 @@ class Main:
|
||||||
shared.reloadBroadcastSendersForWhichImWatching()
|
shared.reloadBroadcastSendersForWhichImWatching()
|
||||||
|
|
||||||
# API is also objproc dependent
|
# API is also objproc dependent
|
||||||
if BMConfigParser().safeGetBoolean(
|
if BMConfigParser().safeGetBoolean('bitmessagesettings', 'apienabled'):
|
||||||
'bitmessagesettings', 'apienabled'):
|
import api
|
||||||
try:
|
singleAPIThread = api.singleAPI()
|
||||||
apiNotifyPath = BMConfigParser().get(
|
|
||||||
'bitmessagesettings', 'apinotifypath')
|
|
||||||
except:
|
|
||||||
apiNotifyPath = ''
|
|
||||||
if apiNotifyPath != '':
|
|
||||||
with shared.printLock:
|
|
||||||
print('Trying to call', apiNotifyPath)
|
|
||||||
|
|
||||||
call([apiNotifyPath, "startingUp"])
|
|
||||||
singleAPIThread = singleAPI()
|
|
||||||
# close the main program even if there are threads left
|
# close the main program even if there are threads left
|
||||||
singleAPIThread.daemon = True
|
singleAPIThread.daemon = True
|
||||||
singleAPIThread.start()
|
singleAPIThread.start()
|
||||||
|
|
Reference in New Issue
Block a user