Merge branch 'ct' of git.pipapo.org:cinelerra-ct into ct
[cinelerra_cv/ct.git] / cinelerra / renderfarmfsserver.C
bloba7e6715e7175be4cd6dff36de2944e890029fbda
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, unsigned 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 = (char*)buffer;
45                         mode = (char*)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 RENDERFARM_FOPEN file=%p file_int64=%llx datagram=%02x%02x%02x%02x%02x%02x%02x%02x path=%s mode=%s\n",
54 file, file_int64, datagram[0], datagram[1], datagram[2], datagram[3], datagram[4], datagram[5], datagram[6], datagram[7], 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 RENDERFARM_FCLOSE file=%p\n", file);
65                         fclose(file);
66                         result = 1;
67                         break;
68                 }
70                 case RENDERFARM_REMOVE:
71                 {
72                         remove((char*)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 = (char*)buffer;
82                         char *newpath = (char*)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;
123 // printf("RenderFarmFSServer::handle_request RENDERFARM_FREAD %02x%02x%02x%02x%02x%02x%02x%02x %p %d %d\n", 
124 // buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7], 
125 // pointer,
126 // file,
127 // size,
128 // num);
130                         server->reallocate_buffer(size * num);
131                         bytes = fread(server->buffer, size, num, file);
132                         STORE_INT32(bytes);
133                         server->write_socket((char*)datagram, 4, RENDERFARM_TIMEOUT);
134                         server->write_socket((char*)server->buffer, size * bytes, RENDERFARM_TIMEOUT);
135                         result = 1;
136 if(DEBUG)
137 printf("RenderFarmFSServer::handle_request file=%p size=%d num=%d bytes=%d\n", 
138 file, size, num, bytes);
139                         break;
140                 }
142                 case RENDERFARM_FGETS:
143                 {
144                         int64_t pointer = READ_INT64((unsigned char*)buffer);
145                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
146                         int size = READ_INT32((unsigned char*)buffer + 8);
147                         unsigned char datagram[4];
148                         int i = 0;
150                         server->reallocate_buffer(size);
151                         char *return_value = fgets((char*)server->buffer, size, file);
152                         int bytes = 0;
153                         if(return_value)
154                         {
155                                 bytes = strlen(return_value) + 1;
156                         }
157                         STORE_INT32(bytes);
158                         server->write_socket((char*)datagram, 4, RENDERFARM_TIMEOUT);
159                         server->write_socket((char*)server->buffer, bytes, RENDERFARM_TIMEOUT);
160                         result = 1;
161                         break;
162                 }
164                 case RENDERFARM_FILENO:
165                 {
166                         int64_t pointer = READ_INT64((unsigned char*)buffer);
167                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
168                         unsigned char datagram[4];
169                         int i = 0;
171                         int return_value = fileno(file);
172                         STORE_INT32(return_value);
173                         server->write_socket((char*)datagram, 4, RENDERFARM_TIMEOUT);
174 if(DEBUG)
175 printf("RenderFarmFSServer::handle_request file=%p fileno=%d\n", 
176 file, return_value);
177                         result = 1;
178                         break;
179                 }
181                 case RENDERFARM_FWRITE:
182                 {
183                         int64_t pointer = READ_INT64((unsigned char*)buffer);
184                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
185                         int size = READ_INT32((unsigned char*)buffer + 8);
186                         int num = READ_INT32((unsigned char*)buffer + 12);
187                         unsigned char datagram[4];
188                         int i = 0;
189                         int bytes;
191                         server->reallocate_buffer(size * num);
192                         server->read_socket((char*)server->buffer, size * num, RENDERFARM_TIMEOUT);
193                         bytes = fwrite(server->buffer, size, num, file);
194                         STORE_INT32(bytes);
195                         server->write_socket((char*)datagram, 4, RENDERFARM_TIMEOUT);
196                         result = 1;
197 if(DEBUG)
198 printf("RenderFarmFSServer::handle_request RENDERFARM_FWRITE file=%p size=%d num=%d bytes=%d\n", 
199 file, size, num, bytes);
200                         break;
201                 }
203                 case RENDERFARM_FSEEK:
204                 {
205                         int64_t pointer = READ_INT64((unsigned char*)buffer);
206                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
207 // printf("RENDERFARM_FSEEK 1 buffer=%02x%02x%02x%02x%02x%02x%02x%02x %p %llx\n",
208 // buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7],  file, pointer);
209                         int64_t offset = READ_INT64((unsigned char*)buffer + 8);
210                         int whence = READ_INT32((unsigned char*)buffer + 16);
211                         int return_value;
212                         unsigned char datagram[4];
213                         int i = 0;
215                         return_value = fseeko64(file, offset, whence);
216                         STORE_INT32(return_value);
217                         server->write_socket((char*)datagram, 4, RENDERFARM_TIMEOUT);
218                         result = 1;
219 if(DEBUG)
220 printf("RenderFarmFSServer::handle_request file=%p offset=%lld whence=%d result=%d\n", 
221 file, offset, whence, result);
222                         break;
223                 }
225                 case RENDERFARM_FTELL:
226                 {
227                         int64_t pointer = READ_INT64((unsigned char*)buffer);
228                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
229                         unsigned char datagram[8];
230                         int i = 0;
231                         int64_t return_value = ftello64(file);
232                         STORE_INT64(return_value);
233                         server->write_socket((char*)datagram, 8, RENDERFARM_TIMEOUT);
234                         result = 1;
235 if(DEBUG)
236 printf("RenderFarmFSServer::handle_request file=%p result=%lld\n", 
237 file, return_value);
238                         break;
239                 }
241                 case RENDERFARM_STAT:
242                 {
243                         struct stat stat_buf;
244                         int return_value = stat((char*)buffer, &stat_buf);
245                         server->write_socket((char*)&stat_buf, sizeof(struct stat), RENDERFARM_TIMEOUT);
246                         result = 1;
247 if(DEBUG)
248 printf("RenderFarmFSServer::handle_request path=%s result=%d\n", 
249 buffer, return_value);
250                         break;
251                 }
253                 case RENDERFARM_STAT64:
254                 {
255                         struct stat64 stat_buf;
256                         int return_value = stat64((char*)buffer, &stat_buf);
257                         vfs_stat_t arg;
258                         arg.dev = stat_buf.st_dev;
259 //                      arg.ino32 = stat_buf.__st_ino;
260                         arg.ino = stat_buf.st_ino;
261                         arg.nlink = stat_buf.st_nlink;
262                         arg.mode = stat_buf.st_mode;
263                         arg.uid = stat_buf.st_uid;
264                         arg.gid = stat_buf.st_gid;
265                         arg.rdev = stat_buf.st_rdev;
266                         arg.size = stat_buf.st_size;
267                         arg.blksize = stat_buf.st_blksize;
268                         arg.blocks = stat_buf.st_blocks;
269                         arg.atim = stat_buf.st_atim.tv_sec;
270                         arg.mtim = stat_buf.st_mtim.tv_sec;
271                         arg.ctim = stat_buf.st_ctim.tv_sec;
272                         server->write_socket((char*)&arg, sizeof(arg), RENDERFARM_TIMEOUT);
273                         result = 1;
274 if(DEBUG)
275 printf("RenderFarmFSServer::handle_request path=%s result=%d\n", 
276 buffer, return_value);
277                         break;
278                 }
279         }
281         return result;
284 //      Local Variables:
285 //      mode: C++
286 //      c-file-style: "linux"
287 //      End: