-
-
Notifications
You must be signed in to change notification settings - Fork 700
/
connector.py
165 lines (138 loc) · 4.87 KB
/
connector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# -*- coding: utf-8 -*-
"""
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
@author: mkaay
"""
SERVER_VERSION = "0.4.8"
from time import sleep
from uuid import uuid4 as uuid
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import socket
from module.remote.thriftbackend.ThriftClient import ThriftClient, WrongLogin, NoSSL, NoConnection
from thrift.Thrift import TException
class Connector(QObject):
"""
manages the connection to the pyload core via thrift
"""
firstAttempt = True
def __init__(self):
QObject.__init__(self)
self.mutex = QMutex()
self.connectionID = None
self.host = None
self.port = None
self.user = None
self.password = None
self.ssl = None
self.running = True
self.internal = False
self.proxy = self.Dummy()
def setConnectionData(self, host, port, user, password, ssl=False):
"""
set connection data for connection attempt, called from slotConnect
"""
self.host = host
self.port = port
self.user = user
self.password = password
self.ssl = ssl
def connectProxy(self):
"""
initialize thrift rpc client,
check for ssl, check auth,
setup dispatcher,
connect error signals,
check server version
"""
if self.internal: return True
err = None
try:
client = ThriftClient(self.host, self.port, self.user, self.password)
except WrongLogin:
err = _("bad login credentials")
except NoSSL:
err = _("no ssl support")
except NoConnection:
err = _("can't connect to host")
if err:
if not Connector.firstAttempt:
self.emit(SIGNAL("errorBox"), err)
Connector.firstAttempt = False
return False
self.proxy = DispatchRPC(self.mutex, client)
self.connect(self.proxy, SIGNAL("connectionLost"), self, SIGNAL("connectionLost"))
server_version = self.proxy.getServerVersion()
self.connectionID = uuid().hex
if not server_version == SERVER_VERSION:
self.emit(SIGNAL("errorBox"), _("server is version %(new)s client accepts version %(current)s") % { "new": server_version, "current": SERVER_VERSION})
return False
return True
def __getattr__(self, attr):
"""
redirect rpc calls to dispatcher
"""
return getattr(self.proxy, attr)
class Dummy(object):
"""
dummy rpc proxy, to prevent errors
"""
def __nonzero__(self):
return False
def __getattr__(self, attr):
def dummy(*args, **kwargs):
return None
return dummy
class DispatchRPC(QObject):
"""
wraps the thrift client, to catch critical exceptions (connection lost)
adds thread safety
"""
def __init__(self, mutex, server):
QObject.__init__(self)
self.mutex = mutex
self.server = server
def __getattr__(self, attr):
"""
redirect and wrap call in Wrapper instance, locks dispatcher
"""
self.mutex.lock()
self.fname = attr
f = self.Wrapper(getattr(self.server, attr), self.mutex, self)
return f
class Wrapper(object):
"""
represents a rpc call
"""
def __init__(self, f, mutex, dispatcher):
self.f = f
self.mutex = mutex
self.dispatcher = dispatcher
def __call__(self, *args, **kwargs):
"""
instance is called, rpc is executed
exceptions are processed
finally dispatcher is unlocked
"""
lost = False
try:
return self.f(*args, **kwargs)
except socket.error: #necessary?
lost = True
except TException:
lost = True
finally:
self.mutex.unlock()
if lost:
from traceback import print_exc
print_exc()
self.dispatcher.emit(SIGNAL("connectionLost"))