Add setting the fixed port to the smartdevice start dialog

This commit is contained in:
Charles Haley 2012-08-23 08:06:30 +02:00
parent b16d35f1f6
commit 3cae8e614d
4 changed files with 180 additions and 79 deletions

View File

@ -89,6 +89,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
SEND_NOOP_EVERY_NTH_PROBE = 5
DISCONNECT_AFTER_N_SECONDS = 30*60 # 30 minutes
opcodes = {
'NOOP' : 12,
'OK' : 0,
@ -163,6 +164,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
OPT_COLLECTIONS = 8
OPT_AUTODISCONNECT = 10
def __init__(self, path):
self.sync_lock = threading.RLock()
self.noop_counter = 0
@ -493,6 +495,10 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
return self.OPT_PASSWORD
elif opt_string == 'autostart':
return self.OPT_AUTOSTART
elif opt_string == 'use_fixed_port':
return self.OPT_USE_PORT
elif opt_string == 'port_number':
return self.OPT_PORT_NUMBER
else:
return None
@ -537,6 +543,19 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
self.device_socket = None
self.is_connected = False
def _attach_to_port(self, port):
try:
self._debug('try port', port)
self.listen_socket.bind(('', port))
except socket.error:
self._debug('socket error on port', port)
port = 0
except:
self._debug('Unknown exception while allocating listen socket')
traceback.print_exc()
raise
return port
# The public interface methods.
@synchronous('sync_lock')
@ -937,57 +956,70 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
self.noop_counter = 0
self.connection_attempts = {}
self.client_can_stream_books = False
message = None
try:
self.listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except:
self._debug('creation of listen socket failed')
return
message = 'creation of listen socket failed'
self._debug(message)
return message
i = 0
while i < 100: # try up to 100 random port numbers
if self.settings().extra_customization[self.OPT_USE_PORT]:
i = 100
try:
port = int(self.settings().extra_customization[self.OPT_PORT_NUMBER])
opt_port = int(self.settings().extra_customization[self.OPT_PORT_NUMBER])
except:
port = 0
else:
i += 1
port = random.randint(8192, 32000)
try:
self._debug('try port', port)
self.listen_socket.bind(('', port))
break
except socket.error:
port = 0
except:
self._debug('Unknown exception while allocating listen socket')
traceback.print_exc()
raise
if port == 0:
self._debug('Failed to allocate a port');
message = _('Invalid port in options: %s')% \
self.settings().extra_customization[self.OPT_PORT_NUMBER]
self.debug(message)
self.listen_socket.close()
self.listen_socket = None
self.is_connected = False
return
return message
port = self._attach_to_port(opt_port)
if port == 0:
message = 'Failed to connect to port %d'%opt_port
self._debug(message);
self.listen_socket.close()
self.listen_socket = None
self.is_connected = False
return message
else:
while i < 100: # try up to 100 random port numbers
i += 1
port = self._attach_to_port(random.randint(8192, 32000))
if port != 0:
break;
if port == 0:
message = _('Failed to allocate a random port')
self._debug(message);
self.listen_socket.close()
self.listen_socket = None
self.is_connected = False
return message
try:
self.listen_socket.listen(0)
except:
self._debug('listen on socket failed', port)
message = 'listen on port %d failed' % port
self._debug(message)
self.listen_socket.close()
self.listen_socket = None
self.is_connected = False
return
return message
try:
do_zeroconf(publish_zeroconf, port)
except:
self._debug('registration with bonjour failed')
message = 'registration with bonjour failed'
self._debug(message)
self.listen_socket.close()
self.listen_socket = None
self.is_connected = False
return
return message
self._debug('listening on port', port)
self.port = port
@ -1008,7 +1040,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
@synchronous('sync_lock')
def start_plugin(self):
self.startup_on_demand()
return self.startup_on_demand()
@synchronous('sync_lock')
def stop_plugin(self):

View File

@ -554,7 +554,7 @@ class DeviceManager(Thread): # {{{
# will switch to the device thread before calling the plugin.
def start_plugin(self, name):
self._call_request(name, 'start_plugin')
return self._call_request(name, 'start_plugin')
def stop_plugin(self, name):
self._call_request(name, 'stop_plugin')

View File

@ -7,6 +7,7 @@ __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
from PyQt4.Qt import (QDialog, QLineEdit, Qt)
from calibre.gui2 import error_dialog
from calibre.gui2.dialogs.smartdevice_ui import Ui_Dialog
class SmartdeviceDialog(QDialog, Ui_Dialog):
@ -27,7 +28,21 @@ class SmartdeviceDialog(QDialog, Ui_Dialog):
'smart device interface when calibre starts. You should not do '
'this if you are using a network that is not secure and you '
'are not setting a password.') + '</p>')
self.use_fixed_port.setToolTip('<p>' +
_('Check this box if you want calibre to use a fixed network '
'port. Normally you will not need to do this. However, if '
'your device consistently fails to connect to calibre, '
'try checking this box.') + '</p>')
self.fixed_port.setToolTip('<p>' +
_('A port number must be a 4-digit integer less than 32,000. No '
'two network applications on the same computer can use '
'the same port number. If calibre says that it fails to connect '
'to the port, try a different number.') + '</p>')
self.show_password.stateChanged[int].connect(self.toggle_password)
self.use_fixed_port.stateChanged[int].connect(self.use_fixed_port_changed)
self.device_manager = parent.device_manager
@ -37,8 +52,22 @@ class SmartdeviceDialog(QDialog, Ui_Dialog):
pw = self.device_manager.get_option('smartdevice', 'password')
if pw:
self.password_box.setText(pw)
use_fixed_port = self.device_manager.get_option('smartdevice', 'use_fixed_port')
port_number = self.device_manager.get_option('smartdevice', 'port_number')
self.fixed_port.setText(port_number)
self.use_fixed_port.setChecked(use_fixed_port);
if not use_fixed_port:
self.fixed_port.setEnabled(False);
if pw:
self.password_box.setText(pw)
self.resize(self.sizeHint())
def use_fixed_port_changed(self, state):
self.fixed_port.setEnabled(state == Qt.Checked)
def toggle_password(self, state):
self.password_box.setEchoMode(QLineEdit.Password if state ==
Qt.Unchecked else QLineEdit.Normal)
@ -48,6 +77,16 @@ class SmartdeviceDialog(QDialog, Ui_Dialog):
unicode(self.password_box.text()))
self.device_manager.set_option('smartdevice', 'autostart',
self.autostart_box.isChecked())
self.device_manager.start_plugin('smartdevice')
self.device_manager.set_option('smartdevice', 'use_fixed_port',
self.use_fixed_port.isChecked())
self.device_manager.set_option('smartdevice', 'port_number',
unicode(self.fixed_port.text()))
message = self.device_manager.start_plugin('smartdevice')
if not self.device_manager.is_running('smartdevice'):
error_dialog(self, _('Problem starting smartdevice'),
_('The snart device driver did not start. It said "%s"')%message, show=True)
else:
QDialog.accept(self)

View File

@ -71,6 +71,36 @@
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_21">
<property name="text">
<string>Optional &amp;fixed port:</string>
</property>
<property name="buddy">
<cstring>fixed_port</cstring>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="fixed_port">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>100</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="placeholderText">
<string>Optional port number</string>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="QCheckBox" name="use_fixed_port">
<property name="text">
<string>&amp;Use the fixed port</string>
</property>
</widget>
</item>
<item row="4" column="0" colspan="3">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">