work on sockets

This commit is contained in:
Kevin Froman 2018-09-20 12:04:58 -05:00
parent 7baa7d5d5f
commit e826bca19e
5 changed files with 38 additions and 28 deletions

View File

@ -21,7 +21,8 @@
'''
import sys, os, core, config, json, requests, time, logger, threading, base64, onionr
import onionrexceptions, onionrpeers, onionrevents as events, onionrplugins as plugins, onionrblockapi as block
import onionrdaemontools, onionrsockets
import onionrdaemontools, onionrsockets, onionrchat
from dependencies import secrets
from defusedxml import minidom
class OnionrCommunicatorDaemon:
@ -80,7 +81,8 @@ class OnionrCommunicatorDaemon:
self.daemonTools = onionrdaemontools.DaemonTools(self)
# Active sockets for direct connections
self.sockets = []
self.sockets = {}
self.socketExchange = {} # Socket ID exchange
if debug or developmentMode:
OnionrCommunicatorTimers(self, self.heartbeat, 10)
@ -469,12 +471,27 @@ class OnionrCommunicatorDaemon:
# Create a socket or connect to one.
# The socket handler (such as the plugin or app using it) is specified in startData['reason]
startData = json.loads(cmd[1])
self.onionrsockets.append(onionrsockets.OnionrSockets(self._core, startData))
threading.Thread(target=self.startSocket, args=(startData,)).start()
else:
logger.info('Recieved daemonQueue command:' + cmd[0])
self.decrementThreadCount('daemonCommands')
def startSocket(self, startData):
# Start a socket client
mySocket = onionrsockets.OnionrSockets(self._core, startData))
self.sockets[mySocket.socketID] = mySocket
sockProgram = '' # Function for socket handler (application)
if startData['reason'] == 'chat':
sockProgram = onionrchat.OnionrChat
else:
del self.sockets[mySocket.socketID] # Delete socket if we have no handler for it
threading.Thread(target=sockProgram, args=(self, mySocket)).start()
mySocket.startConn()
def uploadBlock(self):
'''Upload our block to a few peers'''
# when inserting a block, we try to upload it to a few peers to add some deniability

View File

@ -79,7 +79,6 @@ class Core:
# Initialize the crypto object
self._crypto = onionrcrypto.OnionrCrypto(self)
self._blacklist = onionrblacklist.OnionrBlackList(self)
self.chatInst = onionrchat.OnionrChat(self)
except Exception as error:
logger.error('Failed to initialize core Onionr library.', error=error)

View File

@ -218,7 +218,7 @@ class Onionr:
'getpasswd': self.printWebPassword,
'get-passwd': self.printWebPassword,
'chat': self.onionrCore.chatInst.connect()
'chat': self.startChat(),
'friend': self.friendCmd
}
@ -270,6 +270,11 @@ class Onionr:
THIS SECTION HANDLES THE COMMANDS
'''
def startChat(self):
self.onionrCore.daemonQueueAdd()
socketInfo = json.dumps({'peer': api.data['signer'], 'address': address, 'port': port, 'create': True, 'reason': reason})
self.onionrCore.daemonQueueAdd('startSocket', socketInfo)
def getCommands(self):
return self.cmds

View File

@ -17,14 +17,14 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
import logger
import logger, time
class OnionrChat:
def __init__(self, coreInst):
return
def recieveMessage(self, socketInst, data):
logger.info('Got %s' % (data,))
return ''
def sendMessage(self, socketInst, data):
return "Hello"
def __init__(self, communicatorInst, socketInst):
self.communicator = communicatorInst
self.socket = socketInst
while True:
time.sleep(2)
logger.info(self.socket.readData())
self.socket.sendData('rekt')
return

View File

@ -23,18 +23,6 @@ import onionrexceptions, time, onionrchat
from dependencies import secrets
sel = selectors.DefaultSelector()
def getSocketCallbackRecieveHandler(coreInst, reason, create):
'''Return the recieve handler function for a given socket reason'''
retData = ''
if startData == 'chat':
retData = coreInst.chatInst.recieveMessage
def getSocketCallbackSendHandler(coreInst, reason, create):
'''Return the send handler function for a given socket reason'''
retData = ''
if startData == 'chat':
retData = coreInst.chatInst.sendMessage
class OnionrSockets:
def __init__(self, coreInst, socketInfo):
'''Create a new Socket object. This interface is named a bit misleadingly
@ -64,8 +52,9 @@ class OnionrSockets:
self.connected = False
self.readData = []
self.sendData = 0
self.sendData = 0
def startConn():
if self.isServer:
self.createServer()
else: