Move Connection and MainWindow classes to library

This commit is contained in:
Camden Dixie O'Brien 2025-02-23 10:57:36 +00:00
parent 27ecce1211
commit 31712d5efa
4 changed files with 114 additions and 95 deletions

View File

@ -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]);
}
}
}

View File

@ -1,99 +1,24 @@
using Gtk;
public class Connection {
public signal void response_received(uint8[] response);
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;
}
}
}
public class StudySystemClient : Gtk.Application {
public StudySystemClient() {
namespace StudySystemClient {
public class App : Gtk.Application {
public App() {
Object(application_id: "sh.wip.study-system-client");
}
protected override void activate() {
try {
var connection = new Connection();
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 static int main(string[] args) {
var app = new StudySystemClient();
int main(string[] args) {
var app = new StudySystemClient.App();
return app.run(args);
}
}

View File

@ -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;
}
}
}
}

View File

@ -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']
)