Exemple #1
0
 def initialize_mesh_relay(self):
     '''Initializes the relay interface'''
     relay_loc = 'mesh-relay-in-us-1.herokuapp.com'
     self.relay_interface = RelayInterface(self.crypto, relay_loc, self.manager.uid)
Exemple #2
0
class BaseNode(ConfigurationConsumer):
    '''Bare-bones node, contains no logic... just endpoints'''

    def __init__(self, alias, location, port):
        self.location = location
        self.port = port
        self.alias = alias
        self.check_for_missing_configs()
        self.manager = Manager(alias, location, port)

    def initialize_main_system(self):
        ''' Create Base Components'''
        self.crypto = self.manager.crypto

    def initialize_io(self):
        self.listener = Listener(self.crypto)
        self.sender = Sender(self.crypto, self.relay_interface)
        self.dispatcher = Dispatcher(self.manager, self.sender)

    def initialize_mesh_relay(self):
        '''Initializes the relay interface'''
        relay_loc = 'mesh-relay-in-us-1.herokuapp.com'
        self.relay_interface = RelayInterface(self.crypto, relay_loc, self.manager.uid)

    def initialize(self):
        self.initialize_main_system()
        self.initialize_mesh_relay()
        self.initialize_io()

    def start_up_listener(self):
        '''Set up the listener thread separately'''
        listener_thread = threading.Thread(target=self.listener.__launch__, args=(int(self.port),))
        listener_thread.daemon = True
        listener_thread.start()

    def export(self):
        '''Export the JSON containing all pertinent information about this node'''
        data = {"uid": self.manager.uid, "alias": self.alias, "location": ':'.join([self.location,str(self.port)])}
        data["publickey"] = self.manager.crypto.get_public_key().decode('utf-8')

        # Add into Nodelist
        self.manager.node_list.add(data)

        with open('{0}.json'.format(self.alias),'w') as auth:
            json.dump(data, auth)

    def check_for_missing_configs(self):
        '''Check to see if a config is missing, and create it if so'''
        config_files = ['config', 'users', 'nodes']
        for config in config_files:
            if not os.path.isfile(self.config_path(config + '.json')):
                create_method = getattr(self, 'create_default_' + config)
                create_method()

    def create_default_config(self):
        data = {"version": "0.9.0",
        	"name": "Py\u00d1a Colada",
        	"bindings": {
        		"": "Chat",
        		"/w": "Whisper",
        		"/me": "Emote",
        		"/em": "Emote",
        		"/ping": "Ping",
        		"/exit": "Exit",
        		"/about": "About",
        		"/r": "Reply",
        		"/?": "Info",
        		"/pingall": "PingAll",
        		"/import": "Import",
                "/roll": "Roll",
        		"/who": "Who"}}
        with open(self.config_path('config.json'),'w') as config:
            json.dump(data, config)

    def create_default_users(self):
        with open(self.config_path('users.json'),'w') as config:
            json.dump({}, config)

    def create_default_nodes(self):
        with open(self.config_path('nodes.json'),'w') as config:
            json.dump({}, config)

    def start(self):
        '''Start up this node'''
        self.initialize()
        self.relay_interface.start(self.listener.parser)
        self.start_up_listener()
        self.export()

        #Send a default ping
        authorized_nodes = self.manager.node_list.authorized
        self.dispatcher.broadcast('NodeListHash', targets=authorized_nodes)
        self.__running__()


    def __running__(self):
        '''What happens directly after the listener starts; to be implemented by the user'''
        pass