Beispiel #1
0
 def __init__(self):
     super(RheaRouteReceiver, self).__init__()
     self.name = 'RheaRouteReceiver'
     self.jsonprocessor = JsonHandler()
     self.server_host = os.environ.get('RHEA_ROUTE_RECEIVER', '0.0.0.0')
     self.server_port = int(
         os.environ.get('RHEA_ROUTE_RECEIVER_PORT', 55650))
     self._stop = False
 def __init__(self, gui):
     ''' Handles relationships between options from file and screen/panels. '''
     self.screens = []
     self.max_width = 0
     self.max_height = 0
     self.n_panels = 0
     self.gui = gui
     self.currentPanelShareID = 0
     # options
     self.defaultConfigFile = 'defaultConfig.json'
     self.options = JsonHandler().importFile(self.defaultConfigFile)
     self.convertOptionsToStrings()
 def load(self):
     ''' opens a file dialog and loads it into the model '''
     fname = tk.filedialog.askopenfile(filetypes=(("json files", "*.json"),
                                                  ("all files", "*.*")))
     if fname != None:
         self.options = JsonHandler().importFile(fname.name)
         self.gui.toggleOptionWindow(state='hide')  # hide the window
         self.setupScreensFromFile()
         self.convertOptionsToStrings()
         self.gui.setupOptionsWindow(
         )  # remake the window, easiest thing to do
         self.gui.mainWindow.draw()
     else:
         print('file name was found to be null')
 def save(self):
     ''' opens a file dialog and saves the model to a file '''
     fname = tk.filedialog.asksaveasfilename(filetypes=(("json files",
                                                         "*.json"),
                                                        ("all files",
                                                         "*.*")))
     if fname != '':
         if not fname.endswith('.json'): fname += '.json'
         JsonHandler().exportFile(self, fname)
     else:
         print('file needs a name to save')
Beispiel #5
0
    def __init__(self):
        ''' creates a window handler for Tkinter windows '''
        self.model = Model(self)
        self.jsonHandler = JsonHandler()

        # setup main window
        self.setupMainWindow()

        # setup options window
        # child of main window, will close when main does
        # can be hidden/closed without affecting main window
        self.setupOptionsWindow()

        # setup initilization settings window
        # child of main window, will close when main does
        # can be hidden/closed without affecting main window
        self.setupInitSettingsWindow()
Beispiel #6
0
class RheaRouteReceiver(app_manager.RyuApp):

    _EVENTS = [
        EventRouterConnect, EventRouterDisconnect, EventRouteReceived,
        EventRouteDeleted
    ]

    def __init__(self):
        super(RheaRouteReceiver, self).__init__()
        self.name = 'RheaRouteReceiver'
        self.jsonprocessor = JsonHandler()
        self.server_host = os.environ.get('RHEA_ROUTE_RECEIVER', '0.0.0.0')
        self.server_port = int(
            os.environ.get('RHEA_ROUTE_RECEIVER_PORT', 55650))
        self._stop = False

    def start(self):
        super(RheaRouteReceiver, self).start()
        self.logger.debug("listening on %s:%s", self.server_host,
                          self.server_port)

        return hub.spawn(
            StreamServer((self.server_host, self.server_port),
                         self.loop).serve_forever)

    def loop(self, sock, addr):
        log.error("A router is connected, ip=%s, port=%s", addr[0], addr[1])
        self.send_event_to_observers(EventRouterConnect(addr))
        if self._stop is False:
            is_active = True
        else:
            is_active = False

        while is_active:
            received = sock.recv(256)
            if len(received) == 0:
                is_active = False
                break
            msg = self.jsonprocessor.strip_message(received)
            check = self.jsonprocessor.verify_json(msg)

            if check is True:
                action, route = self.jsonprocessor.determine_action(msg)
                key_check, value_check = self.jsonprocessor.check_routeinfo(
                    route)
                if action == "added":
                    if key_check is True and value_check is True:
                        route_list = self.jsonprocessor.hand_over_route(route)
                        self.send_event_to_observers(
                            EventRouteReceived(route_list))
                        try:
                            lens = sock.send('Route processed and flows added')
                        except socket.error as e:
                            log.error("Error number is %d", e.errno)
                            break

                    else:
                        try:
                            sock.send('Route information not complete')
                            log.warning(
                                "Incomplete route information received, Route not processed"
                            )
                        except socket.error as e:
                            log.error("Error number is %d", e.errno)
                            break
                elif action == "removed":
                    if key_check is True and value_check is True:
                        route_list = self.jsonprocessor.hand_over_route(route)
                        self.send_event_to_observers(
                            EventRouteDeleted(route_list))
                        try:
                            lens = sock.send('Route removed and flows removed')
                        except socket.error as e:
                            log.error("Error number is %d", e.errno)
                            break
                    else:
                        try:
                            sock.send('Route information not complete')
                            log.warning(
                                "Incomplete route information received, Route not processed"
                            )
                        except socket.error as e:
                            log.error("Error number is %d", e.errno)
                            break
                else:
                    try:
                        sock.send('Unknown action')
                        log.warning(
                            "Recieved unknown action from router not processing route"
                        )
                    except socket.error as e:
                        log.error("Error number is %d", e.errno)
                        break
            else:
                fixed_route = self.jsonprocessor.fix_route(msg)
                log.debug('FIXED_ROUTE is %s', fixed_route)
                if fixed_route is None:
                    try:
                        sock.send("Error, Can not parse route")
                        log.error('Error, unknown object "%s" received',
                                  fixed_route)
                    except socket.error as e:
                        log.error("Error number is %d", e.errno)
                        break
                else:
                    check = self.jsonprocessor.verify_json(fixed_route)
                    if check is True:
                        action, route = self.jsonprocessor.determine_action(
                            fixed_route)
                        key_check, value_check = (
                            self.jsonprocessor.check_routeinfo(route))
                        if action == "added":
                            if key_check is True and value_check is True:
                                route_list = (
                                    self.jsonprocessor.hand_over_route(route))
                                self.send_event_to_observers(
                                    EventRouteReceived(route_list))
                                try:
                                    sock.send("Route received and parsed")
                                except socket.error as e:
                                    log.error("Error number is %d", e.errno)
                                    break
                            else:
                                try:
                                    sock.send("Route information not complete")
                                    log.warning(
                                        "Incomplete route information received, Route not processed"
                                    )
                                except socket.error as e:
                                    log.error("Error number is %d", e.errno)
                                    break
                        elif action == "removed":
                            if key_check is True and value_check is True:
                                route_list = (
                                    self.jsonprocessor.hand_over_route(route))
                                self.send_event_to_observers(
                                    EventRouteDeleted(route_list))
                                try:
                                    sock.send(
                                        "Route removed and flows removed")
                                except socket.error as e:
                                    log.error("Error number is %d", e.errno)
                                    break
                            else:
                                try:
                                    sock.send("Route information not complete")
                                    log.warning(
                                        "Incomplete route information recieved, Route not processed"
                                    )
                                except socket.error as e:
                                    log.error("Error number is %d", e.errno)
                                    break
                        else:
                            try:
                                sock.send('Unknown action')
                                log.warning(
                                    "Recieved unknown action from router,not processing route"
                                )
                            except socket.error as e:
                                log.error("Error number is %d", e.errno)
                                break
                    else:
                        try:
                            sock.send(
                                "Error !!! Unknown object received, cannot parse object"
                            )
                            log.error('Error, unknown object "%s" received',
                                      fixed_route)
                        except socket.error as e:
                            log.error("Error number is %d", e.errno)
                            break

        sock.shutdown(socket.SHUT_RDWR)
        self.send_event_to_observers(EventRouterDisconnect(addr))
        log.error("Router disconnected, ip=%s, port=%s", addr[0], addr[1])

    def shutdown(self):
        self._stop = True
        self.stop
class Model:
    def __init__(self, gui):
        ''' Handles relationships between options from file and screen/panels. '''
        self.screens = []
        self.max_width = 0
        self.max_height = 0
        self.n_panels = 0
        self.gui = gui
        self.currentPanelShareID = 0
        # options
        self.defaultConfigFile = 'defaultConfig.json'
        self.options = JsonHandler().importFile(self.defaultConfigFile)
        self.convertOptionsToStrings()

    def load(self):
        ''' opens a file dialog and loads it into the model '''
        fname = tk.filedialog.askopenfile(filetypes=(("json files", "*.json"),
                                                     ("all files", "*.*")))
        if fname != None:
            self.options = JsonHandler().importFile(fname.name)
            self.gui.toggleOptionWindow(state='hide')  # hide the window
            self.setupScreensFromFile()
            self.convertOptionsToStrings()
            self.gui.setupOptionsWindow(
            )  # remake the window, easiest thing to do
            self.gui.mainWindow.draw()
        else:
            print('file name was found to be null')

    def save(self):
        ''' opens a file dialog and saves the model to a file '''
        fname = tk.filedialog.asksaveasfilename(filetypes=(("json files",
                                                            "*.json"),
                                                           ("all files",
                                                            "*.*")))
        if fname != '':
            if not fname.endswith('.json'): fname += '.json'
            JsonHandler().exportFile(self, fname)
        else:
            print('file needs a name to save')

    def getNextPanelShareID(self):
        ''' if panels are shared between nodes they need an id so they know who to share with,
    call this to get a new share id if the panel doesn't have one and the shared panels also don't have one '''
        self.currentPanelShareID += 1
        return self.currentPanelShareID

    def updateScreenSize(self, width, height):
        ''' sets all of the screens in the model to have a new size '''
        if len(self.screens) < 1:
            return

        x = 0
        y = 0
        # w = int( self.gui.mainWindow.canvasW / ( self.numNodes*self.numScreenColumns ) )
        w = int(width / len(self.screens))
        h = int(w / (self.aspectRatioScreensA / self.aspectRatioScreensB))

        if h * self.numScreenRows > height:
            overflow = (h * self.numScreenRows) - height
            h -= int(overflow / self.numScreenRows)
            w = h * ((self.aspectRatioScreensA / self.aspectRatioScreensB))

        for screen in self.screens:
            screen.x = x
            screen.y = y
            screen.width = w
            screen.height = h
            x += w

    def setupScreensFromFile(self):
        try:
            if self.options['screens'] != None:
                data = self.options['screens']
                n_per_slave = int(self.options['n_per_slave'])
                n_rows = int(self.options['n_rows'])
                n_cols = int(self.options['n_cols'])
                numNodes = len(data)
                self.screens = []
                self.createScreens(numNodes, n_rows, n_cols)
                for nKey, nValue in data.items():
                    # print( 'nKey: ' + str(nKey) + ', nValue: ' + str(nValue) ) # for each screen in the file
                    # print( 'nKey: ' + str(nKey) )
                    n = nKey.split('n')[1]
                    screen = self.screens[int(n)]  # current screen
                    screen.panels = []
                    for pKey, pValue in nValue.items():
                        # print( 'pKey: ' + str(pKey) )
                        # print('x ' + str( pValue['dimensions'][0] ) + ' y ' + str( pValue['dimensions'][1] ) + ' w ' + str( pValue['dimensions'][2] ) + ' h ' + str( pValue['dimensions'][3] ))
                        dim = pValue['dimensions']
                        x1 = dim[0]
                        y1 = dim[3]
                        x2 = dim[2]
                        y2 = dim[1]
                        flippedY1 = abs(y1 - 1) * 1
                        flippedY2 = abs(y2 - 1) * 1
                        # print( 'dim ' + str(dim)  )
                        # print( 'x1 ' + str(x1) + ", y1 " + str(y1) + ', x2 ' + str(x2) + ', y2 ' + str(y2)  )

                        x = x1
                        y = flippedY1
                        w = x2 - x1
                        h = abs(flippedY2 - flippedY1)

                        # print('x ' + str(x) + ' y ' + str(y) + ' w ' + str(w) + ' h ' + str(h))

                        p = Panel(screen=screen,
                                  canvas=screen.canvas,
                                  ident="0",
                                  method='c',
                                  x=x,
                                  y=y,
                                  width=w,
                                  height=h,
                                  mode=pValue['type'])
                        screen.panels.append(p)
        except Exception as e:
            print(e)
            # pass

    def convertOptionsToStrings(self):
        ''' convert all options to a string (clean) so they can be put into a text area for user to edit '''
        for key, value in self.options.items():
            if isinstance(value, str):
                pass
            elif isinstance(value, list):
                self.options[key] = str(value)
            elif isinstance(value, dict):
                pass
            elif isinstance(value, bool):
                if value:
                    self.options[key] = 'true'
                else:
                    self.options[key] = 'false'
            elif isinstance(value, int):
                pass
                # self.options[key] = '"' + str(value) + '"'
            elif isinstance(value, float):
                pass
                # self.options[key] = '"' + str(value) + '"'
            elif isinstance(value, complex):
                pass
                # self.options[key] = '"' + str(value) + '"'

    def screensToArray(self):
        ''' returns screen data as a dictionary '''
        screens = dict()
        for (index, screen) in enumerate(self.screens):
            screens['n' + str(index)] = screen.toDimensionArray()
        return screens

    def addOption(self, key, value):
        ''' adds key and value to options '''
        self.options.update({key: value})

    def updateOption(self, uKey, uValue):
        ''' finds matching key and sets new value in options '''
        # print('update called')
        for key, value in self.options.items():
            if key == uKey:
                self.options[key] = uValue
                # print( str(key) + ' == ' + str(uKey) )
                # print( 'updating to ' + str(uValue) )
                # value = uValue

    def removeOption(self, key):
        ''' finds matching key and removes it from options '''
        r = dict(self.options)
        del r[key]
        self.options = r

    def inOptions(self, item):
        ''' finds if given key is contained in options '''
        for key, value in self.options.items():
            if key == item:
                return True
        return False

    def printOptions(self):
        ''' prints all options to console '''
        print('---------------------')
        print('All Options:')
        for k, v in self.options.items():
            print('K: ' + str(k) + ', v: ' + str(v))
        print('---------------------')

    def toJson(self):
        ''' creates a json sting to save to file '''
        data = '{\n'
        out_screens = self.screensToArray()
        for key, value in self.options.items():
            if key != 'screens':
                d = self.processVar(str(value))
                data += '  "' + str(key) + '": ' + d + ',\n'
        try:
            data += '  "screens":' + json.dumps(
                out_screens, cls=MyEncoder, sort_keys=False, indent=2)
        except TypeError:
            tkinter.messagebox.showerror(
                title='Configuration Error',
                message='No screens were added to this configuration')
        data += '\n}'
        data = data.replace("'", '"')
        return data

    def processVar(self, d):
        ''' creates if value has to be modified when converted to string '''
        if d == '': return '""'
        if d.startswith('['): return d
        if d.startswith('{'): return d
        if d.startswith('('): return d
        if d.lower() == 'false': return str(False).lower()
        if d.lower() == 'true': return str(True).lower()
        if self.checkInt(d): return str(d)
        return '"' + str(d) + '"'

    def checkInt(self, s):
        ''' returns true if given string is a digit '''
        if s[0] in ('-', '+'):
            return s[1:].isdigit()
        return s.isdigit()

    def createScreen(self):
        self.screens.append(
            Screen(self, self.gui.mainWindow.canvas, "Screen", 0, 0, 0, 0,
                   "#3d3d3d"))

    def createScreens(self,
                      numNodes,
                      numScreenRows,
                      numScreenColumns,
                      aspectRatioScreensA=16,
                      aspectRatioScreensB=9):  # read AxB, 16x9
        self.screens = []
        self.numNodes = numNodes
        self.numScreenRows = numScreenRows
        self.numScreenColumns = numScreenColumns

        # read AxB, 16x9
        self.aspectRatioScreensA = aspectRatioScreensA * numScreenColumns
        self.aspectRatioScreensB = aspectRatioScreensB * numScreenRows

        x = 0
        y = 0
        # w = int( self.gui.mainWindow.canvasW / ( self.numNodes*self.numScreenColumns ) )
        w = int(self.gui.mainWindow.canvasW / self.numNodes)
        h = int(w / (self.aspectRatioScreensA / self.aspectRatioScreensB))

        if h * self.numScreenRows > self.gui.mainWindow.canvasH:
            overflow = (h * self.numScreenRows) - self.gui.mainWindow.canvasH
            h -= int(overflow / self.numScreenRows)
            w = h * ((self.aspectRatioScreensA / self.aspectRatioScreensB))

        for node in range(self.numNodes):
            # make screen for each node
            screen = Screen(self, self.gui.mainWindow.canvas, "Screen", x, y,
                            w, h, "#3d3d3d")
            # make panel for each self.numScreenRows
            screen.divideHorizontally(self.numScreenRows)

            # make panel for each self.numScreenColumns
            # panel removes itself from screen when dividing so can't loop through that
            pans = []
            for col in screen.panels:
                pans.append(col)

            for col in pans:
                col.divideVertically(self.numScreenColumns)

            self.screens.append(screen)
            x += w

        self.gui.mainWindow.draw()

    def countScreensPanels(self):
        cS = 0
        cP = 0

        for screen in self.screens:
            cS += 1
            cP += screen.countPanels()

        print('Screens: ' + str(cS))
        print('Panels: ' + str(cP))