Skip to content

Commit

Permalink
web/core - fixed socket communication for actor status
Browse files Browse the repository at this point in the history
  • Loading branch information
superstes committed Nov 13, 2021
1 parent 059d450 commit b03958a
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 20 deletions.
2 changes: 1 addition & 1 deletion code/core/config/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def init():

CRYPTO_RECOGNITION_TEXT = '#crypto-recognition'
PATH_HOME_VENV = '/venv/bin' # home prepended
SOCKET_SHUFFLE = None # if unset AGENT.security will be evaluated
SOCKET_SHUFFLE = False # if unset SERVER.security will be evaluated
NONE_RESULTS = ['', 'None', None, ' ']
CONFIG_FILE_PATH = '/core/config/file/core.conf'
CENSOR_SYMBOL = '●'
Expand Down
35 changes: 33 additions & 2 deletions code/core/config/shared_init_web.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,43 @@
from inspect import getmembers, ismethod

from core.config import shared as config


class PseudoConfigObject:
pass


def _create_pseudo_config(source_obj):
attributes = {}

for attr in getmembers(source_obj):
if not attr[0].startswith('_'):
value = attr[1]

if not ismethod(value):
# transform django values to the ones we would expect to get from the db
# could be done in reverse (in the factory) in the future.. would probably be a cleaner way to handle this
if type(value) == bool:
if value:
value = 1
else:
value = 0

attributes[attr[0]] = value

obj = PseudoConfigObject()
for key, value in attributes.items():
setattr(obj, key, value)

return obj


def init(agent_obj, server_obj):
try:
_ = config.AGENT.path_root
_ = config.SERVER.security

except (AttributeError, NameError):
config.init()
config.AGENT = agent_obj
config.SERVER = server_obj
config.AGENT = _create_pseudo_config(agent_obj)
config.SERVER = _create_pseudo_config(server_obj)
11 changes: 6 additions & 5 deletions code/core/config/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
PACKAGE_SIZE = 1024
RECV_INTERVAL = 2
RECV_TIMEOUT = config.AGENT.subprocess_timeout + 2 # we will need to wait longer than any action-process could run so the result can be transmitted
NONE_RESULT = 'none'

if config.SOCKET_SHUFFLE or config.SERVER.security:
SHUFFLE = True
else:
SHUFFLE = False
SHUFFLE = config.SOCKET_SHUFFLE

# todo: will set shuffle to true even if security == 0 ?!?!
# if config.SERVER.security == 1:
# SHUFFLE = True

SHUFFLE_DATA = '80e7540fe29c6b88314a8083acf7110c'.encode('utf-8')

Expand All @@ -40,4 +42,3 @@
MATCH_DEVICE = f'{PATH_CORE}{SUB_PATH_DEVICE}.([0-9]{{1,10}}).(.*?)$'
PATH_WEB = 'ga.web'
SUB_PATHS = [SUB_PATH_DEVICE]

17 changes: 11 additions & 6 deletions code/core/factory/forge/system/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ def get(self) -> dict:

for key, blueprint in _config.items():
# todo: for multi-agent support we must get the right agent config
output_dict[key] = [blueprint(
setting_dict=self.supply_data[key][1][config.SUPPLY_KEY_SETTING_DICT],
object_id=self.supply_data[key][1][config.DB_ALL_KEY_ID],
name=self.supply_data[key][1][config.DB_ALL_KEY_NAME],
description=self.supply_data[key][1][config.DB_ALL_KEY_DESCRIPTION],
)]
try:
output_dict[key] = [blueprint(
setting_dict=self.supply_data[key][1][config.SUPPLY_KEY_SETTING_DICT],
object_id=self.supply_data[key][1][config.DB_ALL_KEY_ID],
name=self.supply_data[key][1][config.DB_ALL_KEY_NAME],
description=self.supply_data[key][1][config.DB_ALL_KEY_DESCRIPTION],
)]

except (IndexError, KeyError) as error:
log(f"Factory wasn't able to pull {key}-data from database! Make sure the configuration exists!", level=1)
raise KeyError(error)

# output_dict.update({
# self.key_object_task: TaskFactory(
Expand Down
17 changes: 14 additions & 3 deletions code/core/sock/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ def __init__(self, link, logger=log, server: bool = False):
self.PATH_SEP = socket_config.PACKAGE_PATH_SEPARATOR
if self.SERVER:
self.LOG_PREFIX = 'Server - '

else:
self.LOG_PREFIX = 'Client - '

if self.SHUFFLE:
self.LOG(f'{self.LOG_PREFIX}Shuffling transferred data', level=7)
self.LOG(f'{self.LOG_PREFIX}Shuffling data!', level=6)
# self.LOG(f'{self.LOG_PREFIX}Shuffle config {config.SOCKET_SHUFFLE} {type(config.SOCKET_SHUFFLE)}, {config.SERVER.security} {type(config.SERVER.security)}!', level=6)

def send(self, data: str) -> (bool, dict):
try:
Expand Down Expand Up @@ -131,14 +133,18 @@ def _validate(self, data: str) -> (dict, None):
head_str = self.HEAD.decode('utf-8')
tail_str = self.TAIL.decode('utf-8')

if data.startswith(head_str) and data.endswith(tail_str):
if data == socket_config.NONE_RESULT:
self.LOG(f'{self.LOG_PREFIX}Received empty result!', level=3)

elif data.startswith(head_str) and data.endswith(tail_str):
_ = data[len(head_str):-len(tail_str)].split(self.PATH_SEP)
try:
return {'path': _[0], 'data': _[1]}

except IndexError:
self.LOG(f"{self.LOG_PREFIX}Wasn't able to parse received data!", level=3)
self.LOG(f"{self.LOG_PREFIX}Data: \"{data}\"!", level=6)
data_one_line = data.replace('\n', '\\n')
self.LOG(f"{self.LOG_PREFIX}Data: \"{data_one_line}\"!", level=6)

else:
self.LOG(f'{self.LOG_PREFIX}Received invalid data!', level=3)
Expand Down Expand Up @@ -200,6 +206,11 @@ def server_thread(srv, connection, client):

if data is None:
srv.LOG(f'{srv.LOG_PREFIX}Unable to get route', level=6)
Interact(
link=connection,
server=True,
logger=srv.LOG
).send(data=socket_config.NONE_RESULT) # return none-result to client so it does not wait for a response that will never come

else:
# parsing command and executing api
Expand Down
1 change: 1 addition & 0 deletions code/web/base/ga/subviews/api/sock/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

init_core_config()
from core.sock.connect import Client
from core.config import shared


mapping = {
Expand Down
14 changes: 13 additions & 1 deletion code/web/base/ga/subviews/config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def _get_status(self, typ: str) -> dict:
status = {}

for device in MAIN_CONFIG[typ]['model'].objects.all():
status[device] = api_sock(
result = api_sock(
request=self.request,
sock_data={
'type': typ,
Expand All @@ -337,5 +337,17 @@ def _get_status(self, typ: str) -> dict:
}
)

if result is None:
# unknown
status[device] = 99

elif result is False:
# idle
status[device] = 0

else:
# active
status[device] = 1

return status

6 changes: 4 additions & 2 deletions code/web/base/ga/templates/config/list/member_data.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
{% endfor %}
{% if 'output' in typ %}
<td>
{% if device_status|get_item:member_obj %}
{% if device_status|get_item:member_obj == 1 %}
Active
{% else %}
{% elif device_status|get_item:member_obj == 0 %}
Idle
{% else %}
Unknown
{% endif %}
</td>
{% endif %}
Expand Down

0 comments on commit b03958a

Please sign in to comment.