GENERIC_SERVER  0.0.0.9
A light-weight, cross-platform, pluggable, extensible and secure framework for deploying C++ plug-ins.
 All Classes Files Functions Variables Typedefs Pages
sample.cpp
1 /*
2  Copyright 2013 Broadcom Corporation
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License version 2.1 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Lesser General Public License for more details.
12 
13  You should have received a copy of the GNU Lesser General Public
14  License along with this library; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include <iostream>
19 #include <algorithm>
20 #include <vector>
21 #include <string>
22 #include <sstream>
23 #include <iomanip>
24 #include <map>
25 using namespace std;
26 
27 #ifdef WINDOWS
28 #include <winsock2.h>
29 #include <ws2tcpip.h>
30 #include <conio.h>
31 #include <process.h>
32 #include "XEventLog.h"
33 #else
34 #include <sys/socket.h>
35 #include <arpa/inet.h>
36 #include <unistd.h>
37 #include <netinet/in.h>
38 #include <stdlib.h>
39 #endif
40 using namespace std;
41 #include <stdlib.h>
42 #include <time.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include "sample.h"
46 #include "generic_plugin.h"
47 #include "generic_server.h"
48 
49 #ifdef DLLAPI
50 extern "C" __declspec(dllexport) sample_plugin *create_instance(void);
51 #else
52 extern "C" sample_plugin *create_instance(void);
53 #endif
54 
55 sample_plugin::sample_plugin(void) : generic_plugin()
56 {
57  db_name = "";
58  db_passwd = "";
59 }
60 /*********************************************************************************************/
61 
62 sample_plugin::sample_plugin(char *plugin_name,int plugin_port) : generic_plugin(plugin_name,plugin_port)
63 {
64  db_name = "";
65  db_passwd = "";
66 }
67 /*********************************************************************************************/
68 
69 #ifdef DLLAPI
70 extern "C" __declspec(dllexport) sample_plugin *create_instance(void)
71 #else
72 extern "C" sample_plugin *create_instance(void)
73 #endif
74 {
75  return(new(SAMPLE));
76 }
77 /*********************************************************************************************/
78 
79 int sample_plugin::plugin_init(int thread_no)
80 {
81  ostringstream oss;
82 
83  try
84  {
85  oss << " Thr.ID:" << thread_no << " Plugin:" << plugin_name << " Successfully initialized session.";
86  log(LOG_HI,oss.str());
87  return(1);
88  }
89  catch(const exception& er)
90  {
91  oss << " Thr.ID:" << thread_no << " Plugin:" << plugin_name << " " << er.what();
92  log(LOG_HI,oss.str());
93  return(0);
94  }
95 }
96 /*********************************************************************************************/
97 
98 sample_plugin &sample_plugin::operator=(const sample_plugin &rhs)
99 {
100  if(this != &rhs)
101  {
102  generic_plugin::operator= (rhs);
103  db_name = rhs.db_name;
104  db_passwd = rhs.db_passwd;
105  }
106  return(*this);
107 }
108 /*********************************************************************************************/
109 
111 {
112  ostringstream oss;
114 
115  try
116  {
117 // Do plug-in cleanup here..
118  oss << " Thr.ID:" << thread_id << " Plugin:" << plugin_name << " Successfully shutdown session.";
119  log(LOG_HI,oss.str());
120  }
121  catch(const exception& er)
122  {
123  oss << " Thr.ID: MAIN Plugin:" << plugin_name << " " << er.what();
124  log(LOG_HI,oss.str());
125  return 0;
126  }
127  return(1);
128 }
129 /*********************************************************************************************/
130 
132 {
133  ostringstream oss;
134 
135  try
136  {
137  oss << " Thr.ID: MAIN Plugin:" << plugin_name << " Successfully initialized sample server.";
138  log(LOG_HI,oss.str());
139  return(1);
140  }
141  catch(const exception& er)
142  {
143  oss << " Thr.ID: MAIN Plugin:" << plugin_name << " " << er.what();
144  log(LOG_HI,oss.str());
145  return(0);
146  }
147 }
148 /*********************************************************************************************/
149 
151 {
152  ostringstream oss;
153 
154  try
155  {
156  oss << " Thr.ID: MAIN Plugin:" << plugin_name << " Successfully shutdown sample server.";
157  log(LOG_HI,oss.str());
158  return(1);
159  }
160  catch(const exception& er)
161  {
162  oss << " Thr.ID: MAIN Plugin:" << plugin_name << " " << er.what();
163  log(LOG_HI,oss.str());
164  return(0);
165  }
166 }
167 /*********************************************************************************************/
168 
169 int sample_plugin::process_request(void *buffer, void *out_buff,unsigned int &size)
170 {
172  ostringstream oss;
173 
174  memset(out_buff,0,MAX_SZ);
175  oss << " Thr.ID:" << thread_id << " Plugin:" << plugin_name << " Received: " << string((char *)buffer,size);
176  log(LOG_LOW,oss.str());
177  if(!memcmp(buffer,"QUIT",4))
178  {
179  size = 12;
180  memcpy((char *)out_buff,"00 TERMINATE",size);
181  return(TERMINATE_SESSION);
182  }
183  size = 22;
184  memcpy((char *)out_buff,"response from plugin..",size);
185  return(CONTINUE_SESSION);
186 }
187 /**********************************************************************************************/
188 
189 string sample_plugin::get_plugin_version(void)
190 {
191  return(PLUGIN_VERSION);
192 }
193 /**********************************************************************************************/
194 
196 {
198  if (line.size() > 8 && line.compare(0,8,"DB_NAME=") == 0)
199  {
200  db_name = line.substr(8);
201  return(0);
202  }
203  if (line.size() > 10 && line.compare(0,10,"DB_PASSWD=") == 0)
204  {
205  db_passwd = line.substr(10);
206  return(0);
207  }
208  return(1);
209 }
210 /*********************************************************************************************/
211 
213 {
214  return(string("INIT_SMART_CARD"));
215 }
216 /*****************************************************************************************************/
217 
218 bool sample_plugin::bootstrap_init(string bootstrap_name)
219 {
220  // Do all bootstrapping stuff here. Load dlls,shared libs, initialize 3rd party apps etc
221  // Don't bother about thread sync here, Framework will take care of it!
222  return(true);
223 }
224 /*****************************************************************************************************/
225 
226 bool sample_plugin::bootstrap_terminate(string bootstrap_name)
227 {
228  // Do all bootstrapping cleanup stuff here. Unload dlls,shared libs,3rd party apps etc
229  // Don't bother about thread sync here, Framework will take care of it!
230  return(true);
231 }
232 /*****************************************************************************************************/
int process_request(void *, void *, unsigned int &)
Definition: sample.cpp:169
Singleton class to manage framework state and provide utility functions. This class stores master Vec...
int get_plugin_params(string line)
Definition: sample.cpp:195
This is a singleton class and provides framework functionality.
string bootstrap_name(void)
Definition: sample.cpp:212
Derived from GENERIC_PLUGIN. Implements virtual functions and all plug-in specific functionality...
Definition: sample.h:38
This component provides functionality that are common across plug-ins. Framework would instantiate an...
bool bootstrap_init(string)
Definition: sample.cpp:218
bool bootstrap_terminate(string)
Definition: sample.cpp:226
int shutdown_plugin(void)
Definition: sample.cpp:110
int server_shutdown(void)
Definition: sample.cpp:150
This is the base class for all plugins. All plug-ins should derive from this class. This class has a bunch of virtual functions that all plug-in could/should implement. In addition to virtual functions, this class also provides a lot of utility functions for all plug-ins.
This is a plug-in to demostrate usage of generic_server. This class is derived from GENERIC_PLUGIN...
unsigned long pinstance
Pointer to 'framework' object.
int plugin_init(int)
Definition: sample.cpp:79
int server_init(void)
Definition: sample.cpp:131
virtual int get_plugin_params(string line)