Coverage for src/lib/python/isc/bind10/socket_cache : 95%
        
        
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
| 
 # Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC") # # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 
 Here's the cache for sockets from socket creator. """ 
 
 """ Exception raised when the socket creator is unable to create requested socket. Possible reasons might be the address it should be bound to is already taken, the permissions are insufficient, the address family is not supported on this computer and many more. 
 The errno, if not None, is passed from the socket creator. """ 
 """ The requested socket is already taken by other component and the sharing parameters don't allow sharing with the new request. """ 
 """ This represents one socket cached by the cache program. This should never be used directly by a user, it is used internally by the Cache. Therefore many member variables are used directly instead of by a accessor method. 
 Be warned that this object implements the __del__ method. It closes the socket held inside in it. But this poses various problems with garbage collector. In short, do not make reference cycles with this and generally leave this class alone to live peacefully. """ """ Creates the socket. 
 The protocol, address and port are preserved for the information. """ # Mapping from token -> application # The tokens which were not yet picked up # Share modes and names by the tokens (token -> (mode, name)) 
 """ Closes the file descriptor. """ 
 """ Checks if the given share mode and name is compatible with the ones already installed here. 
 The allowed values for mode are listed in the Cache.get_token function. """ 
 # Go through the existing ones # One of them can't live together with anything ename != name: # One of them can't live together with someone of different # name # else both are ANY or SAMEAPP with the same name, which is OK # No problem found, so we consider it OK 
 """ This is the cache for sockets from socket creator. The purpose of cache is to hold the sockets that were requested, until they are no longer needed. One reason is, the socket is created before it is sent over the unix domain socket in boss, so we need to keep it somewhere for a while. 
 The other reason is, a single socket might be requested multiple times. So we keep it here in case someone else might ask for it. 
 Each socket kept here has a reference count and when it drops to zero, it is removed from cache and closed. 
 This is expected to be part of Boss, it is not a general utility class. 
 It is not expected to be subclassed. The methods and members are named as protected so tests are easier access into them. """ """ Initialization. The creator is the socket creator object (isc.bind10.sockcreator.Creator) which will be used to create yet uncached sockets. """ # The sockets we have live here, these dicts are various ways how # to get them. Each of them contains the Socket objects somehow 
 # This one is dict of token: socket for the ones that were not yet # picked up by an application. # This format is the same as above, but for the tokens that were # already picked up by the application and not yet released. # This is a dict from applications to set of tokens used by the # application, for the sockets already picked up by an application # The sockets live here to be indexed by protocol, address and # subsequently by port # These are just the tokens actually in use, so we don't generate # dupes. If one is dropped, it can be potentially reclaimed. 
 """ This requests a token representing a socket. The socket is either found in the cache already or requested from the creator at this time (and cached for later time). 
 The parameters are: - protocol: either 'UDP' or 'TCP' - address: the IPAddr object representing the address to bind to - port: integer saying which port to bind to - share_mode: either 'NO', 'SAMEAPP' or 'ANY', specifying how the socket can be shared with others. See bin/bind10/creatorapi.txt for details. - share_name: the name of application, in case of 'SAMEAPP' share mode. Only requests with the same name can share the socket. 
 If the call is successful, it returns a string token which can be used to pick up the socket later. The socket is created with reference count zero and if it isn't picked up soon enough (the time yet has to be set), it will be removed and the token is invalid. 
 It can fail in various ways. Explicitly listed exceptions are: - SocketError: this one is thrown if the socket creator couldn't provide the socket and it is not yet cached (it belongs to other application, for example). - ShareError: the socket is already in the cache, but it can't be shared due to share_mode and share_name combination (both the request restrictions and of all copies of socket handed out are considered, so it can be raised even if you call it with share_mode 'ANY'). - isc.bind10.sockcreator.CreatorError: fatal creator errors are propagated. Thay should cause the boss to exit if ever encountered. 
 Note that it isn't guaranteed the tokens would be unique and they should be used as an opaque handle only. """ # Something in the dicts is not there, so socket is to be # created else: # And cache it # Now we get the token, check it is compatible share_mode + " and name " + share_name) # Grab yet unused token token = 't' + str(random.randint(0, 2 ** 32-1)) 
 """ This returns the socket created by get_token. The token should be the one returned from previous call from get_token. The token can be used only once to receive the socket. 
 The application is a token representing the application that requested it. Currently, boss uses the file descriptor of connection from the application, but anything which can be a key in a dict is OK from the cache's point of view. You just need to use the same thing in drop_application. 
 In case the token is considered invalid (it doesn't come from the get_token, it was already used, the socket wasn't picked up soon enough, ...), it raises ValueError. """ " isn't waiting to be picked up") 
 """ This signals the application no longer uses the socket which was requested by the given token. It decreases the reference count for the socket and closes and removes the cached copy if it was the last one. 
 It raises ValueError if the token doesn't exist. """ "active socket") # Now, remove everything from the bookkeeping # The socket is not used by anything now, so remove it # Clean up empty branches of the structure 
 """ This signals the application terminated and all sockets it picked up should be considered unused by it now. It effectively calls drop_socket on each of the sockets the application picked up and didn't drop yet. 
 If the application is invalid (no get_socket was successful with this value of application), it raises ValueError. """ # Get a copy. Who knows how iteration works through sets if we # delete from it during the time, so we'll just have our own copy # to iterate " doesn't hold any sockets") # We don't call del now. The last drop_socket should have # removed the application key as well.  |