r136: This commit was manufactured by cvs2svn to create tag 'hv_1_1_8'.
[cinelerra_cv/mob.git] / hvirtual / cinelerra / renderfarmfsserver.C
blob44b09588dc63317aeba2982fa3e32f8c87d84e24
1 #include "renderfarm.h"
2 #include "renderfarmfsserver.h"
3 #include "units.h"
6 #include <string.h>
7 #include <sys/stat.h>
9 #define DEBUG 0
11 RenderFarmFSServer::RenderFarmFSServer(RenderFarmServerThread *server)
13         this->server = server;
18 RenderFarmFSServer::~RenderFarmFSServer()
22 void RenderFarmFSServer::initialize()
26 int RenderFarmFSServer::handle_request(int request_id, int request_size, char *buffer)
28         int result = 0;
30 if(DEBUG)
31 printf("RenderFarmFSServer::handle_request request_id=%d\n", request_id);
32         switch(request_id)
33         {
34                 case RENDERFARM_FOPEN:
35                 {
36                         char *path;
37                         char *mode;
38                         FILE *file;
39                         unsigned char datagram[8];
40                         int i = 0;
41                         int64_t file_int64;
44                         path = buffer;
45                         mode = buffer + strlen(path) + 1;
48                         file = fopen64(path, mode);
49                         file_int64 = Units::ptr_to_int64(file);
50                         STORE_INT64(file_int64);
51                         server->write_socket((char*)datagram, 8, RENDERFARM_TIMEOUT);
52 if(DEBUG)
53 printf("RenderFarmFSServer::handle_request file=%p path=%s mode=%s\n", 
54 file, path, mode);
55                         result = 1;
56                         break;
57                 }
59                 case RENDERFARM_FCLOSE:
60                 {
61                         int64_t pointer = READ_INT64((unsigned char*)buffer);
62                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
63 if(DEBUG)
64 printf("RenderFarmFSServer::handle_request file=%p\n", file);
65                         fclose(file);
66                         result = 1;
67                         break;
68                 }
70                 case RENDERFARM_REMOVE:
71                 {
72                         remove(buffer);
73 if(DEBUG)
74 printf("RenderFarmFSServer::handle_request path=%s\n", buffer);
75                         result = 1;
76                         break;
77                 }
79                 case RENDERFARM_RENAME:
80                 {
81                         char *oldpath = buffer;
82                         char *newpath = buffer + strlen(oldpath) + 1;
83                         rename(oldpath, newpath);
84 if(DEBUG)
85 printf("RenderFarmFSServer::handle_request old=%s new=%s\n", oldpath, newpath);
86                         result = 1;
87                         break;
88                 }
90                 case RENDERFARM_FGETC:
91                 {
92                         int64_t pointer = READ_INT64((unsigned char*)buffer);
93                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
94                         unsigned char datagram[1];
95                         datagram[0] = fgetc(file);
96                         server->write_socket((char*)datagram, 1, RENDERFARM_TIMEOUT);
97 if(DEBUG)
98 printf("RenderFarmFSServer::handle_request file=%p\n", file);
99                         result = 1;
100                         break;
101                 }
103                 case RENDERFARM_FPUTC:
104                 {
105                         int64_t pointer = READ_INT64((unsigned char*)buffer);
106                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
107                         fputc(buffer[8], file);
108                         result = 1;
109 if(DEBUG)
110 printf("RenderFarmFSServer::handle_request file=%p\n", file);
111                         break;
112                 }
114                 case RENDERFARM_FREAD:
115                 {
116                         int64_t pointer = READ_INT64((unsigned char*)buffer);
117                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
118                         int size = READ_INT32((unsigned char*)buffer + 8);
119                         int num = READ_INT32((unsigned char*)buffer + 12);
120                         unsigned char datagram[4];
121                         int i = 0;
122                         int bytes;
124                         server->reallocate_buffer(size * num);
125                         bytes = fread(server->buffer, size, num, file);
126                         STORE_INT32(bytes);
127                         server->write_socket((char*)datagram, 4, RENDERFARM_TIMEOUT);
128                         server->write_socket((char*)server->buffer, size * bytes, RENDERFARM_TIMEOUT);
129                         result = 1;
130 if(DEBUG)
131 printf("RenderFarmFSServer::handle_request file=%p size=%d num=%d bytes=%d\n", 
132 file, size, num, bytes);
133                         break;
134                 }
136                 case RENDERFARM_FGETS:
137                 {
138                         int64_t pointer = READ_INT64((unsigned char*)buffer);
139                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
140                         int size = READ_INT32((unsigned char*)buffer + 8);
141                         unsigned char datagram[4];
142                         int i = 0;
144                         server->reallocate_buffer(size);
145                         char *return_value = fgets((char*)server->buffer, size, file);
146                         int bytes = 0;
147                         if(return_value)
148                         {
149                                 bytes = strlen(return_value) + 1;
150                         }
151                         STORE_INT32(bytes);
152                         server->write_socket((char*)datagram, 4, RENDERFARM_TIMEOUT);
153                         server->write_socket((char*)server->buffer, bytes, RENDERFARM_TIMEOUT);
154                         result = 1;
155                         break;
156                 }
158                 case RENDERFARM_FILENO:
159                 {
160                         int64_t pointer = READ_INT64((unsigned char*)buffer);
161                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
162                         unsigned char datagram[4];
163                         int i = 0;
165                         int return_value = fileno(file);
166                         STORE_INT32(return_value);
167                         server->write_socket((char*)datagram, 4, RENDERFARM_TIMEOUT);
168 if(DEBUG)
169 printf("RenderFarmFSServer::handle_request file=%p fileno=%d\n", 
170 file, return_value);
171                         result = 1;
172                         break;
173                 }
175                 case RENDERFARM_FWRITE:
176                 {
177                         int64_t pointer = READ_INT64((unsigned char*)buffer);
178                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
179                         int size = READ_INT32((unsigned char*)buffer + 8);
180                         int num = READ_INT32((unsigned char*)buffer + 12);
181                         unsigned char datagram[4];
182                         int i = 0;
183                         int bytes;
185                         server->reallocate_buffer(size * num);
186                         server->read_socket((char*)server->buffer, size * num, RENDERFARM_TIMEOUT);
187                         bytes = fwrite(server->buffer, size, num, file);
188                         STORE_INT32(bytes);
189                         server->write_socket((char*)datagram, 4, RENDERFARM_TIMEOUT);
190                         result = 1;
191 if(DEBUG)
192 printf("RenderFarmFSServer::handle_request file=%p size=%d num=%d bytes=%d\n", 
193 file, size, num, bytes);
194                         break;
195                 }
197                 case RENDERFARM_FSEEK:
198                 {
199                         int64_t pointer = READ_INT64((unsigned char*)buffer);
200                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
201                         int64_t offset = READ_INT64((unsigned char*)buffer + 8);
202                         int whence = READ_INT32((unsigned char*)buffer + 16);
203                         int return_value;
204                         unsigned char datagram[4];
205                         int i = 0;
207                         return_value = fseeko64(file, offset, whence);
208                         STORE_INT32(return_value);
209                         server->write_socket((char*)datagram, 4, RENDERFARM_TIMEOUT);
210                         result = 1;
211 if(DEBUG)
212 printf("RenderFarmFSServer::handle_request file=%p offset=%lld whence=%d result=%d\n", 
213 file, offset, whence, result);
214                         break;
215                 }
217                 case RENDERFARM_FTELL:
218                 {
219                         int64_t pointer = READ_INT64((unsigned char*)buffer);
220                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
221                         unsigned char datagram[8];
222                         int i = 0;
223                         int64_t return_value = ftello64(file);
224                         STORE_INT64(return_value);
225                         server->write_socket((char*)datagram, 8, RENDERFARM_TIMEOUT);
226                         result = 1;
227 if(DEBUG)
228 printf("RenderFarmFSServer::handle_request file=%p result=%lld\n", 
229 file, return_value);
230                         break;
231                 }
233                 case RENDERFARM_STAT:
234                 {
235                         struct stat stat_buf;
236                         int return_value = stat(buffer, &stat_buf);
237                         server->write_socket((char*)&stat_buf, sizeof(struct stat), RENDERFARM_TIMEOUT);
238                         result = 1;
239 if(DEBUG)
240 printf("RenderFarmFSServer::handle_request path=%s result=%d\n", 
241 buffer, return_value);
242                         break;
243                 }
245                 case RENDERFARM_STAT64:
246                 {
247                         struct stat64 stat_buf;
248                         int return_value = stat64(buffer, &stat_buf);
249                         server->write_socket((char*)&stat_buf, sizeof(struct stat64), RENDERFARM_TIMEOUT);
250                         result = 1;
251 if(DEBUG)
252 printf("RenderFarmFSServer::handle_request path=%s result=%d\n", 
253 buffer, return_value);
254                         break;
255                 }
256         }
258         return result;