34 #define close closesocket 35 #define pipe(phandles) _pipe(phandles, 4096, _O_BINARY) 38 #include <sys/types.h> 39 #include <sys/socket.h> 40 #include <arpa/inet.h> 42 #include <netinet/tcp.h> 43 #include <netinet/in.h> 47 #define OWARN ossimNotify(ossimNotifyLevel_WARN) 48 #define OINFO ossimNotify(ossimNotifyLevel_INFO) 49 #define MAX_BUF_LEN 4096 50 #define FORK_PROCESS false 71 bool status_ok =
true;
74 OINFO<<
"Waiting for connections...\n"<<endl;
78 struct sockaddr_in cli_addr;
79 clilen =
sizeof(cli_addr);
82 error(
"Error accepting message on port.");
87 getnameinfo((
struct sockaddr *) &cli_addr, clilen, clientname, 256, clientport, 256, 0);
88 cout<<
"ossimToolServer: Got connection from "<<clientname<<
":"<<clientport
89 <<
" Forking child process..."<<endl;
102 bool connected =
true;
111 bool connected =
true;
124 printf(
"recv returned -1 from port %d.\n",
m_clisockfd);
129 string msg =
"server received message: ";
133 error(
"ERROR writing to socket");
144 struct addrinfo hints;
145 memset(&hints, 0,
sizeof hints);
146 hints.ai_family = AF_INET;
147 hints.ai_socktype = SOCK_STREAM;
148 hints.ai_flags = AI_PASSIVE;
149 struct addrinfo *res;
151 int failed = getaddrinfo(NULL, portid, &hints, &res);
153 error(gai_strerror(failed));
156 struct addrinfo *server_info = res;
159 m_svrsockfd = socket(server_info->ai_family, server_info->ai_socktype, server_info->ai_protocol);
162 server_info = server_info->ai_next;
165 error(
"Error opening socket");
169 if (setsockopt(
m_svrsockfd,SOL_SOCKET,SO_REUSEADDR,&yes,
sizeof yes) == -1)
170 error(
"Error on setsockopt() call");
172 #if defined (__APPLE__) || defined(__FreeBSD__) 173 int bindResult = ::bind(
m_svrsockfd, server_info->ai_addr, server_info->ai_addrlen);
175 int bindResult = bind(
m_svrsockfd, server_info->ai_addr, server_info->ai_addrlen);
179 error(
"Error on binding to socket:port.");
181 struct sockaddr_in *server_addr = (sockaddr_in*) &(server_info->ai_addr);
183 OINFO<<
"ossimToolServer daemon started. Listening on port "<<portid<<
". Process ID: "<<getpid()<<
"\n"<<endl;
184 freeaddrinfo(server_info);
188 error(
"Error on listen()");
193 sigemptyset(&sa.sa_mask);
194 sa.sa_flags = SA_RESTART;
195 if (sigaction(SIGCHLD, &sa, NULL) == -1)
196 error(
"Error on sigaction()");
202 int saved_errno = errno;
203 while(waitpid(-1, NULL, WNOHANG) > 0);
215 int remaining = bufsize;
225 error(
"ERROR writing to socket");
238 xmsg<<
"ossimToolServer.sendFile() -- Error opening file <"<<fname<<
">."<<endl;
239 error(xmsg.str().c_str());
243 std::streampos fsize = svrfile.tellg();
244 svrfile.seekg( 0, std::ios::end );
245 fsize = svrfile.tellg() -fsize;
246 svrfile.seekg( 0, std::ios::beg );
249 char size_response[19];
250 sprintf(size_response,
"SIZE: %012d", (
int) fsize);
251 if (
_DEBUG_) cout<<
"ossimToolServer:"<<__LINE__<<
" sending <"<<size_response<<
">"<<endl;
257 char name_response[256];
258 memset(name_response, 0, 256);
259 sprintf(name_response,
"NAME: %s", fname.
file().
chars());
260 if (
_DEBUG_) cout<<
"ossimToolServer:"<<__LINE__<<
" sending <"<<name_response<<
">"<<endl;
270 if (
_DEBUG_) cout<<
"ossimToolServer:"<<__LINE__<<
" sending binary data..."<<endl;
271 while (!svrfile.eof())
276 error(
"ossimToolServer.sendFile() -- Error during file read()");
278 n = svrfile.gcount();
287 cout <<
"Send complete."<<endl;
293 if (
_DEBUG_) cout<<
"ossimToolServer:"<<__LINE__<<
" Waiting to recv"<<endl;
295 if (
_DEBUG_) cout<<
"ossimToolServer:"<<__LINE__<<
" Received <"<<
m_buffer<<
">"<<endl;
297 error(
"ossimToolServer: EOF encountered reading from port");
300 if (
_DEBUG_) cout <<
"Send acknowledged by client."<<endl;
307 bool status_ok =
false;
308 static const char* msg =
"\nossimToolServer.runCommand(): ";
311 if (command ==
"sendfile")
314 const char* response =
"FILE ";
322 int pipeDesc[2] = {0,0};
323 int savedStdout = dup( fileno(stdout) );
324 if( pipe( pipeDesc ) == -1 )
325 error(
"Could not redirect stdout (1).");
326 setbuf( stdout, NULL );
327 dup2( pipeDesc[1], fileno(stdout) );
331 ioctlsocket(pipeDesc[0], FIONBIO, &iMode);
333 fcntl( pipeDesc[0], F_SETFL, O_NONBLOCK );
348 command = c2 +
" --help";
352 map<string, string> capabilities;
354 map<string, string>::iterator iter = capabilities.begin();
355 cout<<
"\nAvailable commands:\n"<<endl;
356 for (;iter != capabilities.end(); ++iter)
357 cout<<
" "<<iter->first<<
" -- "<<iter->second<<endl;
358 cout<<
"\nUse option \"--help\" with above commands to get detailed tool command."<<endl;
371 if (!utility.
valid())
372 cout<<msg<<
"Did not understand command <"<<util_name<<
">"<<endl;
374 cout<<msg<<
"Could not execute command sequence <"<<command<<
">."<<endl;
376 cout<<msg<<
"Error encountered executing\n <"<<command <<
">\nCheck options."<<endl;
382 cout << msg <<
"Caught OSSIM exception: "<<
x.what()<<endl;
386 cout << msg <<
"Caught unknown exception: "<<
x.what()<<endl;
393 dup2( savedStdout, fileno(stdout) );
407 const char* response =
"FILE ";
415 const char* response =
"TEXT ";
417 writeSocket(full_output.c_str(), full_output.size());
419 error(
"ERROR receiving acknowledge from client.");
424 const char* response =
"ERROR";
426 writeSocket(full_output.c_str(), full_output.size());
427 cout <<
"Sending ERROR to client and closing connection: <"<<full_output<<
">"<<endl;
436 char dst[INET6_ADDRSTRLEN];
439 cout <<
"\nprocessOssimRequest() -- Process ID: "<<getpid()<<endl;
440 cout <<
" Parent process ID: "<<getppid()<<endl;
444 if (
_DEBUG_) cout<<
"ossimToolServer:"<<__LINE__<<
" Waiting to recv"<<endl;
446 if (
_DEBUG_) cout<<
"ossimToolServer:"<<__LINE__<<
" Received <"<<
m_buffer<<
">"<<endl;
448 error(
"ossimToolServer: EOF encountered reading from port");
454 void* addr = &(cli_addr.sin_addr);
455 if (!inet_ntop(AF_INET, addr, dst, INET6_ADDRSTRLEN))
456 error(
"ossimToolServer: Error returned from inet_ntop(). ");
457 cout <<
"\nossimToolServer: received message from: "<<dst<<
"\n---------------\n"<<
m_buffer 458 <<
"\n---------------\n"<< endl;
464 if (command ==
"goodbye")
ossimString before(const ossimString &str, std::string::size_type pos=0) const
METHOD: before(str, pos) Returns string beginning at pos and ending one before the token str If strin...
std::basic_ostringstream< char > ostringstream
Class for char output memory streams.
std::basic_ifstream< char > ifstream
Class for char input file streams.
os2<< "> n<< " > nendobj n
const char * chars() const
For backward compatibility.
ossimString trim(const ossimString &valueToTrim=ossimString(" \\)) const
this will strip lead and trailing character passed in.
ossimFilename file() const
ossimString after(const ossimString &str, std::string::size_type pos=0) const
METHOD: after(str, pos) Returns string immediately after the token str.