TEST(cps_api_db,db_node_list) {

    cps_api_object_t obj = cps_api_object_create();
    cps_api_object_guard og(obj);
    ASSERT_TRUE(og.get()!=nullptr);

    cps_api_node_ident ids[2] = { {"NODE1", "10.11.56.37:6379"}, {"NODE2","127.0.0.1:6379"} };
    cps_api_node_group_t _g;

    _g.id = "A";
    _g.addrs = ids;
    _g.addr_len = 2;
    _g.data_type = cps_api_node_data_1_PLUS_1_REDUNDENCY;

    ASSERT_EQ(cps_api_set_node_group(&_g),cps_api_ret_code_OK);
    ASSERT_EQ(cps_api_set_master_node("A","NODE1"),cps_api_ret_code_OK);


    cps_api_key_from_attr_with_qual(cps_api_object_key(obj),BASE_IP_IPV6,cps_api_qualifier_TARGET);
    cps_api_object_attr_add_u32(obj,BASE_IP_IPV6_VRF_ID,0);
    cps_api_object_attr_add_u32(obj,BASE_IP_IPV6_IFINDEX,1);
    cps_api_object_attr_add(obj,BASE_IP_IPV6_NAME,"Clifford",9);

    cps_api_key_set_group(obj,"A");

    ASSERT_TRUE(cps_api_commit_one(cps_api_oper_CREATE, obj, 0, 200)==cps_api_ret_code_OK);
    ASSERT_EQ(cps_api_delete_node_group("A"),cps_api_ret_code_OK);

}
Exemplo n.º 2
0
bool cps_api_nodes::load_aliases() {
    cps_db::connection_request b(cps_db::ProcessDBCache(),DEFAULT_REDIS_ADDR);
    if (!b.valid()) {
        return false;
    }

    alias_map_t _cpy;

    cps_api_object_guard og(cps_api_object_create());
    if (!cps_api_key_from_attr_with_qual(cps_api_object_key(og.get()),CPS_NODE_DETAILS, cps_api_qualifier_TARGET)) {
        return false;
    }

    cps_api_object_list_guard lg(cps_api_object_list_create());
    if (!cps_db::get_objects(b.get(),og.get(),lg.get())) return false;

    for ( size_t ix = 0, mx = cps_api_object_list_size(lg.get()); ix < mx ; ++ix ) {
        cps_api_object_t o = cps_api_object_list_get(lg.get(),ix);
        const char *name = (const char*) cps_api_object_get_data(o,CPS_NODE_DETAILS_NAME);
        const char *alias = (const char*) cps_api_object_get_data(o,CPS_NODE_DETAILS_ALIAS);

        _cpy[name] = alias;
    }
    std::swap(_cpy,_alias_map);

    return true;
}
Exemplo n.º 3
0
bool cps_api_nodes::get_port_info(const char *group, _db_node_data *nd) {
    cps_api_object_guard og(cps_api_object_create());

    if (!cps_api_key_from_attr_with_qual(cps_api_object_key(og.get()),CPS_DB_INSTANCE_OBJ,
                                         cps_api_qualifier_TARGET)) {
        EV_LOGGING(DSAPI,ERR,"CPS-CLS-MAP","Meta data for cluster set is not loaded.");
        return false;
    }

    cps_db::connection_request b(cps_db::ProcessDBCache(),DEFAULT_REDIS_ADDR);

    if (!b.valid()) {
        return false;
    }

    cps_api_set_key_data(og.get(),CPS_DB_INSTANCE_GROUP,cps_api_object_ATTR_T_BIN,group,strlen(group)+1);
    cps_api_set_key_data(og.get(),CPS_DB_INSTANCE_NODE_ID,cps_api_object_ATTR_T_BIN,nd->_name.c_str(),strlen(nd->_name.c_str())+1);

    if (cps_db::get_object(b.get(),og.get())) {

        const char *port = (const char*) cps_api_object_get_data(og.get(),CPS_DB_INSTANCE_PORT);
        if(port == nullptr) {
            return false;
        }
        nd->_addr = port;
        return true;
    }
    return false;
}
static bool py_add_object_to_trans( cps_api_transaction_params_t *tr, PyObject *dict) {
    PyObject *_req = PyDict_GetItemString(dict,"change");
    PyObject *_op = PyDict_GetItemString(dict,"operation");

    if (_op==NULL || _req==NULL) {
        return false;
    }
    PyObject *_prev = PyDict_GetItemString(dict,"previous");
    if (_prev!=NULL) {
        if (cps_api_object_list_size(tr->change_list)!=
                cps_api_object_list_size(tr->prev)) {
            return false;
        }
        cps_api_object_guard og(dict_to_cps_obj(_req));
        if (!og.valid()) return false;
        if (!cps_api_object_list_append(tr->prev,og.get())) {
            return false;
        }
        og.release();
    }

    using cps_oper = cps_api_return_code_t (*)(cps_api_transaction_params_t * trans,
            cps_api_object_t object);

    static std::map<std::string,cps_oper> trans = {
            {"delete",cps_api_delete },
            {"create",cps_api_create},
            {"set",cps_api_set},
            {"rpc",cps_api_action}
    };

    if (trans.find(PyString_AsString(_op))==trans.end()) {
        return false;
    }

    cps_oper oper = trans[PyString_AsString(_op)];

    cps_api_object_t obj = dict_to_cps_obj(_req);
    cps_api_object_guard og(obj);
    if (!og.valid()) return false;

    if (oper(tr,obj)!=cps_api_ret_code_OK) {
        return false;
    }
    og.release();
    return true;
}
extern "C" cps_api_object_t cps_api_obj_tool_create(cps_api_qualifier_t qual,cps_api_attr_id_t id, bool add_defaults) {
    cps_api_object_guard og(cps_api_object_create());
    if (!og.valid()) return nullptr;

    if (!cps_api_key_from_attr_with_qual(cps_api_object_key(og.get()),id,qual)) {
        return nullptr;
    }

    return og.release();
}
static t_std_error _phy_media_type_with_default_config_set(npu_id_t npu, port_t port,
                                        PLATFORM_MEDIA_TYPE_t media_type)
{
    t_std_error rc;
    if ((rc = ndi_port_media_type_set(npu, port, media_type)) != STD_ERR_OK) {
        return rc;
    }
    /*  set default speed and autoneg setting  corresponding to the media type */
    cps_api_object_t obj = cps_api_object_create();
    cps_api_object_guard og(obj);
    if ((rc = dn_nas_get_phy_media_default_setting(media_type, obj)) != STD_ERR_OK) {
        return rc;
    }

    /* First set AN and then speed */
    cps_api_object_attr_t autoneg_attr = cps_api_object_attr_get(obj, BASE_MEDIA_MEDIA_INFO_AUTONEG);
    if (autoneg_attr != nullptr) {
        bool autoneg = (bool)cps_api_object_attr_data_u32(autoneg_attr);
        if (ndi_port_auto_neg_set(npu,port,autoneg)!=STD_ERR_OK) {
            EV_LOG(ERR,INTERFACE,0,"NAS-IF-REG","Failed to set autoneg %d for "
                   "npu %d port %d return Error 0x%x",autoneg,npu,port,rc);
            return STD_ERR(INTERFACE,FAIL,0);
        }
    }

    BASE_IF_PHY_BREAKOUT_MODE_t cur_mode;
    if ((rc = ndi_port_breakout_mode_get(npu, port, &cur_mode)) != STD_ERR_OK) {
            EV_LOG(ERR,INTERFACE,0,"NAS-IF-REG","Failed to get breakout mode for "
                   "npu %d port %d return error 0x%x",npu,port, rc);
            return STD_ERR(INTERFACE,FAIL,0);
    }
    /* if the port is fanout skip setting the default speed say 40G */
    if (BASE_IF_PHY_BREAKOUT_MODE_BREAKOUT_1X1 == cur_mode) {
        cps_api_object_attr_t speed_attr = cps_api_object_attr_get(obj, BASE_MEDIA_MEDIA_INFO_SPEED);
        if (speed_attr != nullptr) {
            BASE_IF_SPEED_t speed = (BASE_IF_SPEED_t)cps_api_object_attr_data_u32(speed_attr);
            if ((rc = ndi_port_speed_set(npu,port,speed))!=STD_ERR_OK) {
                EV_LOG(ERR,INTERFACE,0,"NAS-IF-REG","Failed to set speed %d for "
                       "npu %d port %d return error 0x%x",speed,npu,port, rc);
                return STD_ERR(INTERFACE,FAIL,0);
            }
        }
    }
    cps_api_object_attr_t duplex_attr = cps_api_object_attr_get(obj, BASE_MEDIA_MEDIA_INFO_DUPLEX);
    if (duplex_attr != nullptr) {
        BASE_CMN_DUPLEX_TYPE_t duplex = (BASE_CMN_DUPLEX_TYPE_t)cps_api_object_attr_data_u32(duplex_attr);
        if ((rc = ndi_port_duplex_set(npu,port,duplex))!=STD_ERR_OK) {
            EV_LOG(ERR,INTERFACE,0,"NAS-IF-REG","Failed to set duplex %d for "
                   "npu %d port %d return error 0x%x",duplex,npu,port, rc);
            return STD_ERR(INTERFACE,FAIL,0);
        }
    }
    return STD_ERR_OK;
}
Exemplo n.º 7
0
cps_api_return_code_t cps_api_delete_node_group(const char *grp) {
    cps_api_db_del_node_group(grp);
    cps_api_object_guard og(cps_api_object_create());
    cps_api_key_from_attr_with_qual(cps_api_object_key(og.get()),CPS_NODE_GROUP, cps_api_qualifier_TARGET);

    cps_api_object_attr_add(og.get(),CPS_NODE_GROUP_NAME,grp,strlen(grp)+1);

    cps_db::connection_request b(cps_db::ProcessDBCache(),DEFAULT_REDIS_ADDR);
    cps_db::delete_object(b.get(),og.get());
    return cps_api_ret_code_OK;
}
cps_api_return_code_t cps_api_process_get_request(cps_api_get_params_t *param, size_t ix) {
    cps_api_return_code_t rc = cps_api_ret_code_ERR;
    char buff[SCRATCH_LOG_BUFF];
    cps_api_channel_t handle;
    cps_api_key_t *key = cps_api_object_key(cps_api_object_list_get(param->filters,ix));
    if (!cps_api_get_handle(*key,handle)) {
        EV_LOG(ERR,DSAPI,0,"NS","Failed to find owner for %s",
                cps_api_key_print(key,buff,sizeof(buff)-1));
        return cps_api_ret_code_NO_SERVICE;
    }

    do {
        cps_api_object_t o = cps_api_object_list_get(param->filters,ix);
        if (o==NULL) {
            EV_LOG(ERR,DSAPI,0,"NS","Missing filters... %s",
                                       cps_api_key_print(key,buff,sizeof(buff)-1));
            break;
        }
        if (!cps_api_send_one_object(handle,cps_api_msg_o_GET,o)) {
                   EV_LOG(ERR,DSAPI,0,"NS","Failed to send request %s",
                           cps_api_key_print(key,buff,sizeof(buff)-1));
                   break;
        }

        uint32_t op;
        do {
            if (param->timeout > 0) {
                fd_set rset;
                FD_ZERO(&rset);
                FD_SET(handle,&rset);
                if ((rc=cps_api_timeout_wait(handle,&rset,param->timeout,"CPS-OP-GET"))!=cps_api_ret_code_OK) {
                    EV_LOG(ERR,DSAPI,0,"CPS-OP-GET","Send request %s timed out",
                           cps_api_key_print(key,buff,sizeof(buff)-1));
                    break;
                }
            }
            size_t len;
            if (!cps_api_receive_header(handle,op,len)) break;
            if (op!=cps_api_msg_o_GET_RESP) break;
            cps_api_object_guard og (cps_api_receive_object(handle,len));

            if (og.valid() && cps_api_object_list_append(param->list,og.get())) {
                og.release();
            } else break;
        } while (op == cps_api_msg_o_GET_RESP);

        if (op!=cps_api_msg_o_GET_DONE) break; //leave an error code
        rc = cps_api_ret_code_OK;
    } while (0);

    cps_api_disconnect_owner(handle);

    return rc;
}
Exemplo n.º 9
0
// Look for items in the active list that don't have their dependencies filled
// anymore.
//
// POST: Any elements in active with dependencies not filled are added to the
//       the pending list. (A remove and an add are added to the pending).
// RETURNS: The number of lost dependencies found.
int ConfigManager::scanForLostDependencies()
{
   vprASSERT(0 == mActiveLock.test());
   // We can't hold the lock upon entry

   vpr::DebugOutputGuard og(vprDBG_ALL, vprDBG_CONFIG_LVL,
                            "ConfigManager::scanForLostDependencies()\n",
                            "ConfigManager::scanForLostDependencies() done.\n");

   DependencyManager* dep_mgr = DependencyManager::instance();
   std::vector<ConfigElementPtr> elements;
   int num_lost_deps(0);

   // NOTE: can't hold this lock because the isSatisfied routines make
   // use of the activeLock as well
   // NOTE: Make the copy of the elements so that we can iterate without
   // fear of active changing
   mActiveLock.acquire();
   elements = mActiveConfig.vec();   // Get a copy of the elements
   mActiveLock.release();

   // Now test them
   for ( unsigned int i=0;i<elements.size();i++ )
   {
      if ( !dep_mgr->isSatisfied(elements[i]) )     // We are not satisfied
      {
         vprDEBUG_NEXT(vprDBG_ALL, vprDBG_WARNING_LVL)
            << elements[i]->getName()
            << " type: " << elements[i]->getID()
            << " has lost dependencies.\n" << vprDEBUG_FLUSH;

         num_lost_deps++;              // Keep a count of the number lost deps found

         // Add the pending removal
         PendingElement pending;
         pending.mType = PendingElement::REMOVE;
         pending.mElement = elements[i];
         
         vprASSERT(1 == mPendingLock.test());
         addPending(pending);

         // Add the pending re-addition
//         ConfigElementPtr copy_of_element;          // Need a copy so that the remove can delete the element
//         copy_of_element = new ConfigElement(*elements[i]);
         pending.mType = PendingElement::ADD;
         pending.mElement = elements[i];//copy_of_element;
         
         vprASSERT(1 == mPendingLock.test());
         addPending(pending);                   // Add the add item
      }
   }

   return num_lost_deps;
}
static cps_api_return_code_t _write_function(void * context, cps_api_transaction_params_t * param,size_t ix) {
    GILLock gil;

    cps_api_object_t obj = cps_api_object_list_get(param->change_list,ix);
    if (obj==NULL) return cps_api_ret_code_ERR;

    cps_api_object_t prev = cps_api_object_list_get(param->prev,ix);
    if(prev==NULL) {
        prev = cps_api_object_list_create_obj_and_append(param->prev);
    }
    if (prev==NULL) return cps_api_ret_code_ERR;

    cps_api_operation_types_t op = cps_api_object_type_operation(cps_api_object_key(obj));

    PyObject *p = PyDict_New();
    PyRef dict(p);

    py_cps_util_set_item_to_dict(p,"change",cps_obj_to_dict(obj));
    py_cps_util_set_item_to_dict(p,"previous",cps_obj_to_dict(prev));

    static const std::map<int,std::string> trans = {
            {cps_api_oper_DELETE,"delete" },
            {cps_api_oper_CREATE,"create"},
            {cps_api_oper_SET, "set"},
            {cps_api_oper_ACTION,"rpc"}
    };

    py_cps_util_set_item_to_dict(p,"operation",PyString_FromString(trans.at(op).c_str()));

    py_callbacks_t *cb = (py_callbacks_t*)context;
    PyObject *res = cb->execute("transaction",p);

    if (res==NULL || !PyBool_Check(res) || (Py_False==(res))) {
        return cps_api_ret_code_ERR;
    }
    PyRef ret(res);

    PyObject *ch = PyDict_GetItemString(p,"change");
    if (ch==NULL) return cps_api_ret_code_ERR;

    cps_api_object_t o = dict_to_cps_obj(ch);
    cps_api_object_guard og(o);
    if(og.valid()) {
        cps_api_object_clone(obj,og.get());
    }

    PyObject *pr = PyDict_GetItemString(p,"previous");
    if (pr==NULL) return cps_api_ret_code_ERR;
    og.set(dict_to_cps_obj(pr));
    if (og.valid()) {
        cps_api_object_clone(prev,og.get());
    }
    return cps_api_ret_code_OK;
}
static void _ndi_port_event_update_ (ndi_port_t  *ndi_port, ndi_port_event_t event, uint32_t hwport) {
    cps_api_object_guard og(cps_api_object_create());

    init_phy_port_obj(ndi_port->npu_id,ndi_port->npu_port,og.get());

    cps_api_object_attr_add_u32(og.get(),BASE_IF_PHY_PHYSICAL_HARDWARE_PORT_ID,hwport);

    if (!(event == ndi_port_ADD || event==ndi_port_DELETE)) return ;

    cps_api_object_set_type_operation(cps_api_object_key(og.get()),event == ndi_port_ADD ?
        cps_api_oper_CREATE : cps_api_oper_DELETE );

    hal_interface_send_event(og.get());
}
Exemplo n.º 12
0
    void deduplicate(const File &inputGallery, const File &outputGallery, const float threshold)
    {
        qDebug("Deduplicating %s to %s with a score threshold of %f", qPrintable(inputGallery.flat()), qPrintable(outputGallery.flat()), threshold);

        if (distance.isNull()) qFatal("Null distance.");

        QScopedPointer<Gallery> i;
        FileList inputFiles;
        retrieveOrEnroll(inputGallery, i, inputFiles);

        TemplateList t = i->read();

        Output *o = Output::make(QString("buffer.tail[selfSimilar,threshold=%1,atLeast=0]").arg(QString::number(threshold)),inputFiles,inputFiles);

        // Compare to global tail output
        distance->compare(t,t,o);

        delete o;

        QString buffer(Globals->buffer);

        QStringList tail = buffer.split("\n");

        // Remove header
        tail.removeFirst();

        QStringList toRemove;
        foreach(const QString &s, tail)
            toRemove.append(s.split(',').at(1));

        QSet<QString> duplicates = QSet<QString>::fromList(toRemove);

        QStringList fileNames = inputFiles.names();

        QList<int> indices;
        foreach(const QString &d, duplicates)
            indices.append(fileNames.indexOf(d));

        std::sort(indices.begin(),indices.end(),std::greater<float>());

        qDebug("\n%d duplicates removed.", indices.size());

        for (int i=0; i<indices.size(); i++)
            inputFiles.removeAt(indices[i]);

        QScopedPointer<Gallery> og(Gallery::make(outputGallery));

        og->writeBlock(inputFiles);
    }
Exemplo n.º 13
0
   void writeUint8(vpr::Uint8 p0)
   {
      vpr::DebugOutputGuard og(pyjDBG_CXX, vprDBG_VERB_LVL,
                               "vpr_ObjectWriter_Wrapper::writeUint8()\n",
                               "vpr_ObjectWriter_Wrapper::writeUint8() done.\n");
      PyJuggler::InterpreterGuard guard;

      try
      {
         this->get_override("writeUint8")(p0);
      }
      catch (error_already_set)
      {
         PyErr_Print();
      }
   }
Exemplo n.º 14
0
   void endAttribute()
   {
      vpr::DebugOutputGuard og(pyjDBG_CXX, vprDBG_VERB_LVL,
                               "vpr_ObjectWriter_Wrapper::endAttribute()\n",
                               "vpr_ObjectWriter_Wrapper::endAttribute() done.\n");
      PyJuggler::InterpreterGuard guard;

      try
      {
         this->get_override("endAttribute")();
      }
      catch (error_already_set)
      {
         PyErr_Print();
      }
   }
Exemplo n.º 15
0
   void beginAttribute(const std::string& p0)
   {
      vpr::DebugOutputGuard og(pyjDBG_CXX, vprDBG_VERB_LVL,
                               "vpr_ObjectWriter_Wrapper::beginAttribute()\n",
                               "vpr_ObjectWriter_Wrapper::beginAttribute() done.\n");
      PyJuggler::InterpreterGuard guard;

      try
      {
         this->get_override("beginAttribute")(boost::ref(p0));
      }
      catch (error_already_set)
      {
         PyErr_Print();
      }
   }
extern "C" cps_api_return_code_t cps_api_object_stats(cps_api_key_t *key, cps_api_object_t stats_obj) {
    cps_api_return_code_t rc = cps_api_ret_code_ERR;

    cps_api_channel_t handle = -1;

    if (key==nullptr || cps_api_key_get_len(key)==0) {
        std_socket_address_t addr;
        cps_api_ns_get_address(&addr);

        int sock;
        t_std_error _rc = std_sock_connect(&addr,&sock);
        if (_rc!=STD_ERR_OK) {
            return rc;
        }
        handle = sock;
    } else {
        if (!cps_api_get_handle(*key,handle)) {
            char buff[CPS_API_KEY_STR_MAX];
            EV_LOG(ERR,DSAPI,0,"NS","Failed to find owner for %s",
                    cps_api_key_print(key,buff,sizeof(buff)-1));
            return cps_api_ret_code_NO_SERVICE;
        }
    }
    if (handle==-1) {
        EV_LOG(ERR,DSAPI,0,"NS-STATS","No connection to the NS for stats.");
        return rc;
    }
    do {
        if (!cps_api_send_header(handle,cps_api_msg_o_STATS,0)) {
            break;
        }
        size_t len;
        uint32_t op=0;
        if (!cps_api_receive_header(handle,op,len)) break;
        if (op!=cps_api_msg_o_STATS) break;

        cps_api_object_guard og (cps_api_receive_object(handle,len));
        if(!og.valid()) return false;

        if (!cps_api_object_clone(stats_obj,og.get())) {
            break;
        }
        rc = cps_api_ret_code_OK;
    } while (0);
    cps_api_disconnect_owner(handle);
    return rc;
}
bool cps_api_db_del_node_group(const char *group) {
    std::lock_guard<std::recursive_mutex> lg(_mutex);

    cps_api_node_data_type_t type;
    if(!_nodes->get_group_type(std::string(group),type)) {
        EV_LOGGING(DSAPI,ERR,"DEL-MASTER","Failed to get group type for %s",group);
        return false;
    }

    if(type != cps_api_node_data_1_PLUS_1_REDUNDENCY) {
        return true;
    }

    auto it = _nodes->_db_node_map.find(group);
    if( it == _nodes->_db_node_map.end()) {
        EV_LOGGING(DSAPI,ERR,"CPS-DB","No group named %s found",group);
        return false;
    }

    for ( auto node_it : it->second) {
        cps_db::connection_request b(cps_db::ProcessDBCache(),DEFAULT_REDIS_ADDR);
        if (!b.valid()) {
            return false;
        }
        cps_api_object_guard og(cps_api_object_create());

        if (!cps_api_key_from_attr_with_qual(cps_api_object_key(og.get()),CPS_DB_INSTANCE_OBJ,
                                             cps_api_qualifier_TARGET)) {
            EV_LOGGING(DSAPI,ERR,"CPS-CLS-MAP","Meta data for cluster set is not loaded.");
            return false;
        }


        cps_api_set_key_data(og.get(),CPS_DB_INSTANCE_GROUP,cps_api_object_ATTR_T_BIN,group,strlen(group)+1);
        cps_api_set_key_data(og.get(),CPS_DB_INSTANCE_NODE_ID,cps_api_object_ATTR_T_BIN,node_it._name.c_str(),
                             strlen(node_it._name.c_str())+1);

        cps_db::delete_object(b.get(),og.get());
    }

    _nodes->_master.erase(std::string(group));
    _nodes->_db_node_map.erase(std::string(group));
    _nodes->remove_master_set(std::string(group));

    return cps_api_clean_db_instance(group);

}
Exemplo n.º 18
0
cps_api_return_code_t cps_api_set_node_group(cps_api_node_group_t *group) {

    cps_api_object_guard og(cps_api_object_create());

    cps_api_key_from_attr_with_qual(cps_api_object_key(og.get()),CPS_NODE_GROUP, cps_api_qualifier_TARGET);

    cps_api_object_attr_add(og.get(),CPS_NODE_GROUP_NAME,group->id,strlen(group->id)+1);

    for ( size_t ix = 0; ix < group->addr_len; ++ix ) {
        cps_api_attr_id_t _ip[]= {CPS_NODE_GROUP_NODE,ix,CPS_NODE_GROUP_NODE_IP};
        cps_api_object_e_add(og.get(),_ip,sizeof(_ip)/sizeof(*_ip),cps_api_object_ATTR_T_BIN,
                             group->addrs[ix].addr,strlen(group->addrs[ix].addr)+1);
        cps_api_attr_id_t _alias[]= {CPS_NODE_GROUP_NODE,ix,CPS_NODE_GROUP_NODE_NAME};
        cps_api_object_e_add(og.get(),_alias,sizeof(_alias)/sizeof(*_alias),cps_api_object_ATTR_T_BIN,
                             group->addrs[ix].node_name,strlen(group->addrs[ix].node_name)+1);
    }

    cps_api_object_attr_add(og.get(),CPS_NODE_GROUP_TYPE,&group->data_type,sizeof(group->data_type));

    cps_db::connection_request b(cps_db::ProcessDBCache(),DEFAULT_REDIS_ADDR);
    if (!b.valid()) {
        return cps_api_ret_code_ERR;
    }
    cps_api_object_guard tmp(cps_api_object_create());

    if (!cps_api_object_clone(tmp.get(),og.get())) {
        return cps_api_ret_code_ERR;
    }

    bool changed = false;
    if (cps_db::get_object(b.get(),tmp.get())) {
        changed = true;
    }

    (void)changed;
    if (!cps_db::store_object(b.get(),og.get())) {
        return cps_api_ret_code_ERR;
    }

    if(group->data_type == cps_api_node_data_1_PLUS_1_REDUNDENCY) {
        return cps_api_create_global_instance(group);
    }
    ///TODO send out changed...

    return cps_api_ret_code_OK;
}
int main()
{
    lcs::Bus<1> a, b, a_, b_, p1, p2, s;
    
    lcs::Not<> ng1(a_, a), ng2(b_, b);
    lcs::And<2> ag1(p1, (a,b_)), ag2(p2, (a_,b));
    lcs::Or<2> og(s, (p1,p2));

    lcs::ChangeMonitor<2> inputMonitor((a,b), "Input", lcs::DUMP_ON);
    lcs::ChangeMonitor<1> outputMonitor(s, "Output", lcs::DUMP_ON);

    lcs::Tester<2> tester((a,b));

    lcs::Simulation::setStopTime(1000);
    lcs::Simulation::start();

    return 0;
}
Exemplo n.º 20
0
/**
 * Main function
 */
int	main(int argc, char **argv)
{
  try
    {
      // !both filestr and backup are used for the file logger
      std::ofstream	filestr(FILE_TO_LOG);
      std::streambuf*	backup = std::clog.rdbuf();

      std::clog.rdbuf(filestr.rdbuf());
      std::clog << "#### New session opened ####" << std::endl;

      Glib::OptionContext	oc("Here's the parameters");
      Glib::OptionGroup		og("main", "main description");

      Glib::OptionEntry		verbose;
      verbose.set_long_name("verbose");
      verbose.set_short_name('v');
      verbose.set_description("set verbose level");
      verbose.set_arg_description("verbose level");

      og.add_entry(verbose);

      oc.set_main_group(og);
      oc.set_help_enabled();

      Gtk::Main		kit(argc, argv, oc);
      WindowManager	mywindow;

      Gtk::Main::run();

      std::clog << "#### Session closed ####" << std::endl;
      std::clog.rdbuf(backup);
      filestr.close();
    }
  catch (Glib::OptionError oe)
    {
      std::cerr << "OptionError: " << oe.what() << std::endl;
    }

  return 0;
}
 cps_api_object_t cps_api_receive_object(cps_api_channel_t handle,size_t len) {
    if (len==0) return NULL;

    cps_api_object_guard og(cps_api_object_create());
    t_std_error rc = STD_ERR_OK;

    do {
        if (cps_api_object_get_reserve_len(og.get()) < len) {
            if (!cps_api_object_reserve(og.get(),len)) break;
        }

        int by = std_read(handle,cps_api_object_array(og.get()),len,true,&rc);
        if (by!=(int)len) break;

        if (!cps_api_object_received(og.get(),len)) break;
        return og.release();

    } while (0);
    EV_LOG(ERR,DSAPI,0,"CPS IPC","Was not able to read the object - MSG (%X)",rc);
    return NULL;
}
Exemplo n.º 22
0
cps_api_return_code_t cps_api_set_identity(const char *name, const char **alias, size_t len) {
    cps_db::connection_request b(cps_db::ProcessDBCache(),DEFAULT_REDIS_ADDR);
    if (!b.valid()) {
        return cps_api_ret_code_ERR;
    }

    cps_api_object_guard og(cps_api_object_create());

    cps_api_key_from_attr_with_qual(cps_api_object_key(og.get()),CPS_NODE_DETAILS, cps_api_qualifier_TARGET);
    cps_api_object_attr_add(og.get(),CPS_NODE_DETAILS_ALIAS,name,strlen(name)+1);

    size_t ix = 0;
    for (; ix < len ; ++ix ) {
        cps_api_object_attr_add(og.get(),CPS_NODE_DETAILS_NAME,alias[ix],strlen(alias[ix])+1);
        if (!cps_db::store_object(b.get(),og.get())) {
            EV_LOG(ERR,DSAPI,0,"SET-IDENT","Failed to update identity setting for local node");
        }
        cps_api_object_attr_delete(og.get(),CPS_NODE_DETAILS_NAME);
    }
    return cps_api_ret_code_OK;
}
static cps_api_return_code_t _read_function (void * context, cps_api_get_params_t * param,
        size_t key_ix) {
    py_callbacks_t *cb = (py_callbacks_t*)context;

    GILLock gil;

    PyObject *p = PyDict_New();
    PyRef dict(p);

    py_cps_util_set_item_to_dict(p,"filter",cps_obj_to_dict(cps_api_object_list_get(
            param->filters,key_ix)));

    py_cps_util_set_item_to_dict(p,"list",PyList_New(0));

    PyObject * res = cb->execute("get",p);
    PyRef ret(res);
    if (res==NULL || !PyBool_Check(res) || (Py_False == (res))) {
        return cps_api_ret_code_ERR;
    }

    PyObject *d = PyDict_GetItemString(p,"list");
    if (d!=NULL && PyList_Check(d)) {
        size_t ix = 0;
        size_t mx = PyList_Size(d);
        for ( ; ix < mx ; ++ix ) {
            PyObject *o = PyList_GetItem(d,ix);
            if (o==NULL || !PyDict_Check(o)) continue;
            cps_api_object_guard og(dict_to_cps_obj(o));
            if (og.valid() && cps_api_object_list_append(param->list,og.get())) {
                og.release();
                continue;
            }
            return cps_api_ret_code_ERR;
        }
    }
    return cps_api_ret_code_OK;
}
Exemplo n.º 24
0
Image< float > HogFeature::evaluate(const Image< float > lab_image, const QString& name) {
	Image< float > og( lab_image.width(), lab_image.height(), nAngleBins );
	og.fill( 0.0 );
	int c = 0;
	if (type_ == A) c = 1;
	if (type_ == B) c = 2;
	// Compute the per ppixel gradient and store it's origentation and magnitude
	for( int j=0; j<lab_image.height(); j++ ){
		int j1 = j+1<lab_image.height() ? j+1 : j;
		int j2 = j>0 ? j-1 : j;
		for( int i=0; i<lab_image.width(); i++ ){
			int i1 = i+1<lab_image.width() ? i+1 : i;
			int i2 = i>0 ? i-1 : i;
			float dx = (lab_image(i1, j, c) - lab_image(i2, j, c)) / (i1-i2);
			float dy = (lab_image(i, j1, c) - lab_image(i, j2, c)) / (j1-j2);
			float a = atan2( dx, dy );
			float m = sqrt( dx*dx + dy*dy );
			// Signed binning
			if (a < 0){
				a += M_PI;
				m = -m;
			}
			a = nAngleBins*a / M_PI;
			if (a >= nAngleBins)
				a = nAngleBins-1;
			// Linear interpolation
			int a1 = a, a2 = a+1;
			float w1 = a2-a, w2 = a-a1;
			if (a2 >= nAngleBins)
				a2 = 0;
			og(i,j,a1) += w1*m;
			og(i,j,a2) += w2*m;
		}
	}
	
	// Sum upto cellSize (it's numerically more stable than an integral image)
	Image< float > tmp( lab_image.width(), lab_image.height(), nAngleBins );
	// Sum along x
	tmp.fill( 0.0 );
	const int halfCellSize = cellSize/2;
	for( int j=0; j<lab_image.height(); j++ ){
		// Current running sum
		float cnt = 0;
		float sum[nAngleBins];
		for( int k=0; k<nAngleBins; k++ )
			sum[k] = 0;
		
		for( int i=-halfCellSize; i<lab_image.width(); i++ ){
			if (i+halfCellSize < lab_image.width()){
				for( int k=0; k<nAngleBins; k++ )
					sum[k] += og(i+halfCellSize,j,k);
				cnt+= 1;
			}
				
			if (i >= 0)
				for( int k=0; k<nAngleBins; k++ )
					tmp(i,j,k) = sum[k] / cnt;
			
			if (i-halfCellSize >= 0){
				for( int k=0; k<nAngleBins; k++ )
					sum[k] -= og(i-halfCellSize,j,k);
				cnt-= 1;
			}
		}
	}
	// Sum along y [not very cache efficient, but who cares]
	og.fill( 0.0 );
	for( int i=0; i<lab_image.width(); i++ ){
		// Current running sum
		float cnt = 0;
		float sum[nAngleBins];
		for( int k=0; k<nAngleBins; k++ )
			sum[k] = 0;
		
		for( int j=-halfCellSize; j<lab_image.height(); j++ ){
			if (j+halfCellSize < lab_image.height()){
				for( int k=0; k<nAngleBins; k++ )
					sum[k] += tmp(i,j+halfCellSize,k);
				cnt+= 1;
			}
				
			if (j >= 0)
				for( int k=0; k<nAngleBins; k++ )
					og(i,j,k) = sum[k] / cnt;
			
			if (j-halfCellSize >= 0){
				for( int k=0; k<nAngleBins; k++ )
					sum[k] -= tmp(i,j-halfCellSize,k);
				cnt-= 1;
			}
		}
	}
	
	// Normalize each cell [Option 1: by it's size, Option 2: by it's norm]
// 	for( int j=0; j<lab_image.height(); j++ )
// 		for( int i=0; i<lab_image.width(); i++ ){
// 			float l2 = 0;
// 			for( int k=0; k<nAngleBins; k++ )
// 				l2 += og(i,j,k)*og(i,j,k);
// 			l2 = 1.0f / (sqrt(l2) + 0.00000001);
// 			for( int k=0; k<nAngleBins; k++ )
// 				og(i,j,k) *= l2;
// 			// Alternative normalize by 1/((min(i,width-i-1,halfCellSize)+halfCellSize)*(min(j,height-j-1,halfCellSize)+halfCellSize))
// 		}
	
	// Build the final descriptor
	Image< float > res( lab_image.width(), lab_image.height(), size() );
	const int blockCenterSize = (nCells-1)*cellSize;
	const int halfBlockCenterSize = blockCenterSize/2;
	for( int j=0; j<lab_image.height(); j++ )
		for( int i=0; i<lab_image.width(); i++ ){
			for( int jj=0; jj<nCells; jj++ )
				for( int ii=0; ii<nCells; ii++ ){
					const int cid = jj*nCells + ii;
					int x = i+ii*cellSize - halfBlockCenterSize;
					int y = j+jj*cellSize - halfBlockCenterSize;
					if (x<0) x=0; else if (x >= lab_image.width())  x=lab_image.width()-1;
					if (y<0) y=0; else if (y >= lab_image.height()) y=lab_image.height()-1;
					
					for( int k=0; k<nAngleBins; k++ )
						res(i,j,cid*nAngleBins+k) = og(x,y,k);
				}
			
			// Normalize the HoG histogram
			float l2 = 0;
			for( int k=0; k<res.depth(); k++ )
				l2 += res(i,j,k)*res(i,j,k);
			l2 = 1.0f / (sqrt(l2) + 0.00000001);
			for( int k=0; k<res.depth(); k++ )
				res(i,j,k) *= l2;
		}
	return res;
}
Exemplo n.º 25
0
bool cps_api_nodes::load_groups() {
    cps_api_object_guard og(cps_api_object_create());
    if (!cps_api_key_from_attr_with_qual(cps_api_object_key(og.get()),CPS_NODE_GROUP, cps_api_qualifier_TARGET)) {
        EV_LOG(ERR,DSAPI,0,"CPS-CLS-MAP","Meta data for cluster set is not loaded.");
        return false;
    }
    cps_db::connection_request b(cps_db::ProcessDBCache(),DEFAULT_REDIS_ADDR);

    if (!b.valid()) {
        return false;
    }

    group_data_t cpy;

    cps_api_object_list_guard lg(cps_api_object_list_create());
    if (!cps_db::get_objects(b.get(),og.get(),lg.get())) return false;

    for (size_t ix = 0,mx = cps_api_object_list_size(lg.get()); ix < mx ; ++ix ) {
        cps_api_object_t o = cps_api_object_list_get(lg.get(),ix);
        std::vector<_db_node_data> v;
        const char *name = (const char*) cps_api_object_get_data(o,CPS_NODE_GROUP_NAME);
        _node_data nd;



        cps_api_node_data_type_t *_type = (cps_api_node_data_type_t*)cps_api_object_get_data(o,CPS_NODE_GROUP_TYPE);
        if (_type==nullptr) {
            EV_LOGGING(DSAPI,ERR,"NODE-LOAD","Could not get the group type for group %s",name);
            return false;
        }
        nd.type = *_type;
        cps_api_object_attr_t _node_group = cps_api_object_attr_get(o,CPS_NODE_GROUP_NODE);
        if (_node_group==nullptr) continue; ///TODO log

        cps_api_object_it_t it;
        cps_api_object_it_from_attr(_node_group,&it);
        cps_api_object_it_inside(&it);

        while (cps_api_object_it_valid(&it)) {
            cps_api_attr_id_t id = cps_api_object_attr_id(it.attr);
            (void)id;
            cps_api_object_it_t elem = it;

            cps_api_object_it_inside(&elem);

            if (!cps_api_object_it_valid(&elem)) continue;
            _db_node_data db_node;
            cps_api_object_attr_t _ip =cps_api_object_it_find(&elem,CPS_NODE_GROUP_NODE_IP);
            cps_api_object_attr_t _name =cps_api_object_it_find(&elem,CPS_NODE_GROUP_NODE_NAME);
            if (_ip==nullptr || _name==nullptr) continue;
            const char *__ip = (const char*)cps_api_object_attr_data_bin(_ip);
            const char *__name =(const char*)cps_api_object_attr_data_bin(_name);

            if(nd.type == cps_api_node_data_1_PLUS_1_REDUNDENCY) {
                db_node._name = __name;
                if(get_port_info(name,&db_node)) {
                    auto lst = cps_string::split(std::string(__ip),":");
                    if (lst.size()!=2) {
                        return false;
                    }
                    db_node._addr = lst[0] + ':'+ db_node._addr;
                    v.push_back(std::move(db_node));
                }
                else {
                    EV_LOGGING(DSAPI,ERR,"CPS-DB","Failed to get port info for group %s and node %s",
                               name,__name);
                }

            }

            if(v.size()>0) {
                _db_node_map[name] = v;
                update_slaves(name);
            }

            _alias_map[__name] = __ip;

            const char * _alias = this->addr(__ip);
            if (_alias!=nullptr) __ip = _alias;

            nd._addrs.push_back(__ip);
            cps_api_object_it_next(&it);
        }
        cpy[name] = nd;
    }

    size_t _new_hash = cps_api_nodes::gen_hash(cpy);

    if (_new_hash==_hash) return false;  ///TODO is this unique enough?

    std::swap(_groups,cpy);
    _hash = _new_hash;
    return true;

}
cps_api_return_code_t cps_api_process_commit_request(cps_api_transaction_params_t *param, size_t ix) {
    cps_api_return_code_t rc = cps_api_ret_code_ERR;

    cps_api_object_t obj = cps_api_object_list_get(param->change_list,ix);
    cps_api_object_t pre = cps_api_object_list_get(param->prev,ix);
    if (obj==NULL) {
        EV_LOG(ERR,DSAPI,0,"CPS IPC","No Commit Object");
        return rc;
    }

    cps_api_channel_t handle;
    cps_api_key_t *key = cps_api_object_key(obj);

    char buff[CPS_API_KEY_STR_MAX];
    EV_LOG(TRACE,DSAPI,0, "CPS IPC", "Object for commit request: %s ",
                                    cps_api_key_name_print(key, buff, sizeof(buff)));


    if (!cps_api_get_handle(*key,handle)) {
        EV_LOG(ERR,DSAPI,0,"CPS IPC","No Service");
        return cps_api_ret_code_NO_SERVICE;
    }


    rc = cps_api_ret_code_ERR;

    fd_set rset;
    FD_ZERO(&rset);
    FD_SET(handle,&rset);

    do {
        if (!cps_api_send_one_object(handle,cps_api_msg_o_COMMIT_CHANGE,obj)) {
            EV_LOG(ERR,DSAPI,0,"CPS IPC","Could not send COMMIT CHANGE ");
            break;
        }

        if (!cps_api_send_one_object(handle,cps_api_msg_o_COMMIT_PREV,pre)) {
            EV_LOG(ERR,DSAPI,0,"CPS IPC","Could not send COMMIT PREV ");
            break;
        }


        uint32_t op;
        size_t len;
        if (param->timeout > 0) {
            if ((rc=cps_api_timeout_wait(handle,&rset,param->timeout,"CPS-OP-TR"))!=cps_api_ret_code_OK) {
                EV_LOG(ERR,DSAPI,0,"CPS IPC","Transaction Response timed out ");
                break;
            }
        }
        rc = cps_api_ret_code_ERR;

        if (!cps_api_receive_header(handle,op,len)) {
            EV_LOG(ERR,DSAPI,0,"CPS IPC","Failed to read the receive header ");
            break;
        }


        if (op == cps_api_msg_o_COMMIT_OBJECT) {
            cps_api_object_guard og(cps_api_receive_object(handle,len));
            if (!og.valid()) {
                EV_LOG(ERR,DSAPI,0,"CPS IPC","Transaction response missing cur object..");
                break;
            }
            cps_api_object_clone(obj,og.get());

            if (!cps_api_receive_header(handle,op,len)) {
                EV_LOG(ERR,DSAPI,0,"CPS IPC","Failed to read the receive header for prev object ");
                break;
            }

            if (op!=cps_api_msg_o_COMMIT_OBJECT) {
                EV_LOG(ERR,DSAPI,0,"CPS IPC","Transaction response header incorrect for prev object" );
                break;
            }

            if (len>0) {
                cps_api_object_guard og(cps_api_receive_object(handle,len));
                if (!og.valid()) {
                    EV_LOG(ERR,DSAPI,0,"CPS IPC","Transaction response missing resp object..");
                    break;
                }
                obj = cps_api_object_list_get(param->prev,ix);

                if (obj==NULL) { //if there was no previous object passed by client override
                    if (cps_api_object_list_append(param->prev,og.get())) {
                        og.release();
                        rc = cps_api_ret_code_OK;
                    }
                } else {
                    //take the previous provided by the client if the key is valid
                    //assume that the data is valid too
                    if (cps_api_key_get_len(cps_api_object_key(og.get()))>0) {
                        cps_api_object_clone(obj,og.get());
                    }
                    rc = cps_api_ret_code_OK;
                }
            }
        }
        if (op == cps_api_msg_o_RETURN_CODE) {
            if (!cps_api_receive_data(handle,&rc,sizeof(rc))) break;
        }
    } while (0);

    cps_api_disconnect_owner(handle);

    return rc;
}