| 
									
										
										
										
											2009-11-01 15:21:20 +00:00
										 |  |  | # ##### BEGIN GPL LICENSE BLOCK ##### | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #  This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  | #  modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  | #  as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  | #  of the License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2009-12-29 00:04:57 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2009-11-01 15:21:20 +00:00
										 |  |  | #  This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | #  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | #  GNU General Public License for more details. | 
					
						
							| 
									
										
										
										
											2009-12-29 00:04:57 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2009-11-01 15:21:20 +00:00
										 |  |  | #  You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | #  along with this program; if not, write to the Free Software Foundation, | 
					
						
							|  |  |  | #  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # ##### END GPL LICENSE BLOCK ##### | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-29 17:25:22 +00:00
										 |  |  | import sys, os | 
					
						
							| 
									
										
										
										
											2009-09-04 01:33:22 +00:00
										 |  |  | import re | 
					
						
							| 
									
										
										
										
											2009-11-18 17:07:42 +00:00
										 |  |  | import http, http.client, http.server, urllib, socket | 
					
						
							| 
									
										
										
										
											2009-08-29 17:25:22 +00:00
										 |  |  | import subprocess, shutil, time, hashlib | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-01 01:09:05 +00:00
										 |  |  | import netrender.model | 
					
						
							| 
									
										
										
										
											2009-08-29 17:25:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-18 17:07:42 +00:00
										 |  |  | try: | 
					
						
							|  |  |  |   import bpy | 
					
						
							|  |  |  | except: | 
					
						
							|  |  |  |   bpy = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-16 21:00:25 +00:00
										 |  |  | VERSION = bytes("0.7", encoding='utf8') | 
					
						
							| 
									
										
										
										
											2009-08-29 17:25:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-19 22:11:26 +00:00
										 |  |  | # Jobs status | 
					
						
							|  |  |  | JOB_WAITING = 0 # before all data has been entered | 
					
						
							|  |  |  | JOB_PAUSED = 1 # paused by user | 
					
						
							| 
									
										
										
										
											2009-09-23 21:46:29 +00:00
										 |  |  | JOB_FINISHED = 2 # finished rendering | 
					
						
							|  |  |  | JOB_QUEUED = 3 # ready to be dispatched | 
					
						
							| 
									
										
										
										
											2009-09-19 22:11:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-16 21:00:25 +00:00
										 |  |  | JOB_STATUS_TEXT = { | 
					
						
							|  |  |  |         JOB_WAITING: "Waiting", | 
					
						
							|  |  |  |         JOB_PAUSED: "Paused", | 
					
						
							|  |  |  |         JOB_FINISHED: "Finished", | 
					
						
							|  |  |  |         JOB_QUEUED: "Queued" | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-19 22:11:26 +00:00
										 |  |  | # Frames status | 
					
						
							| 
									
										
										
										
											2009-08-29 17:25:22 +00:00
										 |  |  | QUEUED = 0 | 
					
						
							|  |  |  | DISPATCHED = 1 | 
					
						
							|  |  |  | DONE = 2 | 
					
						
							|  |  |  | ERROR = 3 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-16 21:00:25 +00:00
										 |  |  | FRAME_STATUS_TEXT = { | 
					
						
							| 
									
										
										
										
											2009-12-29 00:04:57 +00:00
										 |  |  |         QUEUED: "Queued", | 
					
						
							|  |  |  |         DISPATCHED: "Dispatched", | 
					
						
							|  |  |  |         DONE: "Done", | 
					
						
							|  |  |  |         ERROR: "Error" | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-09-23 01:59:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-10 20:41:18 +00:00
										 |  |  | def rnaType(rna_type): | 
					
						
							| 
									
										
										
										
											2009-12-29 00:04:57 +00:00
										 |  |  |     if bpy: bpy.types.register(rna_type) | 
					
						
							|  |  |  |     return rna_type | 
					
						
							| 
									
										
										
										
											2009-09-10 20:41:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-16 21:00:25 +00:00
										 |  |  | def reporting(report, message, errorType = None): | 
					
						
							|  |  |  |     if errorType: | 
					
						
							|  |  |  |         t = 'ERROR' | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         t = 'INFO' | 
					
						
							| 
									
										
										
										
											2009-12-29 00:04:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-16 21:00:25 +00:00
										 |  |  |     if report: | 
					
						
							|  |  |  |         report(t, message) | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  |     elif errorType: | 
					
						
							|  |  |  |         raise errorType(message) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def clientScan(report = None): | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | 
					
						
							|  |  |  |         s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) | 
					
						
							|  |  |  |         s.settimeout(30) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         s.bind(('', 8000)) | 
					
						
							| 
									
										
										
										
											2009-12-29 00:04:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-16 21:00:25 +00:00
										 |  |  |         buf, address = s.recvfrom(64) | 
					
						
							| 
									
										
										
										
											2009-12-29 00:04:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-16 21:00:25 +00:00
										 |  |  |         address = address[0] | 
					
						
							|  |  |  |         port = int(str(buf, encoding='utf8')) | 
					
						
							| 
									
										
										
										
											2009-12-29 00:04:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-16 21:00:25 +00:00
										 |  |  |         reporting(report, "Master server found") | 
					
						
							| 
									
										
										
										
											2009-12-29 00:04:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-16 21:00:25 +00:00
										 |  |  |         return (address, port) | 
					
						
							|  |  |  |     except socket.timeout: | 
					
						
							|  |  |  |         reporting(report, "No master server on network", IOError) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return ("", 8000) # return default values | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-04 21:05:52 +00:00
										 |  |  | def clientConnection(address, port, report = None, scan = True): | 
					
						
							| 
									
										
										
										
											2009-12-16 21:00:25 +00:00
										 |  |  |     if address == "[default]": | 
					
						
							| 
									
										
										
										
											2009-11-18 18:00:46 +00:00
										 |  |  | #            calling operator from python is fucked, scene isn't in context | 
					
						
							|  |  |  | #			if bpy: | 
					
						
							|  |  |  | #				bpy.ops.render.netclientscan() | 
					
						
							|  |  |  | #			else: | 
					
						
							| 
									
										
										
										
											2010-01-04 21:05:52 +00:00
										 |  |  |         if not scan: | 
					
						
							|  |  |  |             return None | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         address, port = clientScan() | 
					
						
							|  |  |  |         if address == "": | 
					
						
							|  |  |  |             return None | 
					
						
							| 
									
										
										
										
											2009-12-29 00:04:57 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2009-12-16 21:00:25 +00:00
										 |  |  |         conn = http.client.HTTPConnection(address, port) | 
					
						
							| 
									
										
										
										
											2009-12-29 00:04:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-16 21:00:25 +00:00
										 |  |  |         if conn: | 
					
						
							|  |  |  |             if clientVerifyVersion(conn): | 
					
						
							|  |  |  |                 return conn | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 conn.close() | 
					
						
							|  |  |  |                 reporting(report, "Incorrect master version", ValueError) | 
					
						
							|  |  |  |     except Exception as err: | 
					
						
							|  |  |  |         if report: | 
					
						
							|  |  |  |             report('ERROR', str(err)) | 
					
						
							|  |  |  |             return None | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             raise | 
					
						
							| 
									
										
										
										
											2009-08-29 17:25:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def clientVerifyVersion(conn): | 
					
						
							| 
									
										
										
										
											2009-12-29 00:04:57 +00:00
										 |  |  |     conn.request("GET", "/version") | 
					
						
							|  |  |  |     response = conn.getresponse() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if response.status != http.client.OK: | 
					
						
							|  |  |  |         conn.close() | 
					
						
							|  |  |  |         return False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     server_version = response.read() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if server_version != VERSION: | 
					
						
							|  |  |  |         print("Incorrect server version!") | 
					
						
							|  |  |  |         print("expected", str(VERSION, encoding='utf8'), "received", str(server_version, encoding='utf8')) | 
					
						
							|  |  |  |         return False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return True | 
					
						
							| 
									
										
										
										
											2009-08-29 17:25:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-14 23:09:08 +00:00
										 |  |  | def fileURL(job_id, file_index): | 
					
						
							|  |  |  |     return "/file_%s_%i" % (job_id, file_index) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def logURL(job_id, frame_number): | 
					
						
							|  |  |  |     return "/log_%s_%i.log" % (job_id, frame_number) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-08 02:33:20 +00:00
										 |  |  | def renderURL(job_id, frame_number): | 
					
						
							|  |  |  |     return "/render_%s_%i.exr" % (job_id, frame_number) | 
					
						
							| 
									
										
										
										
											2009-12-14 23:09:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-15 18:09:01 +00:00
										 |  |  | def cancelURL(job_id): | 
					
						
							|  |  |  |     return "/cancel_%s" % (job_id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-02 00:07:55 +00:00
										 |  |  | def prefixPath(prefix_directory, file_path, prefix_path): | 
					
						
							| 
									
										
										
										
											2009-12-29 00:04:57 +00:00
										 |  |  |     if os.path.isabs(file_path): | 
					
						
							|  |  |  |         # if an absolute path, make sure path exists, if it doesn't, use relative local path | 
					
						
							|  |  |  |         full_path = file_path | 
					
						
							|  |  |  |         if not os.path.exists(full_path): | 
					
						
							|  |  |  |             p, n = os.path.split(full_path) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if prefix_path and p.startswith(prefix_path): | 
					
						
							|  |  |  |                 directory = prefix_directory + p[len(prefix_path):] | 
					
						
							|  |  |  |                 full_path = directory + os.sep + n | 
					
						
							|  |  |  |                 if not os.path.exists(directory): | 
					
						
							|  |  |  |                     os.mkdir(directory) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 full_path = prefix_directory + n | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         full_path = prefix_directory + file_path | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return full_path |