paul@938 | 1 | #!/usr/bin/env python |
paul@938 | 2 | |
paul@938 | 3 | """ |
paul@1182 | 4 | Autonomous scheduling functionality. |
paul@938 | 5 | |
paul@1025 | 6 | Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@938 | 7 | |
paul@938 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@938 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@938 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@938 | 11 | version. |
paul@938 | 12 | |
paul@938 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@938 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@938 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@938 | 16 | details. |
paul@938 | 17 | |
paul@938 | 18 | You should have received a copy of the GNU General Public License along with |
paul@938 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@938 | 20 | """ |
paul@938 | 21 | |
paul@1031 | 22 | from imiptools.text import parse_line |
paul@1039 | 23 | from imiptools.handlers.scheduling.manifest import confirmation_functions, \ |
paul@1040 | 24 | locking_functions, \ |
paul@1039 | 25 | retraction_functions, \ |
paul@1040 | 26 | scheduling_functions, \ |
paul@1040 | 27 | unlocking_functions |
paul@1039 | 28 | |
paul@1039 | 29 | # Function application/invocation. |
paul@938 | 30 | |
paul@1058 | 31 | def apply_scheduling_functions(handler): |
paul@1025 | 32 | |
paul@1025 | 33 | """ |
paul@1058 | 34 | Apply the scheduling functions for the current object of the given |
paul@1053 | 35 | 'handler'. This function starts a transaction that should be finalised using |
paul@1053 | 36 | the 'finish_scheduling' function. |
paul@1153 | 37 | |
paul@1153 | 38 | Return a tuple containing the scheduling decision and any accompanying |
paul@1153 | 39 | description. |
paul@1025 | 40 | """ |
paul@1025 | 41 | |
paul@1040 | 42 | # First, lock the resources to be used. |
paul@1040 | 43 | |
paul@1058 | 44 | start_scheduling(handler) |
paul@1040 | 45 | |
paul@1053 | 46 | # Obtain the actual scheduling functions with arguments. |
paul@1040 | 47 | |
paul@1058 | 48 | schedulers = get_function_calls(handler.get_scheduling_functions(), scheduling_functions) |
paul@1040 | 49 | |
paul@1040 | 50 | # Then, invoke the scheduling functions. |
paul@1031 | 51 | |
paul@1025 | 52 | response = "ACCEPTED" |
paul@1153 | 53 | description = None |
paul@1025 | 54 | |
paul@1040 | 55 | for fn, args in schedulers: |
paul@1025 | 56 | |
paul@1025 | 57 | # NOTE: Should signal an error for incorrectly configured resources. |
paul@1025 | 58 | |
paul@1025 | 59 | if not fn: |
paul@1153 | 60 | return "DECLINED", None |
paul@1025 | 61 | |
paul@1176 | 62 | # Keep evaluating scheduling functions, stopping if one declines or |
paul@1176 | 63 | # gives a null response, or if one delegates to another resource. |
paul@1025 | 64 | |
paul@1025 | 65 | else: |
paul@1031 | 66 | result = fn(handler, args) |
paul@1153 | 67 | result, description = result or ("DECLINED", None) |
paul@1025 | 68 | |
paul@1025 | 69 | # Return a negative result immediately. |
paul@1025 | 70 | |
paul@1176 | 71 | if result in ("DECLINED", "DELEGATED"): |
paul@1153 | 72 | return result, description |
paul@1025 | 73 | |
paul@1025 | 74 | # Modify the eventual response from acceptance if a countering |
paul@1025 | 75 | # result is obtained. |
paul@1025 | 76 | |
paul@1025 | 77 | elif response == "ACCEPTED": |
paul@1025 | 78 | response = result |
paul@1025 | 79 | |
paul@1153 | 80 | return response, description |
paul@1025 | 81 | |
paul@1058 | 82 | def confirm_scheduling(handler): |
paul@1039 | 83 | |
paul@1039 | 84 | """ |
paul@1058 | 85 | Confirm scheduling using confirmation functions for the current object of |
paul@1058 | 86 | the given 'handler'. This function continues a transaction that should be |
paul@1058 | 87 | finalised using the 'finish_scheduling' function. |
paul@1039 | 88 | """ |
paul@1039 | 89 | |
paul@1058 | 90 | # Obtain the actual confirmation functions with arguments. |
paul@1039 | 91 | |
paul@1058 | 92 | functions = get_function_calls(handler.get_scheduling_functions(), confirmation_functions) |
paul@1039 | 93 | apply_functions(functions, handler) |
paul@1039 | 94 | |
paul@1058 | 95 | def retract_scheduling(handler): |
paul@1053 | 96 | |
paul@1053 | 97 | """ |
paul@1058 | 98 | Retract scheduling using retraction functions for the current object of the |
paul@1058 | 99 | given 'handler'. This function is a complete transaction in itself. |
paul@1053 | 100 | """ |
paul@1053 | 101 | |
paul@1053 | 102 | # First, lock the resources to be used. |
paul@1053 | 103 | |
paul@1058 | 104 | start_scheduling(handler) |
paul@1172 | 105 | try: |
paul@1053 | 106 | |
paul@1172 | 107 | # Obtain the actual retraction functions with arguments. |
paul@1172 | 108 | |
paul@1172 | 109 | functions = get_function_calls(handler.get_scheduling_functions(), retraction_functions) |
paul@1172 | 110 | apply_functions(functions, handler) |
paul@1053 | 111 | |
paul@1053 | 112 | # Finally, unlock the resources. |
paul@1053 | 113 | |
paul@1172 | 114 | finally: |
paul@1172 | 115 | finish_scheduling(handler) |
paul@1053 | 116 | |
paul@1058 | 117 | def start_scheduling(handler): |
paul@1053 | 118 | |
paul@1053 | 119 | """ |
paul@1053 | 120 | Apply locking functions for the given scheduling 'functions' and for the |
paul@1053 | 121 | current object of the given 'handler'. |
paul@1053 | 122 | """ |
paul@1053 | 123 | |
paul@1053 | 124 | # Obtain functions to lock resources. |
paul@1053 | 125 | |
paul@1058 | 126 | functions = get_function_calls(handler.get_scheduling_functions(), locking_functions) |
paul@1058 | 127 | apply_functions(functions, handler) |
paul@1053 | 128 | |
paul@1058 | 129 | def finish_scheduling(handler): |
paul@1040 | 130 | |
paul@1040 | 131 | """ |
paul@1040 | 132 | Finish scheduling using the given scheduling 'functions' for the current |
paul@1040 | 133 | object of the given 'handler'. |
paul@1040 | 134 | """ |
paul@1040 | 135 | |
paul@1040 | 136 | # Obtain functions to unlock resources. |
paul@1040 | 137 | |
paul@1058 | 138 | functions = get_function_calls(handler.get_scheduling_functions(), unlocking_functions) |
paul@1058 | 139 | apply_functions(functions, handler) |
paul@1039 | 140 | |
paul@1039 | 141 | def apply_functions(functions, handler): |
paul@1039 | 142 | |
paul@1039 | 143 | """ |
paul@1039 | 144 | Apply the given notification 'functions' for the current object of the given |
paul@1058 | 145 | 'handler'. Where functions are provided more than once, they will be called |
paul@1058 | 146 | only once for each distinct set of arguments. |
paul@1039 | 147 | """ |
paul@1039 | 148 | |
paul@1058 | 149 | applied = set() |
paul@1058 | 150 | |
paul@1039 | 151 | for fn, args in functions: |
paul@1039 | 152 | |
paul@1039 | 153 | # NOTE: Should signal an error for incorrectly configured resources. |
paul@1039 | 154 | |
paul@1058 | 155 | if not fn or (fn, args) in applied: |
paul@1039 | 156 | continue |
paul@1039 | 157 | |
paul@1039 | 158 | fn(handler, args) |
paul@1058 | 159 | applied.add((fn, args)) |
paul@1039 | 160 | |
paul@1039 | 161 | # Function lookup. |
paul@1039 | 162 | |
paul@1039 | 163 | def get_function_calls(lines, registry): |
paul@1031 | 164 | |
paul@1031 | 165 | """ |
paul@1031 | 166 | Parse the given 'lines', returning a list of (function, arguments) tuples, |
paul@1031 | 167 | with each function being a genuine function object and with the arguments |
paul@1031 | 168 | being a list of strings. |
paul@1031 | 169 | |
paul@1031 | 170 | Each of the 'lines' should employ the function name and argument strings |
paul@1031 | 171 | separated by whitespace, with any whitespace inside arguments quoted using |
paul@1031 | 172 | single or double quotes. |
paul@1039 | 173 | |
paul@1039 | 174 | The given 'registry' indicates the mapping from function names to actual |
paul@1039 | 175 | functions. |
paul@1031 | 176 | """ |
paul@1031 | 177 | |
paul@1031 | 178 | functions = [] |
paul@1031 | 179 | |
paul@1031 | 180 | for line in lines: |
paul@1031 | 181 | parts = parse_line(line) |
paul@1199 | 182 | |
paul@1199 | 183 | # A sequence of functions is provided for each name. |
paul@1199 | 184 | |
paul@1199 | 185 | l = registry.get(parts[0]) |
paul@1199 | 186 | if l: |
paul@1199 | 187 | for function in l: |
paul@1199 | 188 | functions.append((function, tuple(parts[1:]))) |
paul@1031 | 189 | |
paul@1031 | 190 | return functions |
paul@1031 | 191 | |
paul@938 | 192 | # vim: tabstop=4 expandtab shiftwidth=4 |