diff --git a/onionr/api.py b/onionr/api.py index 5b63619f..e4a0b5c8 100755 --- a/onionr/api.py +++ b/onionr/api.py @@ -138,7 +138,13 @@ class API: resp = Response(self._utils.exportMyPubkey()) # setData should be something the communicator initiates, not this api elif action == 'getData': - resp = Response(self._core.getData(data)) + resp = self._core.getData(data) + if resp == False: + abort(404) + resp = "" + resp = Response(resp) + else: + resp = Response("") return resp diff --git a/onionr/communicator.py b/onionr/communicator.py index c167a560..c646ac7c 100755 --- a/onionr/communicator.py +++ b/onionr/communicator.py @@ -31,6 +31,8 @@ class OnionrCommunicate: self._utils = onionrutils.OnionrUtils(self._core) blockProcessTimer = 0 blockProcessAmount = 5 + heartBeatTimer = 0 + heartBeatRate = 10 logger.debug('Communicator debugging enabled.') torID = open('data/hs/hostname').read() @@ -45,14 +47,17 @@ class OnionrCommunicate: self._core.clearDaemonQueue() while True: command = self._core.daemonQueue() - # Process blocks based on a timer blockProcessTimer += 1 + heartBeatTimer += 1 + if heartBeatRate == heartBeatTimer: + logger.debug('Communicator heartbeat') + heartBeatTimer = 0 if blockProcessTimer == blockProcessAmount: self.lookupBlocks() self._core.processBlocks() blockProcessTimer = 0 - logger.debug('Communicator daemon heartbeat') + #logger.debug('Communicator daemon heartbeat') if command != False: if command[0] == 'shutdown': logger.warn('Daemon recieved exit command.') diff --git a/onionr/core.py b/onionr/core.py index 711bf803..0df2a698 100644 --- a/onionr/core.py +++ b/onionr/core.py @@ -45,6 +45,8 @@ class Core: if not os.path.exists('data/'): os.mkdir('data/') + if not os.path.exists('data/blocks/'): + os.mkdir('data/blocks/') if not os.path.exists(self.blockDB): self.createBlockDB() @@ -142,9 +144,12 @@ class Core: def getData(self,hash): '''simply return the data associated to a hash''' - dataFile = open(self.blockDataLocation + hash + '.dat') - data = dataFile.read() - dataFile.close() + try: + dataFile = open(self.blockDataLocation + hash + '.dat') + data = dataFile.read() + dataFile.close() + except FileNotFoundError: + data = False return data def setData(self, data): @@ -158,7 +163,7 @@ class Core: raise Exception("Data is already set for " + dataHash) else: blockFile = open(blockFileName, 'w') - blockFile.write(data) + blockFile.write(data.decode()) blockFile.close() return dataHash