[120] | 1 | #Licensed to the Apache Software Foundation (ASF) under one |
---|
| 2 | #or more contributor license agreements. See the NOTICE file |
---|
| 3 | #distributed with this work for additional information |
---|
| 4 | #regarding copyright ownership. The ASF licenses this file |
---|
| 5 | #to you under the Apache License, Version 2.0 (the |
---|
| 6 | #"License"); you may not use this file except in compliance |
---|
| 7 | #with the License. You may obtain a copy of the License at |
---|
| 8 | |
---|
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 10 | |
---|
| 11 | #Unless required by applicable law or agreed to in writing, software |
---|
| 12 | #distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 13 | #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 14 | #See the License for the specific language governing permissions and |
---|
| 15 | #limitations under the License. |
---|
| 16 | import xmlrpclib, time, random, signal |
---|
| 17 | from hodlib.Common.util import hodInterrupt, HodInterruptException |
---|
| 18 | |
---|
| 19 | class hodXRClient(xmlrpclib.ServerProxy): |
---|
| 20 | def __init__(self, uri, transport=None, encoding=None, verbose=0, |
---|
| 21 | allow_none=0, installSignalHandlers=1, retryRequests=True, timeOut=15): |
---|
| 22 | xmlrpclib.ServerProxy.__init__(self, uri, transport, encoding, verbose, |
---|
| 23 | allow_none) |
---|
| 24 | self.__retryRequests = retryRequests |
---|
| 25 | self.__timeOut = timeOut |
---|
| 26 | if (installSignalHandlers!=0): |
---|
| 27 | self.__set_alarm() |
---|
| 28 | |
---|
| 29 | def __set_alarm(self): |
---|
| 30 | def alarm_handler(sigNum, sigHandler): |
---|
| 31 | raise Exception("XML-RPC socket timeout.") |
---|
| 32 | |
---|
| 33 | signal.signal(signal.SIGALRM, alarm_handler) |
---|
| 34 | |
---|
| 35 | def __request(self, methodname, params): |
---|
| 36 | response = None |
---|
| 37 | retryWaitTime = 5 + random.randint(0, 5) |
---|
| 38 | for i in range(0, 30): |
---|
| 39 | signal.alarm(self.__timeOut) |
---|
| 40 | try: |
---|
| 41 | response = self._ServerProxy__request(methodname, params) |
---|
| 42 | signal.alarm(0) |
---|
| 43 | break |
---|
| 44 | except Exception: |
---|
| 45 | if self.__retryRequests: |
---|
| 46 | if hodInterrupt.isSet(): |
---|
| 47 | raise HodInterruptException() |
---|
| 48 | time.sleep(retryWaitTime) |
---|
| 49 | else: |
---|
| 50 | raise Exception("hodXRClientTimeout") |
---|
| 51 | |
---|
| 52 | return response |
---|
| 53 | |
---|
| 54 | def __getattr__(self, name): |
---|
| 55 | # magic method dispatcher |
---|
| 56 | return xmlrpclib._Method(self.__request, name) |
---|
| 57 | |
---|