source: trunk/FACT++/dim/src/examples/rshServer.cxx@ 11189

Last change on this file since 11189 was 11071, checked in by tbretz, 13 years ago
Replaced v19r21 by a version extracted with 'unzip -a' to get proper unix text format.
File size: 3.1 KB
Line 
1#include <dis.hxx>
2#include <sys/stat.h>
3#include <sys/types.h>
4#ifndef WIN32
5#include <sys/wait.h>
6#include <unistd.h>
7#else
8#include <process.h>
9#endif
10#include <ctype.h>
11
12#ifndef WIN32
13char outfile[] = "/tmp/dim_rsh_server.dat";
14#else
15char outfile[] = "c:\\dim_rch_server.dat";
16#endif
17
18int my_system(char *);
19
20class Cmd: public DimCommand
21{
22 char *result;
23 DimService *resultSrvc;
24
25 void commandHandler();
26public:
27 Cmd(char *cmdName, char *resultName): DimCommand(cmdName, "C")
28 {
29 result = new char[10];
30 strcpy(result,"empty");
31 resultSrvc = new DimService(resultName, "C", result, strlen(result)+1);
32 };
33};
34
35void Cmd::commandHandler()
36{
37 char *str, *client;
38 char commandString[256];
39 struct stat buf;
40 FILE *fd;
41 int ret, size, index, sz;
42
43 str = getString();
44
45 client = DimServer::getClientName();
46
47 cout << "Received " << str << " from " << client << endl;
48 unlink(outfile);
49 strcpy(commandString,str);
50#ifndef WIN32
51 strcat(commandString," >& ");
52 strcat(commandString,outfile);
53 strcat(commandString,"< /dev/null");
54#else
55 strcat(commandString," > ");
56 strcat(commandString,outfile);
57 strcat(commandString," 2>&1 ");
58#endif
59 my_system(commandString);
60
61 delete result;
62 ret = stat(outfile, &buf);
63 if(ret == -1)
64 {
65 result = new char[20];
66 strcpy(result,"File does not exist");
67 }
68 else
69 {
70 size = buf.st_size;
71 result = new char[size +1];
72 fd = fopen(outfile, "r");
73 index = 0;
74 while(!feof(fd) && size)
75 {
76 sz = fread(&result[index], 1, 512, fd);
77 size -= sz;
78 index += sz;
79 if(!sz)
80 break;
81 }
82 fclose(fd);
83 result[index] = '\0';
84 }
85 resultSrvc->updateService(result, strlen(result)+1);
86}
87
88#ifndef WIN32
89
90extern char **environ;
91
92int my_system (char *command)
93{
94 int pid, status, ret, n = 0;
95
96 if (command == 0)
97 return 1;
98 pid = fork();
99 if (pid == -1)
100 return -1;
101 if (pid == 0) {
102 char *argv[4];
103 argv[0] = "sh";
104 argv[1] = "-c";
105 argv[2] = command;
106 argv[3] = 0;
107 execve("/bin/sh", argv, environ);
108 exit(127);
109 }
110 do {
111 ret = waitpid(pid, &status,WNOHANG);
112 if(ret == -1)
113 break;
114 usleep(100000);
115 n++;
116 } while(n < 1200);
117 return status;
118}
119
120#else
121int my_system (char *command)
122{
123 return system(command);
124}
125
126#endif
127
128#ifdef WIN32
129int init_sock()
130{
131 WORD wVersionRequested;
132 WSADATA wsaData;
133 int err;
134 static int sock_init_done = 0;
135
136 if(sock_init_done) return(1);
137 wVersionRequested = MAKEWORD( 2, 0 );
138 err = WSAStartup( wVersionRequested, &wsaData );
139
140 if ( err != 0 ) {
141 return(0);
142 }
143
144 if ( LOBYTE( wsaData.wVersion ) != 2 ||
145 HIBYTE( wsaData.wVersion ) != 0 ) {
146
147 WSACleanup( );
148 return(0);
149 }
150 sock_init_done = 1;
151 return(1);
152}
153#endif
154
155void main(int argc, char **argv)
156{
157 char host[64], cmd_name[128], result_name[128];
158 Cmd *cmd;
159
160#ifdef WIN32
161 init_sock();
162#endif
163 gethostname(host, 64);
164
165//Setup command reception and result publishing
166 sprintf(cmd_name,"%s/ExecuteCmd",host);
167 sprintf(result_name,"%s/CmdResult", host);
168 cmd = new Cmd(cmd_name, result_name);
169
170 sprintf(cmd_name,"%s_server",host);
171 DimServer::start(cmd_name);
172 while(1)
173 {
174 pause();
175 }
176}
Note: See TracBrowser for help on using the repository browser.