Fledge
An open source edge computing platform for industrial users
python_plugin_handle.h
1 
2 #ifndef _PYTHON_PLUGIN_HANDLE_H
3 #define _PYTHON_PLUGIN_HANDLE_H
4 /*
5  * Fledge Base plugin handle class
6  *
7  * Copyright (c) 2019 Dianomic Systems
8  *
9  * Released under the Apache 2.0 Licence
10  *
11  * Author: Amandeep Singh Arora, Massimiliano Pinto
12  */
13 #include <logger.h>
14 #include <vector>
15 #include <sstream>
16 #include <dlfcn.h>
17 #include <plugin_handle.h>
18 #include <Python.h>
19 
20 typedef void* (*pluginResolveSymbolFn)(const char *, const std::string&);
21 typedef void (*pluginCleanupFn)(const std::string&);
22 
28 {
29  public:
30  // Base constructor
31  PythonPluginHandle(const char *name, const char *path) : m_name(name) {};
32 
39  {
40  if (!m_hndl)
41  {
42  return;
43  }
44  pluginCleanupFn cleanupFn =
45  (pluginCleanupFn) dlsym(m_hndl, "PluginInterfaceCleanup");
46  if (cleanupFn == NULL)
47  {
48  // Unable to find PluginInterfaceCleanup entry point
49  Logger::getLogger()->error("Plugin library %s does not support %s function : %s",
50  m_interfaceObjName.c_str(),
51  "PluginInterfaceCleanup",
52  dlerror());
53  }
54  else
55  {
56  cleanupFn(m_name);
57  }
58  dlclose(m_hndl);
59  m_hndl = NULL;
60  };
61 
68  void *ResolveSymbol(const char* sym)
69  {
70  if (!m_hndl)
71  {
72  return NULL;
73  }
74  pluginResolveSymbolFn resolvSymFn =
75  (pluginResolveSymbolFn) dlsym(m_hndl, "PluginInterfaceResolveSymbol");
76  if (resolvSymFn == NULL)
77  {
78  // Unable to find PluginInterfaceResolveSymbol entry point
79  Logger::getLogger()->error("Plugin library %s does not support "
80  "%s function : %s",
81  m_interfaceObjName.c_str(),
82  "PluginInterfaceResolveSymbol",
83  dlerror());
84  return NULL;
85  }
86  void *rv = resolvSymFn(sym, m_name);
87  if (!rv)
88  {
89  // Python filter plugins do not support plugin_start
90  // just log a debug message
91  if (m_type.compare(PLUGIN_TYPE_FILTER) == 0)
92  {
93  Logger::getLogger()->debug("PythonPluginHandle::ResolveSymbol "
94  "returning NULL for sym=%s, plugin %s, type %s",
95  sym,
96  m_name.c_str(),
97  m_type.c_str());
98  }
99  else
100  {
101  Logger::getLogger()->error("PythonPluginHandle::ResolveSymbol "
102  "returning NULL for sym=%s, plugin %s, type %s",
103  sym,
104  m_name.c_str(),
105  m_type.c_str());
106  }
107  }
108 
109  return rv;
110  };
111 
116  void *GetInfo()
117  {
118  return (void *) ResolveSymbol("plugin_info");
119  };
120 
121  // Return pointer to this class
122  void *getHandle() { return this; }
123  public:
124  // The python plugin interface library shared object
125  void* m_hndl;
126  // The interface library name to load
127  std::string m_interfaceObjName;
128 
129  // Set plugin name
130  std::string m_name;
131 
132  // plugin type
133  std::string m_type;
134 };
135 
136 #endif
The PythonPluginHandle class is the base class used to represent an interface to a plugin that is ava...
Definition: python_plugin_handle.h:27
~PythonPluginHandle()
Base destructor.
Definition: python_plugin_handle.h:38
void * GetInfo()
Returns function pointer that can be invoked to call &#39;plugin_info&#39; function in python plugin...
Definition: python_plugin_handle.h:116
void * ResolveSymbol(const char *sym)
Gets function pointer from loaded python interface library that can be invoked to call &#39;sym&#39; function...
Definition: python_plugin_handle.h:68
The PluginHandle class is used to represent an opaque handle to a plugin instance.
Definition: plugin_handle.h:24