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>
55 #define AF_FILE AF_LOCAL
58 // The render client waits for connections from the server.
59 // Then it starts a thread for each connection.
60 RenderFarmClient::RenderFarmClient(int port,
65 char string[BCTEXTLEN];
68 this->deamon_path = deamon_path;
69 SigHandler *signals = new SigHandler;
70 signals->initialize("/tmp/cinelerra_farm%d.dmp");
73 (void)nice(nice_value);
76 MWindow::init_defaults(boot_defaults, config_path);
77 boot_preferences = new Preferences;
78 boot_preferences->load_defaults(boot_defaults);
79 MWindow::init_plugins(0, boot_preferences);
80 BC_Signals::set_catch_segv(boot_preferences->trap_sigsegv);
81 BC_Signals::set_catch_intr(0);
82 if( boot_preferences->trap_sigsegv ) {
83 BC_Trace::enable_locks();
86 BC_Trace::disable_locks();
89 strcpy(string, boot_preferences->plugin_dir);
90 strcat(string, "/" FONT_SEARCHPATH);
91 BC_Resources::init_fontconfig(string);
97 RenderFarmClient::~RenderFarmClient()
100 delete boot_preferences;
104 void RenderFarmClient::main_loop()
107 BC_WindowBase::get_resources()->vframe_shm = 1;
110 // Open listening port
114 struct sockaddr_in addr;
116 addr.sin_family = AF_INET;
117 addr.sin_port = htons((unsigned short)port);
118 addr.sin_addr.s_addr = htonl(INADDR_ANY);
120 if((socket_fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
122 perror(_("RenderFarmClient::main_loop: socket"));
127 (struct sockaddr*)&addr,
131 _("RenderFarmClient::main_loop: bind port %d: %s"),
139 struct sockaddr_un addr;
140 addr.sun_family = AF_FILE;
141 strcpy(addr.sun_path, deamon_path);
142 int size = (offsetof(struct sockaddr_un, sun_path) +
143 strlen(deamon_path) + 1);
145 if((socket_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
147 perror(_("RenderFarmClient::main_loop: socket"));
152 (struct sockaddr*)&addr,
156 _("RenderFarmClient::main_loop: bind path %s: %s\n"),
163 // Wait for connections
164 printf("RenderFarmClient::main_loop: client started\n");
167 if(listen(socket_fd, 256) < 0) {
168 perror(_("RenderFarmClient::main_loop: listen"));
176 struct sockaddr_in clientname;
177 socklen_t size = sizeof(clientname);
178 if( (new_socket_fd = accept(socket_fd,
179 (struct sockaddr*)&clientname, &size) ) < 0 ) {
180 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);
190 struct sockaddr_un clientname;
191 socklen_t size = sizeof(clientname);
192 if( (new_socket_fd = accept(socket_fd,
193 (struct sockaddr*)&clientname, &size)) < 0 ) {
194 perror(_("RenderFarmClient::main_loop: accept"));
197 printf("RenderFarmClient::main_loop: Session started from %s\n", clientname.sun_path);
198 RenderFarmClientThread *thread =
199 new RenderFarmClientThread(this);
200 thread->main_loop(new_socket_fd);
205 void RenderFarmClient::kill_client()
207 printf("RenderFarmClient::kill_client 1\n");
210 printf("RenderFarmClient::kill_client 2\n");
212 kill(this_pid, SIGKILL);
217 // The thread requests jobs from the server until the job table is empty
218 // or the server reports an error. This thread must poll the server
219 // after every frame for the error status.
220 // Detaches when finished.
221 RenderFarmClientThread::RenderFarmClientThread(RenderFarmClient *client)
224 this->client = client;
226 frames_per_second = 0;
227 Thread::set_synchronous(0);
229 mutex_lock = new Mutex("RenderFarmClientThread::mutex_lock");
234 RenderFarmClientThread::~RenderFarmClientThread()
236 // if(fs_client) delete fs_client;
243 int RenderFarmClientThread::send_request_header(int request,
246 unsigned char datagram[5];
247 datagram[0] = request;
251 // printf("RenderFarmClientThread::send_request_header %d %02x%02x%02x%02x%02x\n",
252 // request, datagram[0], datagram[1], datagram[2], datagram[3], datagram[4]);
254 return (write_socket((char*)datagram, 5) != 5);
257 int RenderFarmClientThread::write_socket(char *data, int len)
259 //printf("RenderFarmClientThread::write_socket 1\n");
260 int result = write(socket_fd, data, len);
261 //printf("RenderFarmClientThread::write_socket 10\n");
265 int RenderFarmClientThread::read_socket(char *data, int len)
269 //printf("RenderFarmClientThread::read_socket 1\n");
270 watchdog->begin_request();
271 while(len > 0 && bytes_read >= 0)
273 bytes_read = read(socket_fd, data + offset, len);
277 offset += bytes_read;
285 watchdog->end_request();
286 //printf("RenderFarmClientThread::read_socket 10\n");
291 int RenderFarmClientThread::write_int64(int64_t value)
293 unsigned char data[sizeof(int64_t)];
294 data[0] = (value >> 56) & 0xff;
295 data[1] = (value >> 48) & 0xff;
296 data[2] = (value >> 40) & 0xff;
297 data[3] = (value >> 32) & 0xff;
298 data[4] = (value >> 24) & 0xff;
299 data[5] = (value >> 16) & 0xff;
300 data[6] = (value >> 8) & 0xff;
301 data[7] = value & 0xff;
302 return (write_socket((char*)data, sizeof(int64_t)) != sizeof(int64_t));
305 int64_t RenderFarmClientThread::read_int64(int *error)
308 if(!error) error = &temp;
310 unsigned char data[sizeof(int64_t)];
311 *error = (read_socket((char*)data, sizeof(int64_t)) != sizeof(int64_t));
313 // Make it return 1 if error so it can be used to read a result code from the
318 result = (((int64_t)data[0]) << 56) |
319 (((uint64_t)data[1]) << 48) |
320 (((uint64_t)data[2]) << 40) |
321 (((uint64_t)data[3]) << 32) |
322 (((uint64_t)data[4]) << 24) |
323 (((uint64_t)data[5]) << 16) |
324 (((uint64_t)data[6]) << 8) |
330 void RenderFarmClientThread::read_string(char* &string)
332 unsigned char header[4];
333 if(read_socket((char*)header, 4) != 4)
339 int64_t len = (((u_int32_t)header[0]) << 24) |
340 (((u_int32_t)header[1]) << 16) |
341 (((u_int32_t)header[2]) << 8) |
342 ((u_int32_t)header[3]);
346 string = new char[len];
347 if(read_socket(string, len) != len)
358 void RenderFarmClientThread::abort()
360 send_completion(socket_fd);
365 void RenderFarmClientThread::lock(const char *location)
367 mutex_lock->lock(location);
370 void RenderFarmClientThread::unlock()
372 mutex_lock->unlock();
375 void RenderFarmClientThread::get_command(int socket_fd, int *command)
378 *command = read_int64(&error);
387 void RenderFarmClientThread::read_preferences(int socket_fd,
388 Preferences *preferences)
390 lock("RenderFarmClientThread::read_preferences");
391 send_request_header(RENDERFARM_PREFERENCES,
398 defaults.load_string((char*)string);
399 preferences->load_defaults(&defaults);
407 void RenderFarmClientThread::read_asset(int socket_fd, Asset *asset)
409 lock("RenderFarmClientThread::read_asset");
410 send_request_header(RENDERFARM_ASSET,
415 read_string(string1);
416 read_string(string2);
421 file.read_from_string((char*)string2);
427 defaults.load_string((char*)string1);
428 asset->load_defaults(&defaults,
436 //printf("RenderFarmClientThread::read_asset %d\n", __LINE__);
444 void RenderFarmClientThread::read_edl(int socket_fd,
445 EDL *edl, Preferences *preferences)
447 lock("RenderFarmClientThread::read_edl");
448 send_request_header(RENDERFARM_EDL, 0);
454 file.read_from_string((char*)string);
457 edl->load_xml(&file, LOAD_ALL);
461 int RenderFarmClientThread::read_package(int socket_fd, RenderPackage *package)
463 lock("RenderFarmClientThread::read_package");
464 send_request_header(RENDERFARM_PACKAGE,
467 unsigned char datagram[5];
471 // Fails if -ieee isn't set.
472 int64_t fixed = !EQUIV(frames_per_second, 0.0) ?
473 (int64_t)(frames_per_second * 65536.0) : 0;
475 write_socket((char*)datagram, 4);
478 //printf("RenderFarmClientThread::read_package 1 %f %ld\n", frames_per_second, fixed);
480 unsigned char *data_ptr;
482 //printf("RenderFarmClientThread::read_package 2 %p\n", data);
483 // Signifies end of session.
486 // printf(_("RenderFarmClientThread::read_package no output path received.\n"));
491 //printf("RenderFarmClientThread::read_package 2\n");
494 data_ptr = (unsigned char*)data;
495 strcpy(package->path, data);
496 data_ptr += strlen(package->path);
498 package->audio_start = READ_INT32(data_ptr);
500 package->audio_end = READ_INT32(data_ptr);
502 package->video_start = READ_INT32(data_ptr);
504 package->video_end = READ_INT32(data_ptr);
506 package->use_brender = READ_INT32(data_ptr);
508 package->audio_do = READ_INT32(data_ptr);
510 package->video_do = READ_INT32(data_ptr);
518 int RenderFarmClientThread::send_completion(int socket_fd)
520 lock("RenderFarmClientThread::send_completion");
521 int result = send_request_header(RENDERFARM_DONE, 0);
527 void RenderFarmClientThread::ping_server()
529 lock("RenderFarmClientThread::ping_server");
530 send_request_header(RENDERFARM_KEEPALIVE, 0);
536 void RenderFarmClientThread::main_loop(int socket_fd)
538 this->socket_fd = socket_fd;
543 void RenderFarmClientThread::run()
545 // Create new memory space
550 waitpid(pid, &return_value, 0);
554 // Get the pid of the fork if inside the fork
556 BC_Signals::set_trap_hook(trap_hook, this);
559 int socket_fd = this->socket_fd;
560 int watchdog_timeout = client->boot_preferences->renderfarm_watchdog_timeout;
561 if( watchdog_timeout > 0 )
562 init_client_keepalive(watchdog_timeout);
564 // Get command to run
566 lock("RenderFarmClientThread::run");
567 get_command(socket_fd, &command);
570 //printf("RenderFarmClientThread::run command=%d\n", command);
574 case RENDERFARM_TUNER:
577 case RENDERFARM_PACKAGES:
578 do_packages(socket_fd);
586 void RenderFarmClientThread::init_client_keepalive(int timeout_secs)
588 keep_alive = new RenderFarmKeepalive(this);
590 watchdog = new RenderFarmWatchdog(timeout_secs, 0, this);
596 void RenderFarmClientThread::do_tuner(int socket_fd)
598 // Currently only 1 tuner driver. Maybe more someday.
599 // DVBTune server(this);
600 // server.main_loop();
601 // ::close(socket_fd);
605 void RenderFarmClientThread::do_packages(int socket_fd)
607 RenderPackage *package;
608 Asset *default_asset;
609 Preferences *preferences;
613 FarmPackageRenderer package_renderer(this, socket_fd);
618 //printf("RenderFarmClientThread::run 2\n");
620 preferences = new Preferences;
621 default_asset = new Asset;
622 package = new RenderPackage;
624 edl->create_objects();
626 //printf("RenderFarmClientThread::run 3\n");
627 read_preferences(socket_fd, preferences);
628 //printf("RenderFarmClientThread::run 4\n");
629 read_asset(socket_fd, default_asset);
630 //printf("RenderFarmClientThread::run 5\n");
631 read_edl(socket_fd, edl, preferences);
633 //printf("RenderFarmClientThread::run 6\n");
635 package_renderer.initialize(0, edl, preferences, default_asset);
640 //printf("RenderFarmClientThread::run 5\n");
641 result = read_package(socket_fd, package);
642 //printf("RenderFarmClientThread::run 6 %d\n", result);
648 //printf("RenderFarmClientThread::run 7\n");
650 result = send_completion(socket_fd);
658 if(package_renderer.render_package(package))
660 //printf("RenderFarmClientThread::run 8\n");
661 result = send_completion(socket_fd);
666 frames_per_second = package_renderer.frames_per_second;
667 // frames_per_second = (double)(package->video_end - package->video_start) /
668 // ((double)timer.get_difference() / 1000);
670 //printf("RenderFarmClientThread::run 9\n");
677 //printf("RenderFarmClientThread::run 9\n");
678 default_asset->Garbage::remove_user();
679 //printf("RenderFarmClientThread::run 10\n");
680 edl->Garbage::remove_user();
682 //printf("RenderFarmClientThread::run 11\n");
684 printf(_("RenderFarmClientThread::run: Session finished.\n"));
687 void RenderFarmClientThread::trap_hook(FILE *fp, void *vp)
689 RenderFarmClientThread *thread = (RenderFarmClientThread*)vp;
690 fprintf(fp, "\nEDL:\n");
691 if( thread->edl ) thread->edl->dump(fp);
696 RenderFarmKeepalive::RenderFarmKeepalive(
697 RenderFarmClientThread *client_thread)
700 this->client_thread = client_thread;
704 RenderFarmKeepalive::~RenderFarmKeepalive()
711 void RenderFarmKeepalive::run()
719 //printf("RenderFarmKeepalive::run 1\n");
720 // watchdog thread kills this if it gets stuck
721 client_thread->ping_server();
722 //printf("RenderFarmKeepalive::run 10\n");
728 FarmPackageRenderer::FarmPackageRenderer(RenderFarmClientThread *thread,
732 this->thread = thread;
733 this->socket_fd = socket_fd;
737 FarmPackageRenderer::~FarmPackageRenderer()
742 int FarmPackageRenderer::get_result()
744 thread->lock("FarmPackageRenderer::get_result");
745 thread->send_request_header(RENDERFARM_GET_RESULT,
747 unsigned char data[1];
749 if(thread->read_socket((char*)data, 1) != 1)
758 void FarmPackageRenderer::set_result(int value)
760 thread->lock("FarmPackageRenderer::set_result");
761 thread->send_request_header(RENDERFARM_SET_RESULT,
763 unsigned char data[1];
765 thread->write_socket((char*)data, 1);
769 void FarmPackageRenderer::set_progress(int64_t total_samples)
771 thread->lock("FarmPackageRenderer::set_progress");
772 thread->send_request_header(RENDERFARM_PROGRESS,
774 unsigned char datagram[8];
776 STORE_INT32(total_samples);
779 int64_t fixed = (!EQUIV(frames_per_second, 0.0)) ?
780 (int64_t)(frames_per_second * 65536.0) : 0;
784 thread->write_socket((char*)datagram, 8);
788 int FarmPackageRenderer::set_video_map(int64_t position, int value)
791 unsigned char datagram[8];
792 char return_value[1];
795 thread->lock("FarmPackageRenderer::set_video_map");
796 thread->send_request_header(RENDERFARM_SET_VMAP,
798 STORE_INT32(position);
800 thread->write_socket((char*)datagram, 8);
802 // Get completion since the GUI may be locked for a long time.
803 if(!thread->read_socket(return_value, 1))