Skip to content

Commit e6ecaa5

Browse files
committed
Add API endpoint to return inbound and outbound connections
1 parent eebbcf7 commit e6ecaa5

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

src/api.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
from debug import logger
9292
from helper_sql import SqlBulkExecute, sqlExecute, sqlQuery, sqlStoredProcedure, sql_ready
9393
from inventory import Inventory
94+
from network import BMConnectionPool
9495
from network.threads import StoppableThread
9596
from six.moves import queue
9697
from version import softwareVersion
@@ -1441,6 +1442,33 @@ def HandleClientStatus(self):
14411442
'softwareVersion': softwareVersion
14421443
}
14431444

1445+
@command('listConnections')
1446+
def HandleListConnections(self):
1447+
"""
1448+
Returns bitmessage connection information as dict with keys *inbound,
1449+
*outbound.
1450+
"""
1451+
inboundConnections = []
1452+
outboundConnections = []
1453+
for i in BMConnectionPool().inboundConnections.values():
1454+
inboundConnections.append({
1455+
'host': i.destination.host,
1456+
'port': i.destination.port,
1457+
'fullyEstablished': i.fullyEstablished,
1458+
'userAgent': str(i.userAgent)
1459+
})
1460+
for i in BMConnectionPool().outboundConnections.values():
1461+
outboundConnections.append({
1462+
'host': i.destination.host,
1463+
'port': i.destination.port,
1464+
'fullyEstablished': i.fullyEstablished,
1465+
'userAgent': str(i.userAgent)
1466+
})
1467+
return {
1468+
'inbound': inboundConnections,
1469+
'outbound': outboundConnections
1470+
}
1471+
14441472
@command('helloWorld')
14451473
def HandleHelloWorld(self, a, b):
14461474
"""Test two string params"""

src/tests/test_api.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ def test_clientstatus_consistency(self):
157157
else:
158158
self.assertGreater(status["networkConnections"], 0)
159159

160+
def test_listconnections_consistency(self):
161+
"""Checking the return of API command 'listConnections'"""
162+
result = json.loads(self.api.listConnections())
163+
self.assertGreaterEqual(len(result["inbound"]), 0)
164+
self.assertGreaterEqual(len(result["outbound"]), 0)
165+
160166
def test_list_addresses(self):
161167
"""Checking the return of API command 'listAddresses'"""
162168
self.assertEqual(

0 commit comments

Comments
 (0)