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
23 #include "bcsignals.h"
26 #include "condition.h"
29 #include "filesystem.h"
34 #include "packagedispatcher.h"
35 #include "preferences.h"
37 #include "renderfarm.h"
38 #include "renderfarmclient.h"
40 #include "transportque.h"
43 #include <arpa/inet.h>
47 #include <netinet/in.h>
51 #include <sys/socket.h>
52 #include <sys/types.h>
59 RenderFarmServer::RenderFarmServer(
61 PackageDispatcher *packages,
62 Preferences *preferences,
65 int64_t *total_return,
66 Mutex *total_return_lock,
71 this->mwindow = mwindow;
72 this->packages = packages;
73 this->preferences = preferences;
74 this->use_local_rate = use_local_rate;
75 this->result_return = result_return;
76 this->total_return = total_return;
77 this->total_return_lock = total_return_lock;
78 this->default_asset = default_asset;
80 this->brender = brender;
81 client_lock = new Mutex("RenderFarmServer::client_lock");
84 RenderFarmServer::~RenderFarmServer()
86 clients.remove_all_objects();
90 // Open connections to clients.
91 int RenderFarmServer::start_clients()
95 for( int i=0; i<preferences->get_enabled_nodes() && !result; ++i ) {
96 client_lock->lock("RenderFarmServer::start_clients");
97 RenderFarmServerThread *client = new RenderFarmServerThread(this, i);
98 clients.append(client);
100 result = client->start_loop();
101 client_lock->unlock();
107 // The render farm must wait for all the clients to finish.
108 int RenderFarmServer::wait_clients()
110 //printf("RenderFarmServer::wait_clients 1\n");
111 clients.remove_all_objects();
112 //printf("RenderFarmServer::wait_clients 2\n");
116 int RenderFarmServer::active_clients()
119 for( int i=0; i<clients.size(); ++i )
120 if( clients[i]->running() ) ++n;
124 // Waits for requests from every client.
125 // Joins when the client is finished.
126 RenderFarmServerThread::RenderFarmServerThread(RenderFarmServer *server,
130 this->server = server;
131 this->number = number;
133 frames_per_second = 0;
137 Thread::set_synchronous(1);
142 RenderFarmServerThread::~RenderFarmServerThread()
145 if( socket_fd >= 0 ) close(socket_fd);
152 int RenderFarmServerThread::open_client(const char *hostname, int port)
157 // Open file for master node
158 if( hostname[0] == '/' ) {
159 if( (socket_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0 ) {
160 perror(_("RenderFarmServerThread::open_client: socket\n"));
164 struct sockaddr_un addr;
165 addr.sun_family = AF_FILE;
166 strcpy(addr.sun_path, hostname);
167 int size = (offsetof(struct sockaddr_un, sun_path) +
168 strlen(hostname) + 1);
170 // The master node is always created by BRender. Keep trying for 30 seconds.
172 #define ATTEMPT_DELAY 100000
178 if( connect(socket_fd, (struct sockaddr*)&addr, size) < 0 ) {
180 if( attempt > 30000000 / ATTEMPT_DELAY ) {
181 fprintf(stderr, _("RenderFarmServerThread::open_client: %s: %s\n"),
187 usleep(ATTEMPT_DELAY);
191 }while(!result && !done);
197 if( (socket_fd = socket(PF_INET, SOCK_STREAM, 0)) < 0 ) {
198 perror(_("RenderFarmServerThread::start_loop: socket"));
203 struct sockaddr_in addr;
204 struct hostent *hostinfo;
205 addr.sin_family = AF_INET;
206 addr.sin_port = htons(port);
207 hostinfo = gethostbyname(hostname);
208 if( hostinfo == NULL ) {
209 fprintf(stderr, _("RenderFarmServerThread::open_client: unknown host %s.\n"),
214 addr.sin_addr = *(struct in_addr *) hostinfo->h_addr;
216 if( connect(socket_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0 ) {
217 fprintf(stderr, _("RenderFarmServerThread::open_client: %s: %s\n"),
226 if( result ) socket_fd = -1;
231 int RenderFarmServerThread::start_loop()
234 socket_fd = open_client(server->preferences->get_node_hostname(number),
235 server->preferences->get_node_port(number));
236 if( socket_fd < 0 ) result = 1;
239 int watchdog_timeout = server->preferences->renderfarm_watchdog_timeout;
240 if( watchdog_timeout > 0 ) {
241 watchdog = new RenderFarmWatchdog(watchdog_timeout, this, 0);
246 if( !result ) Thread::start();
261 int64_t RenderFarmServerThread::read_int64(int *error)
264 if( !error ) error = &temp;
266 unsigned char data[sizeof(int64_t)];
267 *error = (read_socket((char*)data, sizeof(int64_t)) !=
270 // Make it return 1 if error so it can be used to read a result code from the
274 result = (((int64_t)data[0]) << 56) |
275 (((uint64_t)data[1]) << 48) |
276 (((uint64_t)data[2]) << 40) |
277 (((uint64_t)data[3]) << 32) |
278 (((uint64_t)data[4]) << 24) |
279 (((uint64_t)data[5]) << 16) |
280 (((uint64_t)data[6]) << 8) |
286 int RenderFarmServerThread::write_int64(int64_t value)
288 unsigned char data[sizeof(int64_t)];
289 data[0] = (value >> 56) & 0xff;
290 data[1] = (value >> 48) & 0xff;
291 data[2] = (value >> 40) & 0xff;
292 data[3] = (value >> 32) & 0xff;
293 data[4] = (value >> 24) & 0xff;
294 data[5] = (value >> 16) & 0xff;
295 data[6] = (value >> 8) & 0xff;
296 data[7] = value & 0xff;
297 return (write_socket((char*)data, sizeof(int64_t)) !=
303 int RenderFarmServerThread::read_socket(char *data, int len)
307 //printf("RenderFarmServerThread::read_socket 1\n");
309 watchdog->begin_request();
310 while(len > 0 && bytes_read >= 0)
313 bytes_read = read(socket_fd, data + offset, len);
315 if( bytes_read > 0 ) {
317 offset += bytes_read;
324 watchdog->end_request();
325 //printf("RenderFarmServerThread::read_socket 10\n");
330 int RenderFarmServerThread::write_socket(char *data, int len)
332 //printf("RenderFarmServerThread::write_socket 1\n");
333 int result = write(socket_fd, data, len);
334 //printf("RenderFarmServerThread::write_socket 10\n");
339 void RenderFarmServerThread::reallocate_buffer(int size)
341 if( buffer && buffer_allocated < size ) {
346 if( !buffer && size ) {
347 buffer = new unsigned char[size];
348 buffer_allocated = size;
352 void RenderFarmServerThread::run()
355 unsigned char header[5];
361 buffer_allocated = 0;
362 // fs_server = new RenderFarmFSServer(this);
363 // fs_server->initialize();
366 // Send command to run package renderer.
367 write_int64(RENDERFARM_PACKAGES);
373 // Wait for requests.
374 // Requests consist of request ID's and accompanying buffers.
376 bytes_read = read_socket((char*)header, 5);
377 //printf("RenderFarmServerThread::run 1\n");
378 if( bytes_read != 5 ) {
383 int request_id = header[0];
384 int64_t request_size = (((u_int32_t)header[1]) << 24) |
385 (((u_int32_t)header[2]) << 16) |
386 (((u_int32_t)header[3]) << 8) |
387 (u_int32_t)header[4];
389 reallocate_buffer(request_size);
391 // Get accompanying buffer
392 bytes_read = read_socket((char*)buffer, request_size);
394 //printf("RenderFarmServerThread::run 2 %d %jd %d\n", request_id, request_size, bytes_read);
395 if( bytes_read != request_size ) {
399 //printf("RenderFarmServerThread::run 3\n");
401 switch( request_id ) {
402 case RENDERFARM_PREFERENCES:
406 case RENDERFARM_ASSET:
414 case RENDERFARM_PACKAGE:
415 send_package(buffer);
418 case RENDERFARM_PROGRESS:
419 set_progress(buffer);
422 case RENDERFARM_SET_RESULT:
426 case RENDERFARM_SET_VMAP:
427 set_video_map(buffer);
430 case RENDERFARM_GET_RESULT:
434 case RENDERFARM_DONE:
435 //printf("RenderFarmServerThread::run 10\n");
439 case RENDERFARM_KEEPALIVE:
443 // if( fs_server->handle_request(request_id, request_size, (unsigned char*)buffer) ) break;
444 printf(_("RenderFarmServerThread::run: unknown request %02x\n"), request_id);
447 //printf("RenderFarmServerThread::run 10 %d %jd\n", request_id, request_size);
449 //printf("RenderFarmServerThread::run 20\n");
451 // Don't let watchdog kill the entire renderfarm when a client finishes normally.
452 delete watchdog; watchdog = 0;
456 void RenderFarmServerThread::write_string(char *string)
461 len = strlen(string) + 1;
462 datagram = new char[len + 4];
464 memcpy(datagram + i, string, len);
465 write_socket((char*)datagram, len + 4);
466 //printf("RenderFarmServerThread::write_string %02x%02x%02x%02x\n",
467 // datagram[0], datagram[1], datagram[2], datagram[3]);
473 void RenderFarmServerThread::send_preferences()
478 server->preferences->save_defaults(&defaults);
479 defaults.save_string(string);
480 write_string(string);
484 void RenderFarmServerThread::send_asset()
489 // The asset must be sent in two segments.
490 // One segment is stored in the EDL and contains decoding information.
491 // One segment is stored in the asset and contains encoding information.
492 server->default_asset->save_defaults(&defaults,
499 defaults.save_string(string1);
501 server->default_asset->write(&file, 0, 0);
502 file.terminate_string();
504 write_string(string1);
505 write_string(file.string());
510 void RenderFarmServerThread::send_edl()
515 server->edl->save_xml(&file, 0);
516 file.terminate_string();
517 //printf("RenderFarmServerThread::send_edl\n%s\n\n", file.string);
519 write_string(file.string());
520 //printf("RenderFarmServerThread::send_edl 2\n");
524 void RenderFarmServerThread::send_package(unsigned char *buffer)
526 this->frames_per_second = (double)((((u_int32_t)buffer[0]) << 24) |
527 (((u_int32_t)buffer[1]) << 16) |
528 (((u_int32_t)buffer[2]) << 8) |
529 ((u_int32_t)buffer[3])) /
532 //printf("RenderFarmServerThread::send_package 1 %f\n", frames_per_second);
533 RenderPackage *package =
534 server->packages->get_package(frames_per_second,
536 server->use_local_rate);
538 //printf("RenderFarmServerThread::send_package 2\n");
539 datagram = new char[BCTEXTLEN];
543 //printf("RenderFarmServerThread::send_package 1\n");
544 datagram[0] = datagram[1] = datagram[2] = datagram[3] = 0;
545 write_socket(datagram, 4);
550 //printf("RenderFarmServerThread::send_package 10\n");
552 strcpy(&datagram[i], package->path);
553 i += strlen(package->path);
556 STORE_INT32(package->audio_start);
557 STORE_INT32(package->audio_end);
558 STORE_INT32(package->video_start);
559 STORE_INT32(package->video_end);
560 int use_brender = (server->brender ? 1 : 0);
561 STORE_INT32(use_brender);
562 STORE_INT32(package->audio_do);
563 STORE_INT32(package->video_do);
567 STORE_INT32(len - 4);
569 write_socket(datagram, len);
576 void RenderFarmServerThread::set_progress(unsigned char *buffer)
578 server->total_return_lock->lock("RenderFarmServerThread::set_progress");
579 *server->total_return += (int64_t)(((u_int32_t)buffer[0]) << 24) |
580 (((u_int32_t)buffer[1]) << 16) |
581 (((u_int32_t)buffer[2]) << 8) |
582 ((u_int32_t)buffer[3]);
583 frames_per_second = (double)((((u_int32_t)buffer[4]) << 24) |
584 (((u_int32_t)buffer[5]) << 16) |
585 (((u_int32_t)buffer[6]) << 8) |
586 ((u_int32_t)buffer[7])) /
588 server->total_return_lock->unlock();
590 server->preferences->set_rate(frames_per_second, number);
592 // This locks the preferences
593 if( server->mwindow ) server->mwindow->preferences->copy_rates_from(
594 server->preferences);
597 int RenderFarmServerThread::set_video_map(unsigned char *buffer)
599 if( server->brender ) {
600 server->brender->set_video_map((int64_t)(((u_int32_t)buffer[0]) << 24) |
601 (((u_int32_t)buffer[1]) << 16) |
602 (((u_int32_t)buffer[2]) << 8) |
603 ((u_int32_t)buffer[3]),
604 (int64_t)(((u_int32_t)buffer[4]) << 24) |
605 (((u_int32_t)buffer[5]) << 16) |
606 (((u_int32_t)buffer[6]) << 8) |
607 ((u_int32_t)buffer[7]));
608 char return_value[1];
610 write_socket(return_value, 1);
617 void RenderFarmServerThread::set_result(unsigned char *buffer)
619 //printf("RenderFarmServerThread::set_result %p\n", buffer);
620 if( !*server->result_return )
621 *server->result_return = buffer[0];
625 void RenderFarmServerThread::get_result()
627 unsigned char data[1];
628 data[0] = *server->result_return;
629 write_socket((char*)data, 1);
645 RenderFarmWatchdog::RenderFarmWatchdog(int timeout_secs,
646 RenderFarmServerThread *server,
647 RenderFarmClientThread *client)
650 this->timeout_usecs = timeout_secs * 1000000;
651 this->server = server;
652 this->client = client;
653 next_request = new Condition(0, "RenderFarmWatchdog::next_request", 0);
654 request_complete = new Condition(0, "RenderFarmWatchdog::request_complete", 0);
658 RenderFarmWatchdog::~RenderFarmWatchdog()
661 next_request->unlock();
662 request_complete->unlock();
665 delete request_complete;
668 void RenderFarmWatchdog::begin_request()
670 next_request->unlock();
673 void RenderFarmWatchdog::end_request()
675 request_complete->unlock();
678 void RenderFarmWatchdog::run()
681 next_request->lock("RenderFarmWatchdog::run");
682 int result = request_complete->timed_lock(timeout_usecs, "RenderFarmWatchdog::run");
683 //printf("RenderFarmWatchdog::run 1 %d\n", result);
687 printf("RenderFarmWatchdog::run 1 killing client pid %d\n", client->pid);
688 kill(client->pid, SIGKILL);
691 printf("RenderFarmWatchdog::run 1 killing server thread %p\n", server);
693 unsigned char buffer[4];
695 server->set_result(buffer);