def start(self):
        self.running = True
        conn_listener = Listener(self.index, self.port, self.clock)
        conn_listener.start()

        for i in range(constants.EVENTS_COUNT):
            if not self.running:
                break

            sleep(min(random.random() + 0.5, 1))
            self.clock.increment()
            if random.random() > self.chance:
                # local
                self.logger.info(f"{perf_counter_ns() // 1000} {self.index} {self.clock.get()}{self.index} l")
            else:
                # remote
                idx, node = self.get_node()
                try:
                    self.send_message(idx, node)
                except SocketTimeout:
                    print(
                        f"""Error: Timeout sending message to node {idx} ({node["host"]}:{node["port"]})"""
                    )
                    break

        conn_listener.stop()
Example #2
0
class Accompanist:
    def __init__(self):
        self.queue_in = Queue()
        self.queue_out = Queue()
        self.predictor_queue = Queue()
        self.running = Value('i', False)
        self.tempo = Value('f', default_tempo)
        self.deadline = Value('f', 0)

        self.player = Player(self.queue_out, self.running, self.tempo,
                             self.deadline)
        self.predictor = ChordPredictor(self.queue_in, self.queue_out)
        self.listener = Listener(self.queue_in, self.running, self.tempo,
                                 self.deadline)

    def run(self):
        self.running.value = True
        self.listener.run()
        self.player.run()
        self.predictor.run()

    def stop(self):
        self.running.value = False
        self.player.stop()
        self.listener.stop()
        self.predictor.stop()
        self.queue_in = Queue()
        self.queue_out = Queue()

    def set_tempo(self, tempo=default_tempo):
        self.tempo.value = tempo

    def set_deadline(self, deadline=0):
        self.deadline.value = deadline

    player = None
    listener = None
    predictor = None
    queue_in = None
    queue_out = None
    running = None
    tempo = None
    deadline = None
    process = None
Example #3
0
def EmonHub():

    # Load settings
    settings = ConfigObj("emonhub.conf", file_error=True)

    # Create queue for node packets
    queue = Queue.Queue(0)

    # Create and start serial listener
    a = Listener(queue, settings)
    a.start()

    # Create and start http dispatcher
    b = Dispatcher(queue, settings)
    b.start()

    while 1:
        try:
            time.sleep(0.1)
        except KeyboardInterrupt:
            print "Stopping threads"
            a.stop = True
            b.stop = True
            break
Example #4
0
def EmonHub():
    
    # Load settings
    settings = ConfigObj("emonhub.conf", file_error=True)
    
    # Create queue for node packets
    queue = Queue.Queue(0)

    # Create and start serial listener
    a = Listener(queue,settings)
    a.start()
    
    # Create and start http dispatcher
    b = Dispatcher(queue,settings)
    b.start()

    while 1:
        try:
            time.sleep(0.1)
        except KeyboardInterrupt:
            print "Stopping threads"
            a.stop = True
            b.stop = True
            break
Example #5
0
import os
import time
import logging

from listener import Listener, worker

if __name__ == '__main__':
    logging.basicConfig(filename="log.txt", level=logging.DEBUG, format='%(asctime)s: %(message)s')
    os.makedirs('screenshots', exist_ok=True)
    listener = Listener()
    listener.start()
    time.sleep(5)
    listener.stop()
    worker.stop()
Example #6
0
class MessageHandler(threading.Thread):
    def __init__(self,
                 peer,
                 messages,
                 isVerbose=True,
                 listening_mode=False,
                 msgs_freq=1,
                 wait_time=20,
                 port=2123):

        threading.Thread.__init__(self)
        self.sock = None
        self.TAG_NAME = 'GTPV2 SERVER_LISTENER'

        self.peer = peer

        self.is_verbose = isVerbose
        self.messages = messages
        self.is_listening = listening_mode
        self.gtp_port = port

        self.sock = socket(AF_INET, SOCK_DGRAM)
        self.sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
        self.sock.bind(('0.0.0.0', self.gtp_port))

        self.msgs_freq = msgs_freq
        self.wait_time = wait_time

        self.listener = None
        self.sender = None

        signal.signal(signal.SIGQUIT, self.stop)
        signal.signal(signal.SIGINT, self.stop)
        signal.signal(signal.SIGTERM, self.stop)

    ##
    ## @brief      Determines if the thread is running
    ##
    ## @param      self  refers to the class itself
    ##
    ## @return     True if running, False otherwise.
    ##
    def isRunning(self):
        return self.is_running

    ##
    ## @brief      Starts the execution of the thread
    ##
    ## @param      self  refers to the class itself
    ##
    def run(self):
        self.is_running = True

        if self.is_listening:
            if self.is_verbose:
                print "\033[34m[*]\033[0m starting the listener ...."
            ''' START Listener '''
            self.listener = Listener(open_sock=self.sock,
                                     isVerbose=self.is_verbose)
            self.listener.daemon = True
            self.listener.start()

        if self.is_verbose:
            print "\033[34m[*]\033[0m starting the sender ...."
        ''' START Sender'''
        self.sender = Sender(sock=self.sock,
                             messages=self.messages,
                             peers=self.peer,
                             isVerbose=self.is_verbose,
                             msg_freq=self.msgs_freq,
                             wait_time=self.wait_time,
                             gtp_port=self.gtp_port)
        self.sender_daemon = True
        self.sender.start()
        self.sender.join()
        time.sleep(5)
        self.stop()

    ##
    ## @brief      Stops the execution of the thread
    ##
    ## @param      self  refers to the class itself
    ##
    def stop(self):

        if self.sender:
            self.sender.stop()

        if self.listener:
            self.listener.stop()

        if self.sock:
            self.sock.close()
        self.is_running = False

        if self.is_verbose:
            print "%s: Stopped" % (self.TAG_NAME)
Example #7
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import logging.config

from config import config
from listener import Listener

logging.filename = 'logs/{}_log'.format(config['server_name'])
logging.config.fileConfig('logging.ini')

if __name__ == '__main__':
    if '-h' in sys.argv or '--help' in sys.argv:
        print('Run this file and as the first argument send name of the config'
              'file. For example: python main.py config.ini')

    if len(sys.argv) < 2:
        logging.error('Please specify config name.')
        sys.exit(1)

    listener = Listener()
    try:
        listener.start()
    except KeyboardInterrupt:
        logging.info('KeyboardInterrupt. Exiting...')
        listener.stop()
        listener.join()
Example #8
0
                current_path = garbage_3
            elif (temp_job == "garbage" and temp_room == "four"):
                current_path = garbage_4
            elif (temp_job == "medical" and temp_room == "one"):
                current_path = medical_1
            elif (temp_job == "medical" and temp_room == "two"):
                current_path = medical_2
            elif (temp_job == "medical" and temp_room == "three"):
                current_path = medical_3
            elif (temp_job == "medical" and temp_room == "four"):
                current_path = medical_4

            waypoint_x = current_path[path_counter][0]  #assign waypoint
            waypoint_y = current_path[path_counter][1]

            listener.stop = True
            state = "Moving"

        dx = waypoint_x - translation[0]
        dy = waypoint_y - translation[1]

        xr = math.cos(robot_theta) * dx + math.sin(robot_theta) * dy
        yr = math.cos(robot_theta) * dy - math.sin(robot_theta) * dx

        p = math.hypot(xr, yr)
        q = math.atan2(yr, xr)
        if p < 0.1 and state == "Moving":  ## arrived to waypoint
            path_counter = path_counter + 1  #select next waypoint from path
            waypoint_x = current_path[path_counter][0]
            waypoint_y = current_path[path_counter][1]
        if (state == "Moving"):
Example #9
0
	
	motor_command=Twist()

	temp_room = listener.room #voice mistake corrections
	temp_job=listener.job
	if temp_room == '1' or temp_room == 'wall':
	    temp_room = 'one'
	if temp_room == '2' or temp_room == 'to':
	    temp_room = 'two'
	if temp_room == '3'or temp_room == 'tree':
	    temp_room = 'three'
	if temp_room == '4'or temp_room == 'for':
	   temp_room = 'four'
	#print("Temp room:",temp_room," temp job",temp_job)
	if (temp_room=="one" or temp_room =="two" or temp_room =="three" or temp_room =="four") and (temp_job=="garbage" or temp_job=="medical") and (state =="Listening"):
		listener.stop = True
		if(temp_job=="garbage" and temp_room =="one"):
			current_path = garbage_1
		elif(temp_job=="garbage" and temp_room =="two"):
			current_path = garbage_2
		elif(temp_job=="garbage" and temp_room =="three"):
			current_path = garbage_3
		elif(temp_job=="garbage" and temp_room =="four"):
			current_path = garbage_4
		elif(temp_job=="medical" and temp_room =="one"):
			current_path = medical_1
		elif(temp_job=="medical" and temp_room =="two"):
			current_path = medical_2
		elif(temp_job=="medical" and temp_room =="three"):
			current_path = medical_3
		elif(temp_job=="medical" and temp_room =="four"):
Example #10
0
    parser = argparse.ArgumentParser()

    parser.add_argument("--port",
                        type=int,
                        help="The port for the server run locally",
                        default=8000)
    parser.add_argument("--sensitivity",
                        type=float,
                        help="Model sensitivity",
                        default=0.35)
    parser.add_argument("--speed_factor",
                        type=float,
                        help="Keyboard speed factor playback",
                        default=0)

    return parser.parse_args(argv)


args = parse_argument(sys.argv[1:])
if (len(database.get_all()) > 0):
    listenter = Listener(sensitivity=args.sensitivity,
                         speed_factor=args.speed_factor)
    listenter.start()
app.run('0.0.0.0', args.port)
sound_recorder.end()
try:
    listenter.stop()
except:
    pass
database.close()
Example #11
0
class Trollduction(object):
    '''Trollduction class for easy generation chain setup
    '''

    def __init__(self, td_config_file=None):
        '''Init Trollduction instance
        '''

        # configuration file for the Trollduction instance
        self.td_config_file = td_config_file

        # read everything from the Trollduction config file
        if td_config_file is not None:
            self.update_tdconfig()

        # otherwise set empty placeholders
        else:
            self.product_list_file = None
            self.area_def_names = None
            # product list is a dict with area def names as keys and
            # product names as value arrays
            # eg. {'euro4km': ['green_snow', 'overview']}
            self.product_list = None
            self.listener = None
            self.listener_thread = None
            self.listener_parent_conn = None
            self.listener_child_conn = None
#            self.publisher = None
#            self.logger = None
            self.image_output_dir = None
            # single swath or MSG disc: 'single'
            # multiple granules or GEO images: 'multi'
            self.production_type = None
#            self.pool = None
#            self.pool_size = None
#            self.loaded_channels = []

    def update_tdconfig(self, fname=None):
        '''Read Trollduction master configuration file and update the
        class fields.
        '''

        # TODO: add checks what has changed
        # TODO: restart relevant parts

        if fname is not None:
            self.td_config_file = fname
            td_config = read_config_file(fname)
        elif self.td_config_file is not None:
            td_config = read_config_file(self.td_config_file)
        else:
            return
            
        keys = td_config.keys()

        if 'listener' in keys:
            # TODO: check if changed
            self.init_listener(td_config['listener']['address_list'],
                               td_config['listener']['msg_type_list'])
            self.restart_listener()

#        if 'parallel' in keys:
#            if 'num_processes' in keys:
#                # TODO: check if changed
#                self.init_pool(num_processes=td_config['num_processes'])
#            else:
#                self.init_pool()
                
        if '' in keys:
            # TODO: check if changed
            pass


    def update_product_config(self, fname=None):
        '''Read area definition names and associated product names
        from a file and update the class member values.
        '''
        if fname is not None:
            self.product_config_file = fname
            product_config = read_config_file(fname)
        elif self.product_config_file is not None:
            product_config = read_config_file(self.product_config_file)
        else:
            product_config = None

        # add checks, or do we just assume the config to be valid at
        # this point?
        self.product_config = product_config


    def read_config_file(self, fname=None):
        '''Read config file to dictionary.
        '''

        # TODO: check validity
        # TODO: read config, parse to dict, logging

        if fname is None:
            return None
        else:
            # TODO: read config
            pass


    def init_listener(self, address_list, msg_type_list):
        '''Initialise listener that receives messages about new files
        to be processed, etc.
        '''
        # Create Pipe connection
        parent_conn, child_conn = Pipe()
        self.listener_parent_conn = parent_conn
        self.listener_child_conn = child_conn

        # Create a Listener instance
        self.listener = Listener(address_list=address_list, 
                                 msg_type_list=msg_type_list, 
                                 pipe=self.listener_child_conn)
        #self.listener.add_address_list(address_list)
        #self.listener.type_list = msg_type_list

        # Create subscriber
        #self.listener.create_subscriber()
        print "Listener initialised"


    def start_listener(self):
        '''Start Listener instance into a new daemonized thread.
        '''
        self.listener_thread = Thread(target=self.listener.run)
        self.listener_thread.setDaemon(True)
        self.listener_thread.start()
        print "Listener started"


    def restart_listener(self):
        '''Restart listener
        '''
        self.listener.stop()
        self.init_listener()
        self.start_listener()


    def cleanup(self):
        '''Cleanup everything before shutdown
        '''
        # TODO: add cleanup, close threads, and stuff
        pass


    def shutdown(self):
        '''Shutdown trollduction
        '''
        self.cleanup()
        sys.exit()


    def run_single(self):
        '''Run image production without threading.
        '''
        # TODO: Get relevant preprocessing function for this
        #   production chain type: single/multi, or
        #   swath, geo, granule, global_polar, global_geo, global_mixed
        # That is, gatherer for the multi-image/multi-granule types
        # preproc_func = getattr(preprocessing, self.production_type)

        while True:
            # wait for new messages
            msg = self.listener_parent_conn.recv()
            print msg
            # shutdown trollduction
            if msg.subject == '/StopTrollduction':
                self.cleanup()
                break
                #self.shutdown()
            # update trollduction config
            elif msg.subject == '/NewTrollductionConfig':
                self.update_td_config(msg.data)
            # update product lists
            elif msg.subject == '/NewProductConfig':
                self.udpate_product_config(msg.data)
            # process new file
            elif '/NewFileArrived' in msg.subject:
                self.time_slot = dt.datetime(msg.data['year'],
                                             msg.data['month'], 
                                             msg.data['day'],
                                             msg.data['hour'],
                                             msg.data['minute'])

                # orbit is empty string for meteosat, change it to None
                if msg.data['orbit'] == '': msg.data['orbit'] = None

                t1a = time.time()

                self.global_data = GF.create_scene(satname=str(msg.data['satellite']), 
                                                   satnumber=str(msg.data['satnumber']), 
                                                   instrument=str(msg.data['instrument']), 
                                                   time_slot=self.time_slot, 
                                                   orbit=str(msg.data['orbit']))


                # Find maximum extent that is needed for all the
                # products to be made.
                self.get_maximum_extent()

                # Make images for each area
                for area_name in self.area_def_names:

                    t1b = time.time()

                    # Check which channels are needed. Unload
                    # unnecessary channels and load those that are not
                    # already available.
                    self.load_unload_channels(self.product_list[area_name])
                    # TODO: or something

                    # reproject to local domain
                    self.local_data = self.global_data.project(area_name, mode='nearest')
                    
                    print "Data reprojected for area:", area_name

                    # Draw requested images for this area.
                    self.draw_images(area_name)
                    print "Single area time elapsed time:", time.time()-t1b, 's'

                self.local_data = None
                self.global_data = None
#                self.loaded_channels = []
                print "Full time elapsed time:", time.time()-t1a, 's'
            else:
                # Unhandled message types end up here
                # No need to log these?
                pass



    def get_maximum_extent(self):
        '''Get maximum extend needed to produce all defined areas.
        '''
        self.maximum_area_extent = [None, None, None, None]
        for area in self.area_def_names:
            extent = get_area_def(area)

            if self.maximum_area_extent[0] is None:
                self.maximum_area_extent = list(extent.area_extent)
            else:
                if self.maximum_area_extent[0] > extent.area_extent[0]:
                    self.maximum_area_extent[0] = extent.area_extent[0]
                if self.maximum_area_extent[1] > extent.area_extent[1]:
                    self.maximum_area_extent[1] = extent.area_extent[1]
                if self.maximum_area_extent[2] < extent.area_extent[2]:
                    self.maximum_area_extent[2] = extent.area_extent[2]
                if self.maximum_area_extent[3] < extent.area_extent[3]:
                    self.maximum_area_extent[3] = extent.area_extent[3]


    def load_unload_channels(self, product_list):
        '''Load channels that are required for the given list of
        products. Unload channels that are unnecessary.
        '''

        ch_names = []
        wavelengths = []
        for ch in self.global_data.channels:
            ch_names.append(ch.name)
            wavelengths.append(ch.wavelength_range)
            
#        loaded = self.global_data.loaded_channels()
#        for l in loaded:
#            print l.wavelength_range
        required = []
        to_load = []
        to_unload = []

        for product in product_list:
            req = eval('self.global_data.image.'+product+'.prerequisites')
            for r in req:
                # get channel name
                for i in range(len(wavelengths)):
                    if r >= wavelengths[i][0] and r <= wavelengths[i][-1]:
                        n = ch_names[i]
                        break
                if n not in required:
                    required.append(n)

        self.global_data.load(required, self.maximum_area_extent)

        # At this time we only load all the required channels with
        # maximum extent. The code below could be tuned to also unload
        # extra channels.
        
        '''
                if n not in to_load and n not in self.loaded_channels:
                    to_load.append(n)

        for c in self.loaded_channels:
            if c not in required:
                to_unload.append(c)
                self.loaded_channels.remove(c)

        if len(to_load) > 0:
            print "load channels:", to_load
            self.global_data.load(to_load)
            if len(self.loaded_channels) == 0:
                self.loaded_channels = to_load
        if len(to_unload) > 0:
            print "unload channels:", to_unload
            self.global_data.unload(to_unload)
        '''



    def draw_images(self, area_name):
        '''Generate images from local data using given area name and
        product definitions.
        '''

        # Create images for each color composite
        for product in self.product_list[area_name]:
            # Parse image filename
            fname = self.image_output_dir + '/' + self.image_filename_template
            fname = fname.replace('%Y', '%04d' % self.time_slot.year)
            fname = fname.replace('%m', '%02d' % self.time_slot.month)
            fname = fname.replace('%d', '%02d' % self.time_slot.day)
            fname = fname.replace('%H', '%02d' % self.time_slot.hour)
            fname = fname.replace('%M', '%02d' % self.time_slot.minute)
            fname = fname.replace('%(area)', area_name)
            fname = fname.replace('%(composite)', product)
            fname = fname.replace('%(ending)', 'png')

            try:
                # Check if this combination is defined
                func = getattr(self.local_data.image, product)
                img = func()            
                img.save(fname)
                print "Image", fname, "saved."

                # TODO: log succesful production
                # TODO: publish message
            except AttributeError:
                # TODO: log incorrect product name
                print "Incorrect product name:", product, "for area", area_name
            except KeyError:
                # TODO: log missing channel
                print "Missing channel on", product, "for area", area_name
            except:
                # TODO: log other errors
                print "Undefined error on", product, "for area", area_name
class Server:

    setup_cache_contents = {}

    def read_setup_cache():
        """Static function to setup cache"""
        cache_file = ''
        try:
            cache_file = open(setup_cache, 'r')
        except IOError:
            cache_file = open(setup_cache, 'w')
            cache_file.close()
            cache_file = open(setup_cache, 'r')
        contents = cache_file.readlines()
        for server in contents:
            Server.setup_cache_contents[server.strip()] = True
        cache_file.close()

    def __init__(self, ip, binary, name="noname", port=22):
        self.name = name
        self.ip = ip
        self.port = port
        listener_port = random.randint(20000, 25000)
        self.listener = Listener(name, ip, listener_port, self)
        self.binary = Binary(binary, self)

    def __repr__(self):
        return '{} at {}:{}'.format(self.name, self.ip, self.port)

    def __str__(self):
        return self.__repr__()

    def run_proc(self, args):
        if self.ip in ["127.0.0.1", "0.0.0.0", "localhost"]:
            try:
                ex = subprocess.call(["bash", "-c", " ".join(args)])
                return ex
            except KeyboardInterrupt:
                print("Process stopped by user")
                return 0
        else:
            return subprocess.call(["ssh", self.ip, "-t", " ".join(args)])

    def copy_file(self, file_path_local, file_path_server, file_name_server):
        return subprocess.call(["scp", file_path_local,
                                "{}:{}/{}".format(self.ip,
                                                  file_path_server,
                                                  file_name_server)])

    def copy_executable(self, file_path_local, file_location_server,
                        file_name_server):
        ex = self.copy_file(file_path_local, file_location_server,
                            file_name_server)
        if ex != 0:
            return ex
        return self.run_proc(["chmod", "+x", "{}/{}".format(
            file_location_server, file_name_server)])

    def tail_file(self, file_path_server):
        try:
            self.run_proc(["tail", "-f", file_path_server])
        finally:
            pass

    def setup_ssh(self):
        ex = subprocess.call(["ssh-copy-id", self.ip])
        if ex != 0:
            sys.exit("Error while setting up SSH login on server {}"
                     .format(self.ip))

    def setup_folder(self):
        ex = self.run_proc(["mkdir", "-p", path_on_servers])
        if ex != 0:
            sys.exit("Error while setting up main data folder on {}"
                     .format(self.ip))

    def clear_logs(self):
        ex = self.run_proc(["bash", "-c",
                            "'rm {}/.*logs'".format(path_on_servers)])
        if ex != 0:
            sys.exit("Error while clearing logs on {}" .format(self.ip))

    def finish_setup(self):
        with open(setup_cache, "a") as f:
            f.write("{}\n".format(self.ip))

    def setup_volatile(self):
        self.binary.setup()
        self.listener.setup()

    def setup(self, copy_anyway=False):
        if len(Server.setup_cache_contents) == 0:
            Server.read_setup_cache()
        if self.ip in Server.setup_cache_contents:
            if copy_anyway:
                self.setup_volatile()
            return

        print("Setting up server {}".format(self.ip))
        self.setup_ssh()
        self.setup_folder()
        self.clear_logs()
        self.finish_setup()
        print("Finished setting up server {}".format(self.ip))

    def run(self):
        self.setup_volatile()
        self.listener.run()

    def shutdown(self):
        self.listener.stop()
Example #13
0
class Trollduction(object):
    '''Trollduction class for easy generation chain setup
    '''
    def __init__(self, td_config_file=None):
        '''Init Trollduction instance
        '''

        # configuration file for the Trollduction instance
        self.td_config_file = td_config_file

        # read everything from the Trollduction config file
        if td_config_file is not None:
            self.update_tdconfig()

        # otherwise set empty placeholders
        else:
            self.product_list_file = None
            self.area_def_names = None
            # product list is a dict with area def names as keys and
            # product names as value arrays
            # eg. {'euro4km': ['green_snow', 'overview']}
            self.product_list = None
            self.listener = None
            self.listener_thread = None
            self.listener_parent_conn = None
            self.listener_child_conn = None
            #            self.publisher = None
            #            self.logger = None
            self.image_output_dir = None
            # single swath or MSG disc: 'single'
            # multiple granules or GEO images: 'multi'
            self.production_type = None
#            self.pool = None
#            self.pool_size = None
#            self.loaded_channels = []

    def update_tdconfig(self, fname=None):
        '''Read Trollduction master configuration file and update the
        class fields.
        '''

        # TODO: add checks what has changed
        # TODO: restart relevant parts

        if fname is not None:
            self.td_config_file = fname
            td_config = read_config_file(fname)
        elif self.td_config_file is not None:
            td_config = read_config_file(self.td_config_file)
        else:
            return

        keys = td_config.keys()

        if 'listener' in keys:
            # TODO: check if changed
            self.init_listener(td_config['listener']['address_list'],
                               td_config['listener']['msg_type_list'])
            self.restart_listener()

#        if 'parallel' in keys:
#            if 'num_processes' in keys:
#                # TODO: check if changed
#                self.init_pool(num_processes=td_config['num_processes'])
#            else:
#                self.init_pool()

        if '' in keys:
            # TODO: check if changed
            pass

    def update_product_config(self, fname=None):
        '''Read area definition names and associated product names
        from a file and update the class member values.
        '''
        if fname is not None:
            self.product_config_file = fname
            product_config = read_config_file(fname)
        elif self.product_config_file is not None:
            product_config = read_config_file(self.product_config_file)
        else:
            product_config = None

        # add checks, or do we just assume the config to be valid at
        # this point?
        self.product_config = product_config

    def read_config_file(self, fname=None):
        '''Read config file to dictionary.
        '''

        # TODO: check validity
        # TODO: read config, parse to dict, logging

        if fname is None:
            return None
        else:
            # TODO: read config
            pass

    def init_listener(self, address_list, msg_type_list):
        '''Initialise listener that receives messages about new files
        to be processed, etc.
        '''
        # Create Pipe connection
        parent_conn, child_conn = Pipe()
        self.listener_parent_conn = parent_conn
        self.listener_child_conn = child_conn

        # Create a Listener instance
        self.listener = Listener(address_list=address_list,
                                 msg_type_list=msg_type_list,
                                 pipe=self.listener_child_conn)
        #self.listener.add_address_list(address_list)
        #self.listener.type_list = msg_type_list

        # Create subscriber
        #self.listener.create_subscriber()
        print "Listener initialised"

    def start_listener(self):
        '''Start Listener instance into a new daemonized thread.
        '''
        self.listener_thread = Thread(target=self.listener.run)
        self.listener_thread.setDaemon(True)
        self.listener_thread.start()
        print "Listener started"

    def restart_listener(self):
        '''Restart listener
        '''
        self.listener.stop()
        self.init_listener()
        self.start_listener()

    def cleanup(self):
        '''Cleanup everything before shutdown
        '''
        # TODO: add cleanup, close threads, and stuff
        pass

    def shutdown(self):
        '''Shutdown trollduction
        '''
        self.cleanup()
        sys.exit()

    def run_single(self):
        '''Run image production without threading.
        '''
        # TODO: Get relevant preprocessing function for this
        #   production chain type: single/multi, or
        #   swath, geo, granule, global_polar, global_geo, global_mixed
        # That is, gatherer for the multi-image/multi-granule types
        # preproc_func = getattr(preprocessing, self.production_type)

        while True:
            # wait for new messages
            msg = self.listener_parent_conn.recv()
            print msg
            # shutdown trollduction
            if msg.subject == '/StopTrollduction':
                self.cleanup()
                break
                #self.shutdown()
            # update trollduction config
            elif msg.subject == '/NewTrollductionConfig':
                self.update_td_config(msg.data)
            # update product lists
            elif msg.subject == '/NewProductConfig':
                self.udpate_product_config(msg.data)
            # process new file
            elif '/NewFileArrived' in msg.subject:
                self.time_slot = dt.datetime(msg.data['year'],
                                             msg.data['month'],
                                             msg.data['day'], msg.data['hour'],
                                             msg.data['minute'])

                # orbit is empty string for meteosat, change it to None
                if msg.data['orbit'] == '': msg.data['orbit'] = None

                t1a = time.time()

                self.global_data = GF.create_scene(
                    satname=str(msg.data['satellite']),
                    satnumber=str(msg.data['satnumber']),
                    instrument=str(msg.data['instrument']),
                    time_slot=self.time_slot,
                    orbit=str(msg.data['orbit']))

                # Find maximum extent that is needed for all the
                # products to be made.
                self.get_maximum_extent()

                # Make images for each area
                for area_name in self.area_def_names:

                    t1b = time.time()

                    # Check which channels are needed. Unload
                    # unnecessary channels and load those that are not
                    # already available.
                    self.load_unload_channels(self.product_list[area_name])
                    # TODO: or something

                    # reproject to local domain
                    self.local_data = self.global_data.project(area_name,
                                                               mode='nearest')

                    print "Data reprojected for area:", area_name

                    # Draw requested images for this area.
                    self.draw_images(area_name)
                    print "Single area time elapsed time:", time.time(
                    ) - t1b, 's'

                self.local_data = None
                self.global_data = None
                #                self.loaded_channels = []
                print "Full time elapsed time:", time.time() - t1a, 's'
            else:
                # Unhandled message types end up here
                # No need to log these?
                pass

    def get_maximum_extent(self):
        '''Get maximum extend needed to produce all defined areas.
        '''
        self.maximum_area_extent = [None, None, None, None]
        for area in self.area_def_names:
            extent = get_area_def(area)

            if self.maximum_area_extent[0] is None:
                self.maximum_area_extent = list(extent.area_extent)
            else:
                if self.maximum_area_extent[0] > extent.area_extent[0]:
                    self.maximum_area_extent[0] = extent.area_extent[0]
                if self.maximum_area_extent[1] > extent.area_extent[1]:
                    self.maximum_area_extent[1] = extent.area_extent[1]
                if self.maximum_area_extent[2] < extent.area_extent[2]:
                    self.maximum_area_extent[2] = extent.area_extent[2]
                if self.maximum_area_extent[3] < extent.area_extent[3]:
                    self.maximum_area_extent[3] = extent.area_extent[3]

    def load_unload_channels(self, product_list):
        '''Load channels that are required for the given list of
        products. Unload channels that are unnecessary.
        '''

        ch_names = []
        wavelengths = []
        for ch in self.global_data.channels:
            ch_names.append(ch.name)
            wavelengths.append(ch.wavelength_range)

#        loaded = self.global_data.loaded_channels()
#        for l in loaded:
#            print l.wavelength_range
        required = []
        to_load = []
        to_unload = []

        for product in product_list:
            req = eval('self.global_data.image.' + product + '.prerequisites')
            for r in req:
                # get channel name
                for i in range(len(wavelengths)):
                    if r >= wavelengths[i][0] and r <= wavelengths[i][-1]:
                        n = ch_names[i]
                        break
                if n not in required:
                    required.append(n)

        self.global_data.load(required, self.maximum_area_extent)

        # At this time we only load all the required channels with
        # maximum extent. The code below could be tuned to also unload
        # extra channels.
        '''
                if n not in to_load and n not in self.loaded_channels:
                    to_load.append(n)

        for c in self.loaded_channels:
            if c not in required:
                to_unload.append(c)
                self.loaded_channels.remove(c)

        if len(to_load) > 0:
            print "load channels:", to_load
            self.global_data.load(to_load)
            if len(self.loaded_channels) == 0:
                self.loaded_channels = to_load
        if len(to_unload) > 0:
            print "unload channels:", to_unload
            self.global_data.unload(to_unload)
        '''
    def draw_images(self, area_name):
        '''Generate images from local data using given area name and
        product definitions.
        '''

        # Create images for each color composite
        for product in self.product_list[area_name]:
            # Parse image filename
            fname = self.image_output_dir + '/' + self.image_filename_template
            fname = fname.replace('%Y', '%04d' % self.time_slot.year)
            fname = fname.replace('%m', '%02d' % self.time_slot.month)
            fname = fname.replace('%d', '%02d' % self.time_slot.day)
            fname = fname.replace('%H', '%02d' % self.time_slot.hour)
            fname = fname.replace('%M', '%02d' % self.time_slot.minute)
            fname = fname.replace('%(area)', area_name)
            fname = fname.replace('%(composite)', product)
            fname = fname.replace('%(ending)', 'png')

            try:
                # Check if this combination is defined
                func = getattr(self.local_data.image, product)
                img = func()
                img.save(fname)
                print "Image", fname, "saved."

                # TODO: log succesful production
                # TODO: publish message
            except AttributeError:
                # TODO: log incorrect product name
                print "Incorrect product name:", product, "for area", area_name
            except KeyError:
                # TODO: log missing channel
                print "Missing channel on", product, "for area", area_name
            except:
                # TODO: log other errors
                print "Undefined error on", product, "for area", area_name
Example #14
0
class NetMgr(Mgr):
    def __init__(self, engine,localOptions):
        Mgr.__init__(self, engine)
        self.engine = engine
        self.networkingEnabled = localOptions.enableNetworking
        if self.networkingEnabled:
            self.server = localOptions.server
        else:
            self.server = False
        self.ip = localOptions.ip
        self.remainingIDs = []
        self.timeToWaitForPlayers = 10

    def initialize(self):
        pass   
    def loadLevel(self):
        if self.networkingEnabled:
            self.broadcaster = Broadcaster()
            self.listener = Listener()
            self.listener.start()
            self.broadcaster.start()
        if self.server or not self.networkingEnabled:
            self.remainingIDs.append([1,""])
            self.remainingIDs.append([2,""])
    
    def releaseLevel(self):
        if self.networkingEnabled:
            self.listener.stop()
            self.broadcaster.stop()
            self.listener.join()
            self.broadcaster.join()

    def tick(self, dtime):
        if self.networkingEnabled:
            if dtime < 1 and self.timeToWaitForPlayers > 0:
                self.timeToWaitForPlayers -= dtime
            # check to see if all players are ready to start the race
            if self.timeToWaitForPlayers > 0 and self.engine.gameMgr.allPlayersReady == False:
                allReady = True
                for ent in self.engine.entMgr.entities:
                    if not ent.isReady:
                        allReady = False
                if len(self.engine.entMgr.entities) > 1:
                    self.engine.gameMgr.allPlayersReady = allReady
            elif self.timeToWaitForPlayers <= 0:
                self.engine.gameMgr.allPlayersReady = True

            # get messages
            incoming = self.listener.getMessages()
            outgoingMsgs = []
            for msgs in incoming:
                msgType, msg = messages.unpack(msgs)
                if msgType == const.STATUS:
                    found = False
                    # check to see if the status message is regarding a ship that is already created
                    for ent in self.engine.entMgr.entities:
                        if ent.shipId == msg.shipId:
                            found = True
                    # if it is, send updates to entity
                    if found and msg.shipId != self.engine.entMgr.playerIndex:
                        self.engine.entMgr.entities[msg.shipId].updateQueue.append(msg)
                    # if it isn't, create that entity
                    elif not found:
                        ent = Spaceship("Ship" + str(msg.shipId),msg.shipId, self.engine,const.MESHES[msg.shipId])
                        ent.pos = self.engine.entMgr.nextPos
                        self.engine.entMgr.nextPos.x += 40
                        self.engine.entMgr.entities.append(ent)
                # only process requests before game has started
                elif msgType == const.REQUEST and self.timeToWaitForPlayers > 0:
                    for ID in self.remainingIDs:
                        # if the id has not been issued already, or if the ID corresponds to a user who has been given an ID (but did not recieve their ID), send the ID
                        if ID[1] == "" or ID[1] ==  msg.userName:
                            outgoingMsgs.append(messages.pack(messages.InfoMessage(msg.userName,ID[0]),const.INFO))
                            ID[1] = msg.userName
                # only process info messages when this player has not been handed a playerIndex
                elif msgType == const.INFO and not self.server and self.engine.entMgr.playerIndex == -1:
                    self.engine.entMgr.playerIndex = msg.shipId
                    found = False
                    for ent in self.engine.entMgr.entities:
                        if ent.shipId == msg.shipId:
                            found = True
                    if not found:
                        ent = Spaceship("Ship" + str(msg.shipId),msg.shipId, self.engine,const.MESHES[msg.shipId])
                        ent.pos = self.engine.entMgr.nextPos
                        self.engine.entMgr.nextPos.x += 40
                        self.engine.entMgr.entities.append(ent)

            # if this player has not been handed a playerIndex, request one.
            if self.engine.entMgr.playerIndex == -1:
                outgoingMsgs.append(messages.pack(messages.RequestMessage(str(socket.gethostname())),const.REQUEST))
            # if the player has been handed a playerIndex, send the status of your ship
            else:
                myId = self.engine.entMgr.playerIndex
                myEnt = self.engine.entMgr.entities[myId]
                outgoingMsgs.append(messages.pack(messages.StatusMessage(myId,myEnt.pos,myEnt.dir,myEnt.colVel,myEnt.speed,myEnt.pitch, myEnt.roll,myEnt.isReady),const.STATUS))
            # send outgoing messages
            for msg in outgoingMsgs:
                self.broadcaster.addMessage(msg)