<commit_before>/* -*- C++ -*-; c-basic-offset: 4; indent-tabs-mode: nil */
/*
 * Implementation for Agent class
 *
 * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <boost/assign/list_of.hpp>
#include <boost/algorithm/string/join.hpp>
#include <modelgbp/dmtree/Root.hpp>

#include <opflexagent/cmd.h>
#include <opflexagent/Agent.h>
#include <opflexagent/FSEndpointSource.h>
#include <opflexagent/ModelEndpointSource.h>
#include <opflexagent/FSServiceSource.h>
#include <opflexagent/FSRDConfigSource.h>
#include <opflexagent/logging.h>

#include <opflexagent/Renderer.h>

#include <unordered_map>
#include <cstdlib>
#include <mutex>
#include <condition_variable>
#include <chrono>

#include <dlfcn.h>

namespace opflexagent {

using std::thread;
using std::make_pair;
using opflex::modb::ModelMetadata;
using opflex::modb::Mutator;
using opflex::ofcore::OFFramework;
using boost::property_tree::ptree;
using boost::optional;
using boost::asio::io_service;

Agent::Agent(OFFramework& framework_)
    : framework(framework_), policyManager(framework),
      endpointManager(framework, policyManager), notifServer(agent_io),
      started(false) {

}

Agent::~Agent() {
    stop();
    renderers.clear();
    for (auto handle : rendPluginHandles) {
        dlclose(handle);
    }
}

#define DEF_INSPECT_SOCKET LOCALSTATEDIR"/run/opflex-agent-ovs-inspect.sock"
#define DEF_NOTIF_SOCKET LOCALSTATEDIR"/run/opflex-agent-ovs-notif.sock"

Renderer* disabled_create(Agent& agent) {
    return NULL;
}

void Agent::loadPlugin(const std::string& name) {
    if (rendPluginLibs.find(name) != rendPluginLibs.end())
        return;

    void* handle = dlopen(name.c_str(), RTLD_NOW);
    if (handle == NULL) {
        LOG(ERROR) << "Failed to load renderer plugin "
                   << "\"" << name << "\":" << dlerror();
        return;
    }
    auto init =
        (renderer_init_func)(dlsym(handle, "init_renderer_plugin"));
    if (init == NULL) {
        LOG(ERROR) << "Could not load renderer plugin "
                   << "\"" << name << "\" init function symbol:"
                   << dlerror();
        dlclose(handle);
        return;<commit_msg>Fix default socket name for Agent when configuration is blank.<commit_after>/* -*- C++ -*-; c-basic-offset: 4; indent-tabs-mode: nil */
/*
 * Implementation for Agent class
 *
 * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <boost/assign/list_of.hpp>
#include <boost/algorithm/string/join.hpp>
#include <modelgbp/dmtree/Root.hpp>

#include <opflexagent/cmd.h>
#include <opflexagent/Agent.h>
#include <opflexagent/FSEndpointSource.h>
#include <opflexagent/ModelEndpointSource.h>
#include <opflexagent/FSServiceSource.h>
#include <opflexagent/FSRDConfigSource.h>
#include <opflexagent/logging.h>

#include <opflexagent/Renderer.h>

#include <unordered_map>
#include <cstdlib>
#include <mutex>
#include <condition_variable>
#include <chrono>

#include <dlfcn.h>

namespace opflexagent {

using std::thread;
using std::make_pair;
using opflex::modb::ModelMetadata;
using opflex::modb::Mutator;
using opflex::ofcore::OFFramework;
using boost::property_tree::ptree;
using boost::optional;
using boost::asio::io_service;

Agent::Agent(OFFramework& framework_)
    : framework(framework_), policyManager(framework),
      endpointManager(framework, policyManager), notifServer(agent_io),
      started(false) {

}

Agent::~Agent() {
    stop();
    renderers.clear();
    for (auto handle : rendPluginHandles) {
        dlclose(handle);
    }
}

#define DEF_INSPECT_SOCKET LOCALSTATEDIR"/run/opflex-agent-inspect.sock"
#define DEF_NOTIF_SOCKET LOCALSTATEDIR"/run/opflex-agent-notif.sock"

Renderer* disabled_create(Agent& agent) {
    return NULL;
}

void Agent::loadPlugin(const std::string& name) {
    if (rendPluginLibs.find(name) != rendPluginLibs.end())
        return;

    void* handle = dlopen(name.c_str(), RTLD_NOW);
    if (handle == NULL) {
        LOG(ERROR) << "Failed to load renderer plugin "
                   << "\"" << name << "\":" << dlerror();
        return;
    }
    auto init =
        (renderer_init_func)(dlsym(handle, "init_renderer_plugin"));
    if (init == NULL) {
        LOG(ERROR) << "Could not load renderer plugin "
                   << "\"" << name << "\" init function symbol:"
                   << dlerror();
        dlclose(handle);
        return;
{'fraction_non_alphanumeric': 0.13968957871396895, 'fraction_numerical': 0.0036283007458173754, 'mean_word_length': 3.9177403369672943, 'pattern_counts': {'":': 2, '<': 77, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7113568', 'n_tokens_mistral': 1630, 'n_tokens_neox': 1592, 'n_words': 360}