6 #include "filesystem.h"
9 #include "pluginserver.h"
10 #include "preferences.h"
11 #include "renderfarm.h"
12 #include "renderfarmclient.h"
13 #include "renderfarmfsclient.h"
14 #include "sighandler.h"
16 #include <arpa/inet.h>
20 #include <netinet/in.h>
23 #include <sys/socket.h>
24 #include <sys/types.h>
31 #define _(String) gettext(String)
32 #define gettext_noop(String) String
33 #define N_(String) gettext_noop (String)
36 // The render client waits for connections from the server.
37 // Then it starts a thread for each connection.
38 RenderFarmClient::RenderFarmClient(int port, char *deamon_path, int nice_value)
41 this->deamon_path = deamon_path;
42 SigHandler *signals = new SigHandler;
43 signals->initialize();
48 thread = new RenderFarmClientThread(this);
50 MWindow::init_defaults(boot_defaults);
51 boot_preferences = new Preferences;
52 boot_preferences->load_defaults(boot_defaults);
53 MWindow::init_plugins(boot_preferences, plugindb, 0);
59 RenderFarmClient::~RenderFarmClient()
63 delete boot_preferences;
64 plugindb->remove_all_objects();
69 void RenderFarmClient::main_loop()
75 // Open listening port
79 struct sockaddr_in addr;
81 addr.sin_family = AF_INET;
82 addr.sin_port = htons((unsigned short)port);
83 addr.sin_addr.s_addr = htonl(INADDR_ANY);
85 if((socket_fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
87 perror(_("RenderFarmClient::main_loop: socket"));
92 (struct sockaddr*)&addr,
96 _("RenderFarmClient::main_loop: bind port %d: %s"),
104 struct sockaddr_un addr;
105 addr.sun_family = AF_FILE;
106 strcpy(addr.sun_path, deamon_path);
107 int size = (offsetof(struct sockaddr_un, sun_path) +
108 strlen(deamon_path) + 1);
110 if((socket_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
112 perror(_("RenderFarmClient::main_loop: socket"));
117 (struct sockaddr*)&addr,
121 _("RenderFarmClient::main_loop: bind path %s: %s\n"),
128 // Wait for connections
129 printf("RenderFarmClient::main_loop: client started\n");
132 if(listen(socket_fd, 256) < 0)
134 perror(_("RenderFarmClient::main_loop: listen"));
144 struct sockaddr_in clientname;
145 socklen_t size = sizeof(clientname);
146 if((new_socket_fd = accept(socket_fd,
147 (struct sockaddr*)&clientname,
150 perror(_("RenderFarmClient::main_loop: accept"));
155 printf("RenderFarmClient::main_loop: Session started from %s\n", inet_ntoa(clientname.sin_addr));
156 thread->main_loop(new_socket_fd);
161 struct sockaddr_un clientname;
162 socklen_t size = sizeof(clientname);
163 if((new_socket_fd = accept(socket_fd,
164 (struct sockaddr*)&clientname,
167 perror(_("RenderFarmClient::main_loop: accept"));
172 printf("RenderFarmClient::main_loop: Session started from %s\n", clientname.sun_path);
173 thread->main_loop(new_socket_fd);
179 void RenderFarmClient::kill_client()
181 printf("RenderFarmClient::kill_client 1\n");
184 printf("RenderFarmClient::kill_client 2\n");
186 kill(this_pid, SIGKILL);
200 // The thread requests jobs from the server until the job table is empty
201 // or the server reports an error. This thread must poll the server
202 // after every frame for the error status.
203 // Detaches when finished.
204 RenderFarmClientThread::RenderFarmClientThread(RenderFarmClient *client)
207 this->client = client;
208 frames_per_second = 0;
209 Thread::set_synchronous(0);
211 mutex_lock = new Mutex;
214 RenderFarmClientThread::~RenderFarmClientThread()
216 if(fs_client) delete fs_client;
221 int RenderFarmClientThread::send_request_header(int request,
224 unsigned char datagram[5];
225 datagram[0] = request;
229 //printf("RenderFarmClientThread::send_request_header %02x%02x%02x%02x%02x\n",
230 // datagram[0], datagram[1], datagram[2], datagram[3], datagram[4]);
232 return (write_socket((char*)datagram,
234 RENDERFARM_TIMEOUT) != 5);
237 int RenderFarmClientThread::write_socket(char *data, int len, int timeout)
239 return RenderFarmServerThread::write_socket(socket_fd,
245 int RenderFarmClientThread::read_socket(char *data, int len, int timeout)
247 return RenderFarmServerThread::read_socket(socket_fd,
253 void RenderFarmClientThread::lock()
255 //printf("RenderFarmClientThread::lock 1 %p %p\n", this, mutex_lock);
259 void RenderFarmClientThread::unlock()
261 //printf("RenderFarmClientThread::unlock 1\n");
262 mutex_lock->unlock();
265 void RenderFarmClientThread::read_string(int socket_fd, char* &string)
267 unsigned char header[4];
268 if(read_socket((char*)header, 4, RENDERFARM_TIMEOUT) != 4)
274 int64_t len = (((u_int32_t)header[0]) << 24) |
275 (((u_int32_t)header[1]) << 16) |
276 (((u_int32_t)header[2]) << 8) |
277 ((u_int32_t)header[3]);
278 //printf("RenderFarmClientThread::read_string %d %02x%02x%02x%02x\n",
279 // len, header[0], header[1], header[2], header[3]);
283 string = new char[len];
284 if(read_socket(string, len, RENDERFARM_TIMEOUT) != len)
293 //printf("RenderFarmClientThread::read_string \n%s\n", string);
297 void RenderFarmClientThread::read_preferences(int socket_fd,
298 Preferences *preferences)
300 //printf("RenderFarmClientThread::read_preferences 1\n");
301 send_request_header(RENDERFARM_PREFERENCES,
304 //printf("RenderFarmClientThread::read_preferences 2\n");
306 read_string(socket_fd, string);
307 //printf("RenderFarmClientThread::read_preferences 3\n%s\n", string);
310 defaults.load_string((char*)string);
311 preferences->load_defaults(&defaults);
312 //printf("RenderFarmClientThread::read_preferences 4\n");
319 void RenderFarmClientThread::read_asset(int socket_fd, Asset *asset)
321 send_request_header(RENDERFARM_ASSET,
325 read_string(socket_fd, string);
326 //printf("RenderFarmClientThread::read_asset\n%s\n", string);
329 file.read_from_string((char*)string);
330 asset->read(client->plugindb, &file);
335 void RenderFarmClientThread::read_edl(int socket_fd,
337 Preferences *preferences)
339 send_request_header(RENDERFARM_EDL,
343 read_string(socket_fd, string);
345 //printf("RenderFarmClientThread::read_edl 1\n");
348 file.read_from_string((char*)string);
358 edl->load_xml(client->plugindb,
364 // Tag input paths for VFS here.
365 // Create VFS object.
367 if(preferences->renderfarm_vfs)
369 fs_client = new RenderFarmFSClient(this);
370 fs_client->initialize();
372 for(Asset *asset = edl->assets->first;
376 char string2[BCTEXTLEN];
377 strcpy(string2, asset->path);
378 sprintf(asset->path, RENDERFARM_FS_PREFIX "%s", string2);
382 // for(Asset *asset = edl->assets->first;
384 // asset = asset->next)
386 // char string2[BCTEXTLEN];
387 // strcpy(string2, asset->path);
388 // fs.join_names(asset->path, preferences->renderfarm_mountpoint, string2);
395 int RenderFarmClientThread::read_package(int socket_fd, RenderPackage *package)
397 send_request_header(RENDERFARM_PACKAGE,
400 unsigned char datagram[5];
404 // Fails if -ieee isn't set.
405 int64_t fixed = !EQUIV(frames_per_second, 0.0) ?
406 (int64_t)(frames_per_second * 65536.0) : 0;
408 write_socket((char*)datagram, 4, RENDERFARM_TIMEOUT);
412 unsigned char *data_ptr;
413 read_string(socket_fd, data);
415 // Signifies end of session.
418 // printf(_("RenderFarmClientThread::read_package no output path recieved.\n"));
424 data_ptr = (unsigned char*)data;
425 strcpy(package->path, data);
426 data_ptr += strlen(package->path);
428 package->audio_start = READ_INT32(data_ptr);
430 package->audio_end = READ_INT32(data_ptr);
432 package->video_start = READ_INT32(data_ptr);
434 package->video_end = READ_INT32(data_ptr);
436 package->use_brender = READ_INT32(data_ptr);
443 int RenderFarmClientThread::send_completion(int socket_fd)
445 return send_request_header(RENDERFARM_DONE,
452 void RenderFarmClientThread::main_loop(int socket_fd)
454 this->socket_fd = socket_fd;
458 void RenderFarmClientThread::run()
460 // Create new memory space
465 waitpid(pid, &return_value, 0);
474 int socket_fd = this->socket_fd;
475 //printf("RenderFarmClientThread::run 1\n");
477 RenderPackage *package;
478 Asset *default_asset;
479 Preferences *preferences;
480 FarmPackageRenderer package_renderer(this, socket_fd);
483 //printf("RenderFarmClientThread::run 2\n");
485 preferences = new Preferences;
486 default_asset = new Asset;
487 package = new RenderPackage;
489 edl->create_objects();
491 //printf("RenderFarmClientThread::run 3\n");
499 read_preferences(socket_fd, preferences);
500 //printf("RenderFarmClientThread::run 3\n");
501 read_asset(socket_fd, default_asset);
502 //printf("RenderFarmClientThread::run 3\n");
503 read_edl(socket_fd, edl, preferences);
512 //printf("RenderFarmClientThread::run 4\n");
514 package_renderer.initialize(0,
519 //printf("RenderFarmClientThread::run 5\n");
524 result = read_package(socket_fd, package);
525 //printf("RenderFarmClientThread::run 6 %d\n", result);
531 //printf("RenderFarmClientThread::run 7 %d\n", result);
533 result = send_completion(socket_fd);
541 if(package_renderer.render_package(package))
543 //printf("RenderFarmClientThread::run 8\n");
544 result = send_completion(socket_fd);
548 frames_per_second = (double)(package->video_end - package->video_start) /
549 ((double)timer.get_difference() / 1000);
551 //printf("RenderFarmClientThread::run 9\n");
558 //printf("RenderFarmClientThread::run 9\n");
559 delete default_asset;
560 //printf("RenderFarmClientThread::run 10\n");
562 //printf("RenderFarmClientThread::run 11\n");
564 //printf("RenderFarmClientThread::run 12\n");
565 printf(_("RenderFarmClientThread::run: Session finished.\n"));
567 // Socket error should be the only cause of this
583 FarmPackageRenderer::FarmPackageRenderer(RenderFarmClientThread *thread,
587 this->thread = thread;
588 this->socket_fd = socket_fd;
593 FarmPackageRenderer::~FarmPackageRenderer()
598 int FarmPackageRenderer::get_result()
601 thread->send_request_header(RENDERFARM_GET_RESULT,
603 unsigned char data[1];
605 if(thread->read_socket((char*)data, 1, RENDERFARM_TIMEOUT) != 1)
614 void FarmPackageRenderer::set_result(int value)
617 thread->send_request_header(RENDERFARM_SET_RESULT,
619 unsigned char data[1];
621 thread->write_socket((char*)data, 1, RENDERFARM_TIMEOUT);
625 void FarmPackageRenderer::set_progress(int64_t total_samples)
628 thread->send_request_header(RENDERFARM_PROGRESS,
630 unsigned char datagram[4];
632 STORE_INT32(total_samples);
633 thread->write_socket((char*)datagram, 4, RENDERFARM_TIMEOUT);
637 void FarmPackageRenderer::set_video_map(int64_t position, int value)
640 thread->send_request_header(RENDERFARM_SET_VMAP,
642 unsigned char datagram[8];
644 STORE_INT32(position);
646 thread->write_socket((char*)datagram, 8, RENDERFARM_TIMEOUT);