-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnet_profile.py
More file actions
77 lines (62 loc) · 2.75 KB
/
net_profile.py
File metadata and controls
77 lines (62 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*- coding: utf-8 -*-
# MDA Network Project
# Ring decentralized P2P network for secure instance-messaging
# By Amr Abdelnaser C&E Engineer, Qatar
# Begin in 14/9/2015 04:23 PM
# This file handle the .mdar network profile
import base64
import db_dht as db
class mdar:
"""
A MDA Network profile manage the primary connections to join and connect to a specific mda network using it's
file association .mdar file.
"""
def __init__(self):
self.mda_pre = "**MDA Project Network profile**"
self.network_name = None
self.network_type = None # Public or Private
self.network_rules = [] # If any
self.network_description = None # If any
def make_mdar(self,net_name, net_type,net_description ,net_rules): # selected_nodes add for test only
# Make a new MDA network profile.
# Set the properties of the network
self.network_name = net_name
if net_type == 'public' or net_type == 'private':
self.network_type = net_type
else:
self.network_type = 'public'
self.network_description = net_description
self.network_rules = net_rules
selected_nodes = db.select_nodes(net_name)
file_name = "{}".format(self.network_name) # By default
mdar_data = self.mdar_format(selected_nodes)
# put into .mdar file
out = file_name + ".mdar"
with open(out, 'wb') as fo:
fo.write(mdar_data)
def mdar_format(self, selected_nodes):
# make the metadata in the format of .mdar network profile file
data_format = "{0}\n{1}:{2}\n{3}:{4}\n{5}".format(self.mda_pre, self.network_name, self.network_type, self.network_description, self.network_rules, selected_nodes)
encoded_data = base64.b64encode(data_format) # 64encoding
return encoded_data
def add_network(self, file_name):
err = False
net_name, net_type, net_des, net_rules, selected_nodes = self.read_mdar(file_name)
if net_name == "0" and net_type == "0":
err = True
else:
db.add_network(net_name, net_type, net_des, net_rules, selected_nodes)
return err
@staticmethod
def read_mdar(file_name):
# read a .mdar file
net_name, net_type, net_des, net_rules, selected_nodes = "0", "0", "0", "0", "0"
if file_name.endswith('.mdar'):
with open(file_name, 'rb') as fo:
txt = fo.read()
decoded_data = base64.b64decode(txt)
mda_pre, bit2, bit3, bit4 = decoded_data.split("\n", 4)
net_name, net_type = bit2.split(':', 1)
net_des, net_rules = bit3.split(':', 1)
selected_nodes = bit4
return net_name, net_type, net_des, net_rules, selected_nodes