7 #include "filesystem.h"
12 #include "pluginserver.h"
13 #include "preferences.h"
14 #include "renderfarm.h"
15 #include "renderfarmclient.h"
16 //#include "renderfarmfsclient.h"
17 #include "sighandler.h"
19 #include <arpa/inet.h>
23 #include <netinet/in.h>
26 #include <sys/socket.h>
27 #include <sys/types.h>
35 // The render client waits for connections from the server.
36 // Then it starts a thread for each connection.
37 RenderFarmClient::RenderFarmClient(int port,
43 this->deamon_path = deamon_path;
44 SigHandler *signals = new SigHandler;
45 signals->initialize();
51 MWindow::init_defaults(boot_defaults, config_path);
52 boot_preferences = new Preferences;
53 boot_preferences->load_defaults(boot_defaults);
54 MWindow::init_plugins(boot_preferences, plugindb, 0);
60 RenderFarmClient::~RenderFarmClient()
64 delete boot_preferences;
65 plugindb->remove_all_objects();
70 void RenderFarmClient::main_loop()
76 // Open listening port
80 struct sockaddr_in addr;
82 addr.sin_family = AF_INET;
83 addr.sin_port = htons((unsigned short)port);
84 addr.sin_addr.s_addr = htonl(INADDR_ANY);
86 if((socket_fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
88 perror(_("RenderFarmClient::main_loop: socket"));
93 (struct sockaddr*)&addr,
97 _("RenderFarmClient::main_loop: bind port %d: %s"),
105 struct sockaddr_un addr;
106 addr.sun_family = AF_FILE;
107 strcpy(addr.sun_path, deamon_path);
108 int size = (offsetof(struct sockaddr_un, sun_path) +
109 strlen(deamon_path) + 1);
111 if((socket_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
113 perror(_("RenderFarmClient::main_loop: socket"));
118 (struct sockaddr*)&addr,
122 _("RenderFarmClient::main_loop: bind path %s: %s\n"),
129 // Wait for connections
130 printf("RenderFarmClient::main_loop: client started\n");
133 if(listen(socket_fd, 256) < 0)
135 perror(_("RenderFarmClient::main_loop: listen"));
145 struct sockaddr_in clientname;
146 socklen_t size = sizeof(clientname);
147 if((new_socket_fd = accept(socket_fd,
148 (struct sockaddr*)&clientname,
151 perror(_("RenderFarmClient::main_loop: accept"));
156 //printf("RenderFarmClient::main_loop: Session started from %s\n", inet_ntoa(clientname.sin_addr));
157 RenderFarmClientThread *thread =
158 new RenderFarmClientThread(this);
159 thread->main_loop(new_socket_fd);
164 struct sockaddr_un clientname;
165 socklen_t size = sizeof(clientname);
166 if((new_socket_fd = accept(socket_fd,
167 (struct sockaddr*)&clientname,
170 perror(_("RenderFarmClient::main_loop: accept"));
175 //printf("RenderFarmClient::main_loop: Session started from %s\n", clientname.sun_path);
176 RenderFarmClientThread *thread =
177 new RenderFarmClientThread(this);
178 thread->main_loop(new_socket_fd);
184 void RenderFarmClient::kill_client()
186 printf("RenderFarmClient::kill_client 1\n");
189 printf("RenderFarmClient::kill_client 2\n");
191 kill(this_pid, SIGKILL);
205 // The thread requests jobs from the server until the job table is empty
206 // or the server reports an error. This thread must poll the server
207 // after every frame for the error status.
208 // Detaches when finished.
209 RenderFarmClientThread::RenderFarmClientThread(RenderFarmClient *client)
212 this->client = client;
213 frames_per_second = 0;
214 Thread::set_synchronous(0);
216 mutex_lock = new Mutex("RenderFarmClientThread::mutex_lock");
221 RenderFarmClientThread::~RenderFarmClientThread()
223 // if(fs_client) delete fs_client;
230 int RenderFarmClientThread::send_request_header(int request,
233 unsigned char datagram[5];
234 datagram[0] = request;
238 // printf("RenderFarmClientThread::send_request_header %d %02x%02x%02x%02x%02x\n",
239 // request, datagram[0], datagram[1], datagram[2], datagram[3], datagram[4]);
241 return (write_socket((char*)datagram, 5) != 5);
244 int RenderFarmClientThread::write_socket(char *data, int len)
246 return write(socket_fd, data, len);
249 int RenderFarmClientThread::read_socket(char *data, int len)
253 watchdog->begin_request();
254 while(len > 0 && bytes_read >= 0)
256 bytes_read = read(socket_fd, data + offset, len);
260 offset += bytes_read;
268 watchdog->end_request();
273 int RenderFarmClientThread::write_int64(int64_t value)
275 unsigned char data[sizeof(int64_t)];
276 data[0] = (value >> 56) & 0xff;
277 data[1] = (value >> 48) & 0xff;
278 data[2] = (value >> 40) & 0xff;
279 data[3] = (value >> 32) & 0xff;
280 data[4] = (value >> 24) & 0xff;
281 data[5] = (value >> 16) & 0xff;
282 data[6] = (value >> 8) & 0xff;
283 data[7] = value & 0xff;
284 return (write_socket((char*)data, sizeof(int64_t)) != sizeof(int64_t));
287 int64_t RenderFarmClientThread::read_int64(int *error)
290 if(!error) error = &temp;
292 unsigned char data[sizeof(int64_t)];
293 *error = (read_socket((char*)data, sizeof(int64_t)) != sizeof(int64_t));
295 // Make it return 1 if error so it can be used to read a result code from the
300 result = (((int64_t)data[0]) << 56) |
301 (((uint64_t)data[1]) << 48) |
302 (((uint64_t)data[2]) << 40) |
303 (((uint64_t)data[3]) << 32) |
304 (((uint64_t)data[4]) << 24) |
305 (((uint64_t)data[5]) << 16) |
306 (((uint64_t)data[6]) << 8) |
312 void RenderFarmClientThread::read_string(char* &string)
314 unsigned char header[4];
315 if(read_socket((char*)header, 4) != 4)
321 int64_t len = (((u_int32_t)header[0]) << 24) |
322 (((u_int32_t)header[1]) << 16) |
323 (((u_int32_t)header[2]) << 8) |
324 ((u_int32_t)header[3]);
328 string = new char[len];
329 if(read_socket(string, len) != len)
340 void RenderFarmClientThread::abort()
342 send_completion(socket_fd);
347 void RenderFarmClientThread::lock(char *location)
349 mutex_lock->lock(location);
352 void RenderFarmClientThread::unlock()
354 mutex_lock->unlock();
357 void RenderFarmClientThread::get_command(int socket_fd, int *command)
359 unsigned char data[4];
361 *command = read_int64(&error);
370 void RenderFarmClientThread::read_preferences(int socket_fd,
371 Preferences *preferences)
373 lock("RenderFarmClientThread::read_preferences");
374 send_request_header(RENDERFARM_PREFERENCES,
381 defaults.load_string((char*)string);
382 preferences->load_defaults(&defaults);
390 void RenderFarmClientThread::read_asset(int socket_fd, Asset *asset)
392 lock("RenderFarmClientThread::read_asset");
393 send_request_header(RENDERFARM_ASSET,
398 read_string(string1);
399 read_string(string2);
404 file.read_from_string((char*)string2);
410 defaults.load_string((char*)string1);
411 asset->load_defaults(&defaults,
424 void RenderFarmClientThread::read_edl(int socket_fd,
426 Preferences *preferences)
428 lock("RenderFarmClientThread::read_edl");
429 send_request_header(RENDERFARM_EDL,
437 file.read_from_string((char*)string);
447 edl->load_xml(client->plugindb,
455 int RenderFarmClientThread::read_package(int socket_fd, RenderPackage *package)
457 lock("RenderFarmClientThread::read_package");
458 send_request_header(RENDERFARM_PACKAGE,
461 unsigned char datagram[5];
465 // Fails if -ieee isn't set.
466 int64_t fixed = !EQUIV(frames_per_second, 0.0) ?
467 (int64_t)(frames_per_second * 65536.0) : 0;
469 write_socket((char*)datagram, 4);
472 //printf("RenderFarmClientThread::read_package 1 %f %ld\n", frames_per_second, fixed);
474 unsigned char *data_ptr;
476 //printf("RenderFarmClientThread::read_package 2 %p\n", data);
477 // Signifies end of session.
480 // printf(_("RenderFarmClientThread::read_package no output path recieved.\n"));
485 //printf("RenderFarmClientThread::read_package 2\n");
488 data_ptr = (unsigned char*)data;
489 strcpy(package->path, data);
490 data_ptr += strlen(package->path);
492 package->audio_start = READ_INT32(data_ptr);
494 package->audio_end = READ_INT32(data_ptr);
496 package->video_start = READ_INT32(data_ptr);
498 package->video_end = READ_INT32(data_ptr);
500 package->use_brender = READ_INT32(data_ptr);
502 package->audio_do = READ_INT32(data_ptr);
504 package->video_do = READ_INT32(data_ptr);
512 int RenderFarmClientThread::send_completion(int socket_fd)
514 lock("RenderFarmClientThread::send_completion");
515 int result = send_request_header(RENDERFARM_DONE, 0);
521 void RenderFarmClientThread::ping_server()
523 lock("RenderFarmClientThread::ping_server");
524 send_request_header(RENDERFARM_KEEPALIVE, 0);
530 void RenderFarmClientThread::main_loop(int socket_fd)
532 this->socket_fd = socket_fd;
537 void RenderFarmClientThread::run()
539 // Create new memory space
544 waitpid(pid, &return_value, 0);
548 // Get the pid of the fork if inside the fork
553 int socket_fd = this->socket_fd;
555 init_client_keepalive();
557 // Get command to run
560 lock("RenderFarmClientThread::run");
562 get_command(socket_fd, &command);
566 //printf("RenderFarmClientThread::run command=%d\n", command);
571 case RENDERFARM_TUNER:
574 case RENDERFARM_PACKAGES:
575 do_packages(socket_fd);
583 void RenderFarmClientThread::init_client_keepalive()
585 keep_alive = new RenderFarmKeepalive(this);
587 watchdog = new RenderFarmWatchdog(0, this);
593 void RenderFarmClientThread::do_tuner(int socket_fd)
595 // Currently only 1 tuner driver. Maybe more someday.
596 DVBTune server(this);
602 void RenderFarmClientThread::do_packages(int socket_fd)
606 RenderPackage *package;
607 Asset *default_asset;
608 Preferences *preferences;
612 FarmPackageRenderer package_renderer(this, socket_fd);
617 //printf("RenderFarmClientThread::run 2\n");
619 preferences = new Preferences;
620 default_asset = new Asset;
621 package = new RenderPackage;
623 edl->create_objects();
625 //printf("RenderFarmClientThread::run 3\n");
633 read_preferences(socket_fd, preferences);
634 //printf("RenderFarmClientThread::run 3\n");
635 read_asset(socket_fd, default_asset);
636 //printf("RenderFarmClientThread::run 3\n");
637 read_edl(socket_fd, edl, preferences);
646 //printf("RenderFarmClientThread::run 4\n");
648 package_renderer.initialize(0,
653 //printf("RenderFarmClientThread::run 5\n");
658 result = read_package(socket_fd, package);
659 //printf("RenderFarmClientThread::run 6 %d\n", result);
665 //printf("RenderFarmClientThread::run 7\n");
667 result = send_completion(socket_fd);
675 if(package_renderer.render_package(package))
677 //printf("RenderFarmClientThread::run 8\n");
678 result = send_completion(socket_fd);
682 frames_per_second = (double)(package->video_end - package->video_start) /
683 ((double)timer.get_difference() / 1000);
685 //printf("RenderFarmClientThread::run 9\n");
692 //printf("RenderFarmClientThread::run 9\n");
693 Garbage::delete_object(default_asset);
694 //printf("RenderFarmClientThread::run 10\n");
696 //printf("RenderFarmClientThread::run 11\n");
698 printf(_("RenderFarmClientThread::run: Session finished.\n"));
709 RenderFarmKeepalive::RenderFarmKeepalive(
710 RenderFarmClientThread *client_thread)
713 this->client_thread = client_thread;
717 RenderFarmKeepalive::~RenderFarmKeepalive()
725 void RenderFarmKeepalive::run()
734 // watchdog thread kills this if it gets stuck
735 client_thread->ping_server();
754 FarmPackageRenderer::FarmPackageRenderer(RenderFarmClientThread *thread,
758 this->thread = thread;
759 this->socket_fd = socket_fd;
764 FarmPackageRenderer::~FarmPackageRenderer()
769 int FarmPackageRenderer::get_result()
771 thread->lock("FarmPackageRenderer::get_result");
772 thread->send_request_header(RENDERFARM_GET_RESULT,
774 unsigned char data[1];
776 if(thread->read_socket((char*)data, 1) != 1)
785 void FarmPackageRenderer::set_result(int value)
787 thread->lock("FarmPackageRenderer::set_result");
788 thread->send_request_header(RENDERFARM_SET_RESULT,
790 unsigned char data[1];
792 thread->write_socket((char*)data, 1);
796 void FarmPackageRenderer::set_progress(int64_t total_samples)
798 thread->lock("FarmPackageRenderer::set_progress");
799 thread->send_request_header(RENDERFARM_PROGRESS,
801 unsigned char datagram[4];
803 STORE_INT32(total_samples);
804 thread->write_socket((char*)datagram, 4);
808 int FarmPackageRenderer::set_video_map(int64_t position, int value)
811 unsigned char datagram[8];
812 char return_value[1];
815 thread->lock("FarmPackageRenderer::set_video_map");
816 thread->send_request_header(RENDERFARM_SET_VMAP,
818 STORE_INT32(position);
820 thread->write_socket((char*)datagram, 8);
822 // Get completion since the GUI may be locked for a long time.
823 if(!thread->read_socket(return_value, 1))