Skip to content

Commit

Permalink
Make all SSH I/O non-blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
apmasell committed Nov 4, 2014
1 parent 64f423d commit 0469e59
Show file tree
Hide file tree
Showing 8 changed files with 326 additions and 136 deletions.
89 changes: 89 additions & 0 deletions busy.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.16.1 -->
<interface>
<requires lib="gtk+" version="3.10"/>
<template class="TabbedMuxBusyDialog" parent="GtkDialog">
<property name="can_focus">False</property>
<property name="title" translatable="yes">Connecting via SSH</property>
<property name="resizable">False</property>
<property name="window_position">center-on-parent</property>
<property name="icon_name">network-wired</property>
<property name="type_hint">dialog</property>
<property name="skip_pager_hint">True</property>
<property name="deletable">False</property>
<property name="has_resize_grip">False</property>
<child internal-child="vbox">
<object class="GtkBox" id="dialog-vbox">
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
<child internal-child="action_area">
<object class="GtkButtonBox" id="dialog-action_area">
<property name="can_focus">False</property>
<property name="layout_style">end</property>
<child>
<object class="GtkButton" id="cancel_button">
<property name="label">gtk-cancel</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
<property name="always_show_image">True</property>
<signal name="activate" handler="on_cancel" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="pack_type">end</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox" id="box">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkSpinner" id="spinner">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="active">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="padding">10</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="text">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Connecting...</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="padding">10</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="padding">20</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
</template>
</interface>
5 changes: 3 additions & 2 deletions contextmenu.vala
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ class TabbedMux.ContextMenu : Gtk.Menu {
AppInfo.launch_default_for_uri (url, null);
} catch (Error e) {
var dialog = new Gtk.MessageDialog (get_toplevel () as Gtk.Window, Gtk.DialogFlags.MODAL, Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, "%s", e.message);
dialog.run ();
dialog.destroy ();
unowned Gtk.Widget unowned_this = dialog;
dialog.response.connect (() => unowned_this.destroy ());
dialog.show ();
}
}
private void copy () {
Expand Down
66 changes: 51 additions & 15 deletions open.vala
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public class TabbedMux.OpenDialog : Gtk.Dialog {
[GtkCallback]
private void on_connect () {
try {
TMuxStream? stream;

/* Validate fields common to SSH and local. */
var session_name = strip (session.text);
if (":" in session_name) {
Expand Down Expand Up @@ -88,31 +86,69 @@ public class TabbedMux.OpenDialog : Gtk.Dialog {

/* Create a handler for the password/prompts. */
var keybd_dialog = new KeyboardInteractiveDialog (this, host.text);
stream = TMuxSshStream.open (session_name, host.text, (uint16) port_number, username, tmux_binary, keybd_dialog.respond);
var busy_dialog = new BusyDialog (this);
busy_dialog.show ();
TMuxSshStream.open.begin (session_name, host.text, (uint16) port_number, username, tmux_binary, keybd_dialog.respond, busy_dialog, (sender, result) => {
try {
var stream = TMuxSshStream.open.end (result);

/* Save if desired */
if (stream != null && save.active && application is Application) {
((Application) application).saved_sessions.append_ssh (session_name, host.text, (uint16) port_number, username, tmux_binary);
}
/* Save if desired */
if (stream != null && save.active && application is Application) {
((Application) application).saved_sessions.append_ssh (session_name, host.text, (uint16) port_number, username, tmux_binary);
}
deal_with_stream (stream);
} catch (Error e) {
message ("Catch");
show_error (this, e.message);
message ("Showed");
}
message ("Kill busy");
busy_dialog.destroy ();
});
} else {
/* Local. Don't validate SSH fields. */

stream = TMuxLocalStream.open (session_name, tmux_binary);
var stream = TMuxLocalStream.open (session_name, tmux_binary);

/* Save if desired */
if (stream != null && save.active && application is Application) {
((Application) application).saved_sessions.append_local (session_name, tmux_binary);
}
}
/* Deal with the connection attempt. */
if (stream == null) {
show_error (this, "Could not connect.");
} else {
((Application) application).add_stream ((!)stream);
success = true;
deal_with_stream (stream);
}
} catch (Error e) {
show_error (this, e.message);
}
}
/* Deal with the connection attempt. */
void deal_with_stream (TMuxStream? stream) {
if (stream == null) {
show_error (this, "Could not connect.");
} else {
((Application) application).add_stream ((!)stream);
success = true;
destroy ();
}
}
}
[GtkTemplate (ui = "/name/masella/tabbedmux/busy.ui")]
public class TabbedMux.BusyDialog : Gtk.Dialog {
[GtkChild]
private Gtk.Label text;
public string message {
set {
text.label = value;
}
}
public Cancellable cancellable {
get; private set;
}
public BusyDialog (Gtk.Window parent) {
Object (application : parent.application, transient_for: parent);
cancellable = new Cancellable ();
}
[GtkCallback]
private void on_cancel () {
cancellable.cancel ();
}
}
8 changes: 2 additions & 6 deletions password_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,13 @@ int tabbed_mux_tmux_ssh_stream_password_adapter(
/*
* Since we've already got a thing for keyboard interactive, make simple passwords just call the keyboard interactive handler.
*/
int tabbed_mux_tmux_ssh_stream_password_simple(
LIBSSH2_SESSION * session,
gchar *tabbed_mux_tmux_ssh_stream_password_simple(
const gchar *username,
TabbedMuxTMuxSshStreamInteractiveAuthentication handler,
void *handler_target) {

LIBSSH2_USERAUTH_KBDINT_PROMPT prompt;
LIBSSH2_USERAUTH_KBDINT_RESPONSE response;
int result;

prompt.text = "Password:";
prompt.length = strlen(prompt.text);
Expand All @@ -80,7 +78,5 @@ int tabbed_mux_tmux_ssh_stream_password_simple(

handler(username, "Enter password.", &prompt, 1, &response, 1, handler_target);

result = libssh2_userauth_password(session, username, response.text == NULL ? "" : response.text);
g_free(response.text);
return result;
return response.text;
}
1 change: 1 addition & 0 deletions resources.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/name/masella/tabbedmux">
<file compressed="true" preprocess="xml-stripblanks">busy.ui</file>
<file compressed="true" preprocess="xml-stripblanks">open.ui</file>
<file compressed="true" preprocess="xml-stripblanks">window.ui</file>
</gresource>
Expand Down
Loading

0 comments on commit 0469e59

Please sign in to comment.