diff --git a/client/src/connection.vala b/client/src/connection.vala new file mode 100644 index 0000000..d0e27fd --- /dev/null +++ b/client/src/connection.vala @@ -0,0 +1,33 @@ +namespace StudySystemClient { + public class Connection { + public signal void response_received(uint8[] response); + + private TlsClientConnection tls_client; + + public Connection(string cert_dir) throws Error { + var loopback = new InetAddress.loopback(SocketFamily.IPV6); + var host = new InetSocketAddress(loopback, 12888); + + var db_type = TlsBackend.get_default().get_file_database_type(); + var ca_path = cert_dir + "/ca.pem"; + var db = Object.new(db_type, "anchors", ca_path) as TlsDatabase; + var cert_path = cert_dir + "/client.pem"; + var cert = new TlsCertificate.from_file(cert_path); + + var plain_client = new SocketClient(); + var plain_connection = plain_client.connect(host); + tls_client = TlsClientConnection.new(plain_connection, host); + + tls_client.set_database(db); + tls_client.set_certificate(cert); + tls_client.handshake(); + } + + public async void send(uint8[] message) throws Error { + yield tls_client.output_stream.write_async(message); + var response = new uint8[1024]; + var len = yield tls_client.input_stream.read_async(response); + response_received(response[0:len]); + } + } +} diff --git a/client/src/main.vala b/client/src/main.vala index bb76b93..37a4944 100644 --- a/client/src/main.vala +++ b/client/src/main.vala @@ -1,99 +1,24 @@ using Gtk; -public class Connection { - public signal void response_received(uint8[] response); +namespace StudySystemClient { + public class App : Gtk.Application { + public App() { + Object(application_id: "sh.wip.study-system-client"); + } - private TlsClientConnection tls_client; - - public Connection() throws Error { - var loopback = new InetAddress.loopback(SocketFamily.IPV6); - var host = new InetSocketAddress(loopback, 12888); - - var db_type = TlsBackend.get_default().get_file_database_type(); - const string ca_path = Config.CERT_DIR + "/ca.pem"; - var db = Object.new(db_type, "anchors", ca_path) as TlsDatabase; - var cert = - new TlsCertificate.from_file(Config.CERT_DIR + "/client.pem"); - - var plain_client = new SocketClient(); - var plain_connection = plain_client.connect(host); - tls_client = TlsClientConnection.new(plain_connection, host); - - tls_client.set_database(db); - tls_client.set_certificate(cert); - tls_client.handshake(); - } - - public async void send(uint8[] message) throws Error { - yield tls_client.output_stream.write_async(message); - var response = new uint8[1024]; - var len = yield tls_client.input_stream.read_async(response); - response_received(response[0:len]); - } -} - -public class MainWindow : Gtk.ApplicationWindow { - private Connection connection; - private Gtk.Button send_button; - private Gtk.Label response_label; - - public MainWindow(Gtk.Application app, Connection connection) { - Object(application: app); - - default_width = 600; - default_height = 400; - title = "Study System Client"; - - this.connection = connection; - connection.response_received.connect((response) => { - response_label.label = "Response: " + (string)response; - }); - - var box = new Gtk.Box(Gtk.Orientation.VERTICAL, 10); - box.margin_start = 10; - box.margin_end = 10; - box.margin_top = 10; - box.margin_bottom = 10; - - send_button = new Gtk.Button.with_label("Send"); - send_button.clicked.connect(on_send_clicked); - box.append(send_button); - - response_label = new Gtk.Label(""); - response_label.wrap = true; - box.append(response_label); - - set_child(box); - - present(); - } - - private async void on_send_clicked() { - try { - yield connection.send("Foo".data); - } catch (Error e) { - response_label.label = "Error: " + e.message; + protected override void activate() { + try { + var connection = new Connection(Config.CERT_DIR); + new MainWindow(this, connection); + } catch (Error e) { + stderr.printf("Failed to initialize connection: %s\n", + e.message); + } } } } -public class StudySystemClient : Gtk.Application { - public StudySystemClient() { - Object(application_id: "sh.wip.study-system-client"); - } - - protected override void activate() { - try { - var connection = new Connection(); - new MainWindow(this, connection); - } catch (Error e) { - stderr.printf("Failed to initialize connection: %s\n", - e.message); - } - } - - public static int main(string[] args) { - var app = new StudySystemClient(); - return app.run(args); - } +int main(string[] args) { + var app = new StudySystemClient.App(); + return app.run(args); } diff --git a/client/src/main_window.vala b/client/src/main_window.vala new file mode 100644 index 0000000..bfd147c --- /dev/null +++ b/client/src/main_window.vala @@ -0,0 +1,47 @@ +namespace StudySystemClient { + const string title = "Study System Client"; + + public class MainWindow : Gtk.ApplicationWindow { + private Connection connection; + private Gtk.Button send_button; + private Gtk.Label response_label; + + public MainWindow(Gtk.Application app, Connection connection) { + Object(application: app); + + default_width = 360; + default_height = 580; + + this.connection = connection; + connection.response_received.connect((response) => { + response_label.label = "Response: " + (string)response; + }); + + var box = new Gtk.Box(Gtk.Orientation.VERTICAL, 10); + box.margin_start = 10; + box.margin_end = 10; + box.margin_top = 10; + box.margin_bottom = 10; + + send_button = new Gtk.Button.with_label("Send"); + send_button.clicked.connect(on_send_clicked); + box.append(send_button); + + response_label = new Gtk.Label(""); + response_label.wrap = true; + box.append(response_label); + + set_child(box); + + present(); + } + + private async void on_send_clicked() { + try { + yield connection.send("Foo".data); + } catch (Error e) { + response_label.label = "Error: " + e.message; + } + } + } +} diff --git a/client/src/meson.build b/client/src/meson.build index 9f2cd5c..b5795aa 100644 --- a/client/src/meson.build +++ b/client/src/meson.build @@ -7,12 +7,26 @@ configure_file( configuration: conf ) +lib = library( + 'study-system-client', + sources: files( + 'connection.vala', + 'main_window.vala', + ), + dependencies: [gtk_dep], + vala_vapi: 'study-system-client.vapi' +) +lib_dep = declare_dependency( + link_with: lib, + include_directories: include_directories('.') +) + exe = executable( 'study-system-client', - [ + sources: files( 'config.vapi', - 'main.vala' - ], - dependencies: [gtk_dep], + 'main.vala', + ), + dependencies: [lib_dep, gtk_dep], c_args: ['-w'] )