r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / cinelerra / renderfarmfsserver.C
blob5bd8ce3edc0ebb155700c0d5b3682fcca3c6a6e8
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_FWRITE:
159                 {
160                         int64_t pointer = READ_INT64((unsigned char*)buffer);
161                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
162                         int size = READ_INT32((unsigned char*)buffer + 8);
163                         int num = READ_INT32((unsigned char*)buffer + 12);
164                         unsigned char datagram[4];
165                         int i = 0;
166                         int bytes;
168                         server->reallocate_buffer(size * num);
169                         server->read_socket((char*)server->buffer, size * num, RENDERFARM_TIMEOUT);
170                         bytes = fwrite(server->buffer, size, num, file);
171                         STORE_INT32(bytes);
172                         server->write_socket((char*)datagram, 4, RENDERFARM_TIMEOUT);
173                         result = 1;
174 if(DEBUG)
175 printf("RenderFarmFSServer::handle_request file=%p size=%d num=%d bytes=%d\n", 
176 file, size, num, bytes);
177                         break;
178                 }
180                 case RENDERFARM_FSEEK:
181                 {
182                         int64_t pointer = READ_INT64((unsigned char*)buffer);
183                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
184                         int64_t offset = READ_INT64((unsigned char*)buffer + 8);
185                         int whence = READ_INT32((unsigned char*)buffer + 16);
186                         int return_value;
187                         unsigned char datagram[4];
188                         int i = 0;
190                         return_value = fseeko64(file, offset, whence);
191                         STORE_INT32(return_value);
192                         server->write_socket((char*)datagram, 4, RENDERFARM_TIMEOUT);
193                         result = 1;
194 if(DEBUG)
195 printf("RenderFarmFSServer::handle_request file=%p offset=%lld whence=%d result=%d\n", 
196 file, offset, whence, result);
197                         break;
198                 }
200                 case RENDERFARM_FTELL:
201                 {
202                         int64_t pointer = READ_INT64((unsigned char*)buffer);
203                         FILE *file = (FILE*)Units::int64_to_ptr(pointer);
204                         unsigned char datagram[8];
205                         int i = 0;
206                         int64_t return_value = ftello64(file);
207                         STORE_INT64(return_value);
208                         server->write_socket((char*)datagram, 8, RENDERFARM_TIMEOUT);
209                         result = 1;
210 if(DEBUG)
211 printf("RenderFarmFSServer::handle_request file=%p result=%lld\n", 
212 file, return_value);
213                         break;
214                 }
216                 case RENDERFARM_STAT:
217                 {
218                         struct stat stat_buf;
219                         int return_value = stat(buffer, &stat_buf);
220                         server->write_socket((char*)&stat_buf, sizeof(struct stat), RENDERFARM_TIMEOUT);
221                         result = 1;
222 if(DEBUG)
223 printf("RenderFarmFSServer::handle_request path=%s result=%d\n", 
224 buffer, return_value);
225                         break;
226                 }
228                 case RENDERFARM_STAT64:
229                 {
230                         struct stat64 stat_buf;
231                         int return_value = stat64(buffer, &stat_buf);
232                         server->write_socket((char*)&stat_buf, sizeof(struct stat64), RENDERFARM_TIMEOUT);
233                         result = 1;
234 if(DEBUG)
235 printf("RenderFarmFSServer::handle_request path=%s result=%d\n", 
236 buffer, return_value);
237                         break;
238                 }
239         }
241         return result;