Move Connection and MainWindow classes to library
This commit is contained in:
parent
27ecce1211
commit
31712d5efa
33
client/src/connection.vala
Normal file
33
client/src/connection.vala
Normal 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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,99 +1,24 @@
|
|||||||
using Gtk;
|
using Gtk;
|
||||||
|
|
||||||
public class Connection {
|
namespace StudySystemClient {
|
||||||
public signal void response_received(uint8[] response);
|
public class App : Gtk.Application {
|
||||||
|
public App() {
|
||||||
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() {
|
|
||||||
Object(application_id: "sh.wip.study-system-client");
|
Object(application_id: "sh.wip.study-system-client");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void activate() {
|
protected override void activate() {
|
||||||
try {
|
try {
|
||||||
var connection = new Connection();
|
var connection = new Connection(Config.CERT_DIR);
|
||||||
new MainWindow(this, connection);
|
new MainWindow(this, connection);
|
||||||
} catch (Error e) {
|
} catch (Error e) {
|
||||||
stderr.printf("Failed to initialize connection: %s\n",
|
stderr.printf("Failed to initialize connection: %s\n",
|
||||||
e.message);
|
e.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static int main(string[] args) {
|
int main(string[] args) {
|
||||||
var app = new StudySystemClient();
|
var app = new StudySystemClient.App();
|
||||||
return app.run(args);
|
return app.run(args);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
47
client/src/main_window.vala
Normal file
47
client/src/main_window.vala
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -7,12 +7,26 @@ configure_file(
|
|||||||
configuration: conf
|
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(
|
exe = executable(
|
||||||
'study-system-client',
|
'study-system-client',
|
||||||
[
|
sources: files(
|
||||||
'config.vapi',
|
'config.vapi',
|
||||||
'main.vala'
|
'main.vala',
|
||||||
],
|
),
|
||||||
dependencies: [gtk_dep],
|
dependencies: [lib_dep, gtk_dep],
|
||||||
c_args: ['-w']
|
c_args: ['-w']
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user