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
sample2.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 <fstream>
24 #include <iostream>
25 #include <iomanip>
26 #include <map>
27 using namespace std;
28 
29 #ifdef WINDOWS
30 #include <winsock2.h>
31 #include <ws2tcpip.h>
32 #include <conio.h>
33 #include <process.h>
34 #include "XEventLog.h"
35 #else
36 #include <sys/socket.h>
37 #include <arpa/inet.h>
38 #include <unistd.h>
39 #include <netinet/in.h>
40 #include <stdlib.h>
41 #endif
42 using namespace std;
43 #include <stdlib.h>
44 #include <time.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include "sample2.h"
48 #include "generic_plugin.h"
49 #include "generic_server.h"
50 
51 #ifdef DLLAPI
52 extern "C" __declspec(dllexport) sample2_plugin *create_instance(void);
53 #else
54 extern "C" sample2_plugin *create_instance(void);
55 #endif
56 
57 sample2_plugin::sample2_plugin(void) : generic_plugin()
58 {
59  ftp_dir = "";
60 }
61 /*********************************************************************************************/
62 
63 sample2_plugin::sample2_plugin(char *plugin_name,int plugin_port) : generic_plugin(plugin_name,plugin_port)
64 {
65  ftp_dir = "";
66 }
67 /*********************************************************************************************/
68 
69 #ifdef DLLAPI
70 extern "C" __declspec(dllexport) sample2_plugin *create_instance(void)
71 #else
72 extern "C" sample2_plugin *create_instance(void)
73 #endif
74 {
75  return(new(SAMPLE2));
76 }
77 /*********************************************************************************************/
78 
79 int sample2_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 sample2_plugin &sample2_plugin::operator=(const sample2_plugin &rhs)
99 {
100  if(this != &rhs)
101  {
102  generic_plugin::operator= (rhs);
103  ftp_dir = rhs.ftp_dir;
104  }
105  return(*this);
106 }
107 /*********************************************************************************************/
108 
110 {
111  ostringstream oss;
113 
114  try
115  {
116 // Do plug-in cleanup here..
117  oss << " Thr.ID:" << thread_id << " Plugin:" << plugin_name << " Successfully shutdown session.";
118  log(LOG_HI,oss.str());
119  }
120  catch(const exception& er)
121  {
122  oss << " Thr.ID: MAIN Plugin:" << plugin_name << " " << er.what();
123  log(LOG_HI,oss.str());
124  return 0;
125  }
126  return(1);
127 }
128 /*********************************************************************************************/
129 
131 {
132  ostringstream oss;
133 
134  try
135  {
136  oss << " Thr.ID: MAIN Plugin:" << plugin_name << " Successfully initialized sample2 server.";
137  log(LOG_HI,oss.str());
138  return(1);
139  }
140  catch(const exception& er)
141  {
142  oss << " Thr.ID: MAIN Plugin:" << plugin_name << " " << er.what();
143  log(LOG_HI,oss.str());
144  return(0);
145  }
146 }
147 /*********************************************************************************************/
148 
150 {
151  ostringstream oss;
152 
153  try
154  {
155  oss << " Thr.ID: MAIN Plugin:" << plugin_name << " Successfully shutdown sample2 server.";
156  log(LOG_HI,oss.str());
157  return(1);
158  }
159  catch(const exception& er)
160  {
161  oss << " Thr.ID: MAIN Plugin:" << plugin_name << " " << er.what();
162  log(LOG_HI,oss.str());
163  return(0);
164  }
165 }
166 /*********************************************************************************************/
167 
168 int sample2_plugin::process_request(void *buffer, void *out_buff,unsigned int &size)
169 {
171  ostringstream oss;
172  unsigned char *req_type;
173  // Simple file transfer messaging
174  // REQUEST_TYPE - 1 byte
175  // PLUGIN_DATA - variable/optional,based on REQUEST_TYPE
176  // REQUEST_TYPE:
177  // 1 - set file name; plugin will create a file on server on receipt of this request
178  // Example: 1file_to_transfer.jpg
179  // 2 - file data; plugin will append data to file created earlier
180  // Example: 2file_data
181  // 3 - close file; plugin will close file created
182 
183  req_type = static_cast<unsigned char *>(buffer);
184  switch(*req_type)
185  {
186  case SET_FILENAME:
187  fname = ftp_dir + "/" + string((char *)buffer+1,size-1);
188  new_file.open(fname.c_str(),ios::binary);
189  if(!new_file.rdbuf()->is_open())
190  {
191  size = 0;
192  return(TERMINATE_SESSION);
193  }
194  oss.clear();
195  oss.str("");
196  oss << " Thr.ID:" << thread_id << " Plugin:" << plugin_name << " Creating new file: " << fname;
197  log(LOG_LOW,oss.str());
198  size = 0;
199  return(CONTINUE_SESSION);
200 
201  case FILE_DATA:
202  new_file << string((char *)buffer+1,size-1);
203  return(CONTINUE_SESSION);
204 
205  case CLOSE_FILE:
206  new_file.close();
207  oss.clear();
208  oss.str("");
209  oss << " Thr.ID:" << thread_id << " Plugin:" << plugin_name << " Closing file: " << fname;
210  log(LOG_LOW,oss.str());
211  size = 0;
212  return(TERMINATE_SESSION);
213 
214  default:
215  return(TERMINATE_SESSION);
216  }
217 return(CONTINUE_SESSION);
218 }
219 /**********************************************************************************************/
220 
221 string sample2_plugin::get_plugin_version(void)
222 {
223  return(PLUGIN_VERSION);
224 }
225 /**********************************************************************************************/
226 
228 {
230  if (line.size() > 8 && line.compare(0,8,"FTP_DIR=") == 0)
231  {
232  ftp_dir = line.substr(8);
233  return(0);
234  }
235  return(1);
236 }
237 /*********************************************************************************************/
238 
This is a plug-in to demostrate usage of generic_server. This class is derived from GENERIC_PLUGIN...
int plugin_init(int)
Definition: sample2.cpp:79
Singleton class to manage framework state and provide utility functions. This class stores master Vec...
This is a singleton class and provides framework functionality.
int process_request(void *, void *, unsigned int &)
Definition: sample2.cpp:168
This component provides functionality that are common across plug-ins. Framework would instantiate an...
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.
int shutdown_plugin(void)
Definition: sample2.cpp:109
int server_init(void)
Definition: sample2.cpp:130
unsigned long pinstance
Pointer to 'framework' object.
int get_plugin_params(string line)
Definition: sample2.cpp:227
int server_shutdown(void)
Definition: sample2.cpp:149
Derived from GENERIC_PLUGIN. Implements virtual functions and all plug-in specific functionality...
Definition: sample2.h:41
virtual int get_plugin_params(string line)