Beispiel #1
0
def create_tables():
    click.echo(
        click.style('[...] Creating Pade tables in selected data base.',
                    fg='red'))
    from pade.web.flask_server import db
    db.create_all()
    click.echo(
        click.style('[ok_] Tables created in selected data base', fg='green'))
Beispiel #2
0
def start_loop(session, debug=False):
    """
        Lança o loop do twisted integrado com o loop do Qt se a opção GUI for
        verdadeira, se não lança o loop do Twisted
    """

    # instancia o factory do agente AMS e chama o metodo listenTCP
    # do Twisted para o agente AMS
    amsFactory = AgentManagementFactory(session.ams['port'], debug)
    twisted.internet.reactor.listenTCP(session.ams['port'], amsFactory)

    # instancia a classe que lança o processo
    # com o servidor web com o aplicativo Flask
    p1 = FlaskServerProcess()
    p1.daemon = True

    p1.start()

    db.drop_all()
    db.create_all()

    # registra uma nova sessão no banco de dados
    s = Session(name=session.name,
                date=datetime.datetime.now(),
                state = 'Ativo')
    db.session.add(s)
    db.session.commit()

    # registra os agentes no banco de dados
    i = 1
    agents_db = list()
    for agent in session.agents:
        twisted.internet.reactor.callLater(i, listen_agent, agent)
        a = Agent(name=agent.aid.localname,
                  session_id=s.id,
                  date=datetime.datetime.now(),
                  state = 'Ativo')
        agents_db.append(a)
        i += 0.2

    db.session.add_all(agents_db)
    db.session.commit()

    # registra os usuarios no banco de dados
    users_db = list()
    for user in session.users:
        u = User(username=user['username'],
                 email=user['email'],
                 password=user['password'],
                 session_id=s.id)
        users_db.append(u)

    db.session.add_all(users_db)
    db.session.commit()

    # lança o loop do Twisted
    twisted.internet.reactor.run()
Beispiel #3
0
    def _initialize_database(self):
        db.create_all()
        # searches in the database if there is a session with 
        # this name
        db_session = Session.query.filter_by(name=self.name).first()

        # in case there is not a session with this name
        if db_session is None:
            # clear out the database and creates new registers
            db.drop_all()
            db.create_all()

            # registers a new session in the database
            db_session = Session(name=self.name,
                                 date=datetime.datetime.now(),
                                 state='Active')
            db.session.add(db_session)
            db.session.commit()

            # instantiates AMS agent and calls listenTCP method
            # from Twisted to launch the agent
            ams_agent = AMS(host=self.ams['name'],
                            port=self.ams['port'],
                            session=db_session,
                            debug=self.ams_debug)
            reactor.listenTCP(ams_agent.aid.port, ams_agent.agentInstance)

            # registers the users, in case they exist, in the database

            if len(self.users) != 0:
                users_db = list()
                for user in self.users:
                    u = User(username=user['username'],
                             email=user['email'],
                             password=user['password'],
                             session_id=db_session.id)
                    users_db.append(u)

                db.session.add_all(users_db)
                db.session.commit()

        # in case there is a session with this name
        else:

            self._verify_user_in_session(db_session)
Beispiel #4
0
    def _initialize_database(self):
        db.create_all()
        # searches in the database if there is a session with
        # this name
        self.session = Session.query.filter_by(name=self.session_name).first()

        # in case there is not a session with this name
        if self.session is None:
            # clear out the database and creates new registers
            db.drop_all()
            db.create_all()

            # registers a new session in the database
            self.session = Session(name=self.session_name,
                                   date=datetime.now(),
                                   state='Active')
            db.session.add(self.session)
            db.session.commit()

            reactor.listenTCP(self.aid.port, self.agentInstance)

            # registers the users, in case they exist, in the database

            if len(self.users) != 0:
                users_db = list()
                for user in self.users:
                    u = User(username=user['username'],
                             email=user['email'],
                             password=user['password'],
                             session_id=self.session.id)
                    users_db.append(u)

                db.session.add_all(users_db)
                db.session.commit()

        # in case there is a session with this name
        else:
            self._verify_user_in_session(self.session)
Beispiel #5
0
                             password=user['password'],
                             session_id=self.session.id)
                    users_db.append(u)

                db.session.add_all(users_db)
                db.session.commit()

        # in case there is a session with this name
        else:
            self._verify_user_in_session(self.session)


if __name__ == '__main__':

    display_message('AMS', 'creating tables in database...')
    db.create_all()
    display_message('AMS', 'tables created in database.')

    ENGINE = create_engine('sqlite:///' + os.path.join(basedir, 'data.sqlite'))
    TWISTED_ENGINE = wrap_engine(reactor, ENGINE)
    TWISTED_ENGINE.run_callable = ENGINE.run_callable

    METADATA = MetaData()
    METADATA.bind = ENGINE
    AGENTS = Table('agents', METADATA, autoload=True, autoload_with=ENGINE)

    ams = AMS(port=int(sys.argv[4]))
    # instantiates AMS agent and calls listenTCP method
    # from Twisted to launch the agent
    ams_agent = AMS()  # TODO: precisa implementar a passagem de parametros
    ams.register_user(username=sys.argv[1],