4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "filesystem.h"
33 #include "pluginserver.h"
34 #include "preferences.h"
35 #include "renderfarm.h"
36 #include "renderfarmclient.h"
37 //#include "renderfarmfsclient.h"
38 #include "sighandler.h"
40 #include <arpa/inet.h>
44 #include <netinet/in.h>
47 #include <sys/socket.h>
48 #include <sys/types.h>
56 // The render client waits for connections from the server.
57 // Then it starts a thread for each connection.
58 RenderFarmClient::RenderFarmClient(int port,
63 char string[BCTEXTLEN];
66 this->deamon_path = deamon_path;
67 SigHandler *signals = new SigHandler;
68 signals->initialize();
71 (void)nice(nice_value);
74 MWindow::init_defaults(boot_defaults, config_path);
75 boot_preferences = new Preferences;
76 boot_preferences->load_defaults(boot_defaults);
77 MWindow::init_plugins(0, boot_preferences);
79 strcpy(string, boot_preferences->plugin_dir);
80 strcat(string, "/" FONT_SEARCHPATH);
81 BC_Resources::init_fontconfig(string);
87 RenderFarmClient::~RenderFarmClient()
91 delete boot_preferences;
92 plugindb->remove_all_objects();
97 void RenderFarmClient::main_loop()
100 BC_WindowBase::get_resources()->vframe_shm = 1;
103 // Open listening port
107 struct sockaddr_in addr;
109 addr.sin_family = AF_INET;
110 addr.sin_port = htons((unsigned short)port);
111 addr.sin_addr.s_addr = htonl(INADDR_ANY);
113 if((socket_fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
115 perror(_("RenderFarmClient::main_loop: socket"));
120 (struct sockaddr*)&addr,
124 _("RenderFarmClient::main_loop: bind port %d: %s"),
132 struct sockaddr_un addr;
133 addr.sun_family = AF_FILE;
134 strcpy(addr.sun_path, deamon_path);
135 int size = (offsetof(struct sockaddr_un, sun_path) +
136 strlen(deamon_path) + 1);
138 if((socket_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
140 perror(_("RenderFarmClient::main_loop: socket"));
145 (struct sockaddr*)&addr,
149 _("RenderFarmClient::main_loop: bind path %s: %s\n"),
156 // Wait for connections
157 printf("RenderFarmClient::main_loop: client started\n");
160 if(listen(socket_fd, 256) < 0)
162 perror(_("RenderFarmClient::main_loop: listen"));
172 struct sockaddr_in clientname;
173 socklen_t size = sizeof(clientname);
174 if((new_socket_fd = accept(socket_fd,
175 (struct sockaddr*)&clientname,
178 perror(_("RenderFarmClient::main_loop: accept"));
183 printf("RenderFarmClient::main_loop: Session started from %s\n", inet_ntoa(clientname.sin_addr));
184 RenderFarmClientThread *thread =
185 new RenderFarmClientThread(this);
186 thread->main_loop(new_socket_fd);
191 struct sockaddr_un clientname;
192 socklen_t size = sizeof(clientname);
193 if((new_socket_fd = accept(socket_fd,
194 (struct sockaddr*)&clientname,
197 perror(_("RenderFarmClient::main_loop: accept"));
202 printf("RenderFarmClient::main_loop: Session started from %s\n", clientname.sun_path);
203 RenderFarmClientThread *thread =
204 new RenderFarmClientThread(this);
205 thread->main_loop(new_socket_fd);
211 void RenderFarmClient::kill_client()
213 printf("RenderFarmClient::kill_client 1\n");
216 printf("RenderFarmClient::kill_client 2\n");
218 kill(this_pid, SIGKILL);
223 // The thread requests jobs from the server until the job table is empty
224 // or the server reports an error. This thread must poll the server
225 // after every frame for the error status.
226 // Detaches when finished.
227 RenderFarmClientThread::RenderFarmClientThread(RenderFarmClient *client)
230 this->client = client;
231 frames_per_second = 0;
232 Thread::set_synchronous(0);
234 mutex_lock = new Mutex("RenderFarmClientThread::mutex_lock");
239 RenderFarmClientThread::~RenderFarmClientThread()
241 // if(fs_client) delete fs_client;
248 int RenderFarmClientThread::send_request_header(int request,
251 unsigned char datagram[5];
252 datagram[0] = request;
256 // printf("RenderFarmClientThread::send_request_header %d %02x%02x%02x%02x%02x\n",
257 // request, datagram[0], datagram[1], datagram[2], datagram[3], datagram[4]);
259 return (write_socket((char*)datagram, 5) != 5);
262 int RenderFarmClientThread::write_socket(char *data, int len)
264 //printf("RenderFarmClientThread::write_socket 1\n");
265 int result = write(socket_fd, data, len);
266 //printf("RenderFarmClientThread::write_socket 10\n");
270 int RenderFarmClientThread::read_socket(char *data, int len)
274 //printf("RenderFarmClientThread::read_socket 1\n");
275 watchdog->begin_request();
276 while(len > 0 && bytes_read >= 0)
278 bytes_read = read(socket_fd, data + offset, len);
282 offset += bytes_read;
290 watchdog->end_request();
291 //printf("RenderFarmClientThread::read_socket 10\n");
296 int RenderFarmClientThread::write_int64(int64_t value)
298 unsigned char data[sizeof(int64_t)];
299 data[0] = (value >> 56) & 0xff;
300 data[1] = (value >> 48) & 0xff;
301 data[2] = (value >> 40) & 0xff;
302 data[3] = (value >> 32) & 0xff;
303 data[4] = (value >> 24) & 0xff;
304 data[5] = (value >> 16) & 0xff;
305 data[6] = (value >> 8) & 0xff;
306 data[7] = value & 0xff;
307 return (write_socket((char*)data, sizeof(int64_t)) != sizeof(int64_t));
310 int64_t RenderFarmClientThread::read_int64(int *error)
313 if(!error) error = &temp;
315 unsigned char data[sizeof(int64_t)];
316 *error = (read_socket((char*)data, sizeof(int64_t)) != sizeof(int64_t));
318 // Make it return 1 if error so it can be used to read a result code from the
323 result = (((int64_t)data[0]) << 56) |
324 (((uint64_t)data[1]) << 48) |
325 (((uint64_t)data[2]) << 40) |
326 (((uint64_t)data[3]) << 32) |
327 (((uint64_t)data[4]) << 24) |
328 (((uint64_t)data[5]) << 16) |
329 (((uint64_t)data[6]) << 8) |
335 void RenderFarmClientThread::read_string(char* &string)
337 unsigned char header[4];
338 if(read_socket((char*)header, 4) != 4)
344 int64_t len = (((u_int32_t)header[0]) << 24) |
345 (((u_int32_t)header[1]) << 16) |
346 (((u_int32_t)header[2]) << 8) |
347 ((u_int32_t)header[3]);
351 string = new char[len];
352 if(read_socket(string, len) != len)
363 void RenderFarmClientThread::abort()
365 send_completion(socket_fd);
370 void RenderFarmClientThread::lock(const char *location)
372 mutex_lock->lock(location);
375 void RenderFarmClientThread::unlock()
377 mutex_lock->unlock();
380 void RenderFarmClientThread::get_command(int socket_fd, int *command)
383 *command = read_int64(&error);
392 void RenderFarmClientThread::read_preferences(int socket_fd,
393 Preferences *preferences)
395 lock("RenderFarmClientThread::read_preferences");
396 send_request_header(RENDERFARM_PREFERENCES,
403 defaults.load_string((char*)string);
404 preferences->load_defaults(&defaults);
412 void RenderFarmClientThread::read_asset(int socket_fd, Asset *asset)
414 lock("RenderFarmClientThread::read_asset");
415 send_request_header(RENDERFARM_ASSET,
420 read_string(string1);
421 read_string(string2);
426 file.read_from_string((char*)string2);
432 defaults.load_string((char*)string1);
433 asset->load_defaults(&defaults,
441 //printf("RenderFarmClientThread::read_asset %d\n", __LINE__);
449 void RenderFarmClientThread::read_edl(int socket_fd,
451 Preferences *preferences)
453 lock("RenderFarmClientThread::read_edl");
454 send_request_header(RENDERFARM_EDL,
462 file.read_from_string((char*)string);
479 int RenderFarmClientThread::read_package(int socket_fd, RenderPackage *package)
481 lock("RenderFarmClientThread::read_package");
482 send_request_header(RENDERFARM_PACKAGE,
485 unsigned char datagram[5];
489 // Fails if -ieee isn't set.
490 int64_t fixed = !EQUIV(frames_per_second, 0.0) ?
491 (int64_t)(frames_per_second * 65536.0) : 0;
493 write_socket((char*)datagram, 4);
496 //printf("RenderFarmClientThread::read_package 1 %f %ld\n", frames_per_second, fixed);
498 unsigned char *data_ptr;
500 //printf("RenderFarmClientThread::read_package 2 %p\n", data);
501 // Signifies end of session.
504 // printf(_("RenderFarmClientThread::read_package no output path recieved.\n"));
509 //printf("RenderFarmClientThread::read_package 2\n");
512 data_ptr = (unsigned char*)data;
513 strcpy(package->path, data);
514 data_ptr += strlen(package->path);
516 package->audio_start = READ_INT32(data_ptr);
518 package->audio_end = READ_INT32(data_ptr);
520 package->video_start = READ_INT32(data_ptr);
522 package->video_end = READ_INT32(data_ptr);
524 package->use_brender = READ_INT32(data_ptr);
526 package->audio_do = READ_INT32(data_ptr);
528 package->video_do = READ_INT32(data_ptr);
536 int RenderFarmClientThread::send_completion(int socket_fd)
538 lock("RenderFarmClientThread::send_completion");
539 int result = send_request_header(RENDERFARM_DONE, 0);
545 void RenderFarmClientThread::ping_server()
547 lock("RenderFarmClientThread::ping_server");
548 send_request_header(RENDERFARM_KEEPALIVE, 0);
554 void RenderFarmClientThread::main_loop(int socket_fd)
556 this->socket_fd = socket_fd;
561 void RenderFarmClientThread::run()
563 // Create new memory space
568 waitpid(pid, &return_value, 0);
572 // Get the pid of the fork if inside the fork
577 int socket_fd = this->socket_fd;
579 init_client_keepalive();
581 // Get command to run
583 lock("RenderFarmClientThread::run");
584 get_command(socket_fd, &command);
587 //printf("RenderFarmClientThread::run command=%d\n", command);
591 case RENDERFARM_TUNER:
594 case RENDERFARM_PACKAGES:
595 do_packages(socket_fd);
603 void RenderFarmClientThread::init_client_keepalive()
605 keep_alive = new RenderFarmKeepalive(this);
607 watchdog = new RenderFarmWatchdog(0, this);
613 void RenderFarmClientThread::do_tuner(int socket_fd)
615 // Currently only 1 tuner driver. Maybe more someday.
616 DVBTune server(this);
622 void RenderFarmClientThread::do_packages(int socket_fd)
626 RenderPackage *package;
627 Asset *default_asset;
628 Preferences *preferences;
632 FarmPackageRenderer package_renderer(this, socket_fd);
637 //printf("RenderFarmClientThread::run 2\n");
639 preferences = new Preferences;
640 default_asset = new Asset;
641 package = new RenderPackage;
643 edl->create_objects();
650 //printf("RenderFarmClientThread::run 3\n");
651 read_preferences(socket_fd, preferences);
652 //printf("RenderFarmClientThread::run 4\n");
653 read_asset(socket_fd, default_asset);
654 //printf("RenderFarmClientThread::run 5\n");
655 read_edl(socket_fd, edl, preferences);
657 //printf("RenderFarmClientThread::run 6\n");
666 package_renderer.initialize(0,
674 //printf("RenderFarmClientThread::run 5\n");
675 result = read_package(socket_fd, package);
676 //printf("RenderFarmClientThread::run 6 %d\n", result);
682 //printf("RenderFarmClientThread::run 7\n");
684 result = send_completion(socket_fd);
692 if(package_renderer.render_package(package))
694 //printf("RenderFarmClientThread::run 8\n");
695 result = send_completion(socket_fd);
700 frames_per_second = package_renderer.frames_per_second;
701 // frames_per_second = (double)(package->video_end - package->video_start) /
702 // ((double)timer.get_difference() / 1000);
704 //printf("RenderFarmClientThread::run 9\n");
711 //printf("RenderFarmClientThread::run 9\n");
712 default_asset->Garbage::remove_user();
713 //printf("RenderFarmClientThread::run 10\n");
714 edl->Garbage::remove_user();
715 //printf("RenderFarmClientThread::run 11\n");
717 printf(_("RenderFarmClientThread::run: Session finished.\n"));
728 RenderFarmKeepalive::RenderFarmKeepalive(
729 RenderFarmClientThread *client_thread)
732 this->client_thread = client_thread;
736 RenderFarmKeepalive::~RenderFarmKeepalive()
743 void RenderFarmKeepalive::run()
752 //printf("RenderFarmKeepalive::run 1\n");
753 // watchdog thread kills this if it gets stuck
754 client_thread->ping_server();
755 //printf("RenderFarmKeepalive::run 10\n");
774 FarmPackageRenderer::FarmPackageRenderer(RenderFarmClientThread *thread,
778 this->thread = thread;
779 this->socket_fd = socket_fd;
784 FarmPackageRenderer::~FarmPackageRenderer()
789 int FarmPackageRenderer::get_result()
791 thread->lock("FarmPackageRenderer::get_result");
792 thread->send_request_header(RENDERFARM_GET_RESULT,
794 unsigned char data[1];
796 if(thread->read_socket((char*)data, 1) != 1)
805 void FarmPackageRenderer::set_result(int value)
807 thread->lock("FarmPackageRenderer::set_result");
808 thread->send_request_header(RENDERFARM_SET_RESULT,
810 unsigned char data[1];
812 thread->write_socket((char*)data, 1);
816 void FarmPackageRenderer::set_progress(int64_t total_samples)
818 thread->lock("FarmPackageRenderer::set_progress");
819 thread->send_request_header(RENDERFARM_PROGRESS,
821 unsigned char datagram[8];
823 STORE_INT32(total_samples);
826 int64_t fixed = (!EQUIV(frames_per_second, 0.0)) ?
827 (int64_t)(frames_per_second * 65536.0) : 0;
831 thread->write_socket((char*)datagram, 8);
835 int FarmPackageRenderer::set_video_map(int64_t position, int value)
838 unsigned char datagram[8];
839 char return_value[1];
842 thread->lock("FarmPackageRenderer::set_video_map");
843 thread->send_request_header(RENDERFARM_SET_VMAP,
845 STORE_INT32(position);
847 thread->write_socket((char*)datagram, 8);
849 // Get completion since the GUI may be locked for a long time.
850 if(!thread->read_socket(return_value, 1))