Appendix B: Processing Resource Applet
Appendix C: Control Server Source Code
#!/usr/bin/python2.4 import sys import re from xmlrpclib import Binary from SimpleXMLRPCServer import CGIXMLRPCRequestHandler from MySQLdb import connect, escape_string from dbcredentials import host, user_name, password, database, flags lucas_lehmer_job_proc = "call assign_lucas_lehmer_task(%d)" trial_division_job_proc = "call assign_trial_division_task(%d)" sieve_job_proc = "call assign_sieve_task(%d, %d)" add_sieved_primes_proc = "call add_sieved_primes(%d, %d, '%s', '%s')" confirm_possible_prime_proc = "call confirm_possible_prime(%d, %d, '%s')" update_lucas_lehmer_status_proc = \ "call update_lucas_lehmer_status(%d, %d, '%s', '%s')" confirm_prime_proc = "call confirm_mersenne_prime(%d, %d, '%s')" key_expiry = 1800 segment_size = 100000 sieve_task_flag = 1 trial_division_task_flag = 2 lucas_lehmer_task_flag = 4 key_pattern = re.compile(r"^[0-9a-f\-]*$", re.IGNORECASE) def open_connection(): """Open connection to MySQL; return Connection object.""" return connect(host, user_name, password, database, client_flag=flags) def key_safe(job_key): """Ensure job key contains only legal characters.""" return (key_pattern.match(job_key) is not None) def next_lucas_lehmer_job(): """Get next Lucas-Lehmer task, if one exists.""" connection = open_connection() cursor = connection.cursor() cursor.execute(lucas_lehmer_job_proc % key_expiry) result = cursor.fetchone() cursor.close() connection.close() return result def next_trial_division_job(): """Get next trial division task, if one exists.""" connection = open_connection() cursor = connection.cursor() cursor.execute(trial_division_job_proc % key_expiry) result = cursor.fetchone() cursor.close() connection.close() return result def next_sieve_job(): """Get next Sieve task, if one exists.""" connection = open_connection() cursor = connection.cursor() cursor.execute(sieve_job_proc % (key_expiry, segment_size)) result = cursor.fetchone() cursor.close() connection.close() return result def request_work(job_flags): """Checks for work on the server.""" result = None task = None try: if ((task is None) and ((job_flags & lucas_lehmer_task_flag) != 0)): task = next_lucas_lehmer_job() if (task is not None): iterations = task[1] if (iterations == 0): seed = task[0] key = task[3] result = (lucas_lehmer_task_flag, (seed, iterations, key)) else: seed = task[0] residue = task[2] key = task[3] result = (lucas_lehmer_task_flag, (seed, iterations, Binary(residue), key)) if ((task is None) and ((job_flags & trial_division_task_flag) != 0)): task = next_trial_division_job() if (task is not None): result = (trial_division_task_flag, task) if ((task is None) and ((job_flags & sieve_task_flag) != 0)): task = next_sieve_job() if (task is not None): result = (sieve_task_flag, task) if (task is None): result = (0,) except: result = str(sys.exc_info()[1]) return result def post_sieved_primes(lower, upper, flags, job_key): """Update the database with results from performing a segmented sieve.""" result = False if (key_safe(job_key)): try: connection = open_connection() cursor = connection.cursor() cursor.execute(add_sieved_primes_proc % (lower, upper, escape_string(flags.data), job_key)) cursor.close() connection.close() result = True except: result = str(sys.exc_info()[1]) return result def post_trial_division_result(seed, is_possible_prime, job_key): """Update the database with result of trial division.""" result = False if (key_safe(job_key)): try: connection = open_connection() cursor = connection.cursor() cursor.execute(confirm_possible_prime_proc % (seed, is_possible_prime, job_key)) cursor.close() connection.close() result = True except: result = str(sys.exc_info()[1]) return result def post_lucas_lehmer_status(seed, iterations, residue, job_key): """Update the database with in-process status of Lucas-Lehmer test.""" result = False if (key_safe(job_key)): try: connection = open_connection() cursor = connection.cursor() cursor.execute(update_lucas_lehmer_status_proc % (seed, iterations, escape_string(residue.data), job_key)) cursor.close() connection.commit() connection.close() result = True except: result = str(sys.exc_info()[1]) return result def post_lucas_lehmer_result(seed, is_prime, job_key): """Update the database with result of Lucas-Lehmer primality test.""" result = False if (key_safe(job_key)): try: connection = open_connection() cursor = connection.cursor() cursor.execute(confirm_prime_proc % (seed, is_prime, job_key)) cursor.close() connection.close() result = True except: result = str(sys.exc_info()[1]) return result handler = CGIXMLRPCRequestHandler() handler.register_function(request_work) handler.register_function(post_sieved_primes) handler.register_function(post_trial_division_result) handler.register_function(post_lucas_lehmer_status) handler.register_function(post_lucas_lehmer_result) handler.handle_request()