imip-agent

imiptools/handlers/resource.py

1040:8f2b373a311b
2016-02-08 Paul Boddie Introduced compound locking so that information can be in a consistent state for scheduling functions and confirmation functions within the same transaction, unchanged by concurrent transactions.
     1 #!/usr/bin/env python     2      3 """     4 Handlers for a resource.     5      6 Copyright (C) 2014, 2015, 2016 Paul Boddie <paul@boddie.org.uk>     7      8 This program is free software; you can redistribute it and/or modify it under     9 the terms of the GNU General Public License as published by the Free Software    10 Foundation; either version 3 of the License, or (at your option) any later    11 version.    12     13 This program is distributed in the hope that it will be useful, but WITHOUT    14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    15 FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more    16 details.    17     18 You should have received a copy of the GNU General Public License along with    19 this program.  If not, see <http://www.gnu.org/licenses/>.    20 """    21     22 from imiptools.data import get_address, to_part, uri_dict    23 from imiptools.handlers import Handler    24 from imiptools.handlers.common import CommonFreebusy, CommonEvent    25 from imiptools.handlers.scheduling import apply_scheduling_functions, \    26                                           confirm_scheduling, \    27                                           finish_scheduling, \    28                                           retract_scheduling    29     30 class ResourceHandler(CommonEvent, Handler):    31     32     "Handling mechanisms specific to resources."    33     34     def _process(self, handle_for_attendee):    35     36         """    37         Record details from the incoming message, using the given    38         'handle_for_attendee' callable to process any valid message    39         appropriately.    40         """    41     42         oa = self.require_organiser_and_attendees()    43         if not oa:    44             return None    45     46         organiser_item, attendees = oa    47     48         # Process for the current user, a resource as attendee.    49     50         if not self.have_new_object():    51             return None    52     53         # Collect response objects produced when handling the request.    54     55         handle_for_attendee()    56     57     def _add_for_attendee(self):    58     59         """    60         Attempt to add a recurrence to an existing object for the current user.    61         This does not request a response concerning participation, apparently.    62         """    63     64         # Request details where configured, doing so for unknown objects anyway.    65     66         if self.will_refresh():    67             self.make_refresh()    68             return    69     70         # Record the event as a recurrence of the parent object.    71     72         self.update_recurrenceid()    73         self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node())    74     75         # Remove any previous cancellations involving this event.    76     77         self.store.remove_cancellation(self.user, self.uid, self.recurrenceid)    78     79         # Update free/busy information.    80     81         self.update_event_in_freebusy(for_organiser=False)    82     83         # Confirm the scheduling of the recurrence.    84     85         self.confirm_scheduling()    86     87     def _schedule_for_attendee(self):    88     89         "Attempt to schedule the current object for the current user."    90     91         attendee_attr = uri_dict(self.obj.get_value_map("ATTENDEE"))[self.user]    92     93         # Attempt to schedule the event.    94     95         scheduled = self.schedule()    96     97         try:    98             # Update the participation of the resource in the object.    99             # Update free/busy information.   100    101             if scheduled in ("ACCEPTED", "DECLINED"):   102                 method = "REPLY"   103                 attendee_attr = self.update_participation(scheduled)   104    105                 self.update_event_in_freebusy(for_organiser=False)   106                 self.remove_event_from_freebusy_offers()   107    108                 # Set the complete event or an additional occurrence.   109    110                 event = self.obj.to_node()   111                 self.store.set_event(self.user, self.uid, self.recurrenceid, event)   112    113                 # Remove additional recurrences if handling a complete event.   114                 # Also remove any previous cancellations involving this event.   115    116                 if not self.recurrenceid:   117                     self.store.remove_recurrences(self.user, self.uid)   118                     self.store.remove_cancellations(self.user, self.uid)   119                 else:   120                     self.store.remove_cancellation(self.user, self.uid, self.recurrenceid)   121    122                 if scheduled == "ACCEPTED":   123                     self.confirm_scheduling()   124    125             # For countered proposals, record the offer in the resource's   126             # free/busy collection.   127    128             elif scheduled == "COUNTER":   129                 method = "COUNTER"   130                 self.update_event_in_freebusy_offers()   131    132             # For inappropriate periods, reply declining participation.   133    134             else:   135                 method = "REPLY"   136                 attendee_attr = self.update_participation("DECLINED")   137    138         # Confirm any scheduling.   139    140         finally:   141             self.finish_scheduling()   142    143         # Make a version of the object with just this attendee, update the   144         # DTSTAMP in the response, and return the object for sending.   145    146         self.update_sender(attendee_attr)   147         self.obj["ATTENDEE"] = [(self.user, attendee_attr)]   148         self.update_dtstamp()   149         self.add_result(method, map(get_address, self.obj.get_values("ORGANIZER")), to_part(method, [self.obj.to_node()]))   150    151     def _cancel_for_attendee(self):   152    153         """   154         Cancel for the current user their attendance of the event described by   155         the current object.   156         """   157    158         # Update free/busy information.   159    160         self.remove_event_from_freebusy()   161    162         # Update the stored event and cancel it.   163    164         self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node())   165         self.store.cancel_event(self.user, self.uid, self.recurrenceid)   166    167         # Retract the scheduling of the event.   168    169         self.retract_scheduling()   170    171     def _revoke_for_attendee(self):   172    173         "Revoke any counter-proposal recorded as a free/busy offer."   174    175         self.remove_event_from_freebusy_offers()   176    177     # Scheduling details.   178    179     def schedule(self):   180    181         """   182         Attempt to schedule the current object, returning an indication of the   183         kind of response to be returned: "COUNTER" for counter-proposals,   184         "ACCEPTED" for acceptances, "DECLINED" for rejections, and None for   185         invalid requests.   186         """   187    188         # Obtain a list of scheduling functions.   189    190         functions = self.get_preferences().get("scheduling_function",   191             "schedule_in_freebusy").split("\n")   192    193         return apply_scheduling_functions(functions, self)   194    195     def confirm_scheduling(self):   196    197         "Confirm that this event has been scheduled."   198    199         functions = self.get_preferences().get("confirmation_function")   200    201         if functions:   202             confirm_scheduling(functions.split("\n"), self)   203    204     def finish_scheduling(self):   205    206         "Finish the scheduling, unlocking resources where appropriate."   207    208         functions = self.get_preferences().get("scheduling_function",   209             "schedule_in_freebusy").split("\n")   210    211         finish_scheduling(functions, self)   212    213     def retract_scheduling(self):   214    215         "Retract this event from scheduling records."   216    217         functions = self.get_preferences().get("retraction_function")   218    219         if functions:   220             retract_scheduling(functions.split("\n"), self)   221    222 class Event(ResourceHandler):   223    224     "An event handler."   225    226     def add(self):   227    228         "Add a new occurrence to an existing event."   229    230         self._process(self._add_for_attendee)   231    232     def cancel(self):   233    234         "Cancel attendance for attendees."   235    236         self._process(self._cancel_for_attendee)   237    238     def counter(self):   239    240         "Since this handler does not send requests, it will not handle replies."   241    242         pass   243    244     def declinecounter(self):   245    246         "Revoke any counter-proposal."   247    248         self._process(self._revoke_for_attendee)   249    250     def publish(self):   251    252         """   253         Resources only consider events sent as requests, not generally published   254         events.   255         """   256    257         pass   258    259     def refresh(self):   260    261         """   262         Refresh messages are typically sent to event organisers, but resources   263         do not act as organisers themselves.   264         """   265    266         pass   267    268     def reply(self):   269    270         "Since this handler does not send requests, it will not handle replies."   271    272         pass   273    274     def request(self):   275    276         """   277         Respond to a request by preparing a reply containing accept/decline   278         information for the recipient.   279    280         No support for countering requests is implemented.   281         """   282    283         self._process(self._schedule_for_attendee)   284    285 class Freebusy(CommonFreebusy, Handler):   286    287     "A free/busy handler."   288    289     def publish(self):   290    291         "Resources ignore generally published free/busy information."   292    293         self._record_freebusy(from_organiser=True)   294    295     def reply(self):   296    297         "Since this handler does not send requests, it will not handle replies."   298    299         pass   300    301     # request provided by CommonFreeBusy.request   302    303 # Handler registry.   304    305 handlers = [   306     ("VFREEBUSY",   Freebusy),   307     ("VEVENT",      Event),   308     ]   309    310 # vim: tabstop=4 expandtab shiftwidth=4