0
0
mirror of https://github.com/OpenVPN/openvpn3.git synced 2024-09-20 04:02:15 +02:00

Split OpenVPNClientThread.connect() into two methods: (1) connect()

establishes the client connection thread, and (2) wait_thread()
waits for client connection thread completion.
This commit is contained in:
James Yonan 2012-02-15 18:59:24 +00:00
parent 7573a2dea6
commit bc02566ed9
2 changed files with 26 additions and 14 deletions

View File

@ -1,5 +1,5 @@
public class Client implements OpenVPNClientThread.EventReceiver {
private OpenVPNClientThread client_impl;
private OpenVPNClientThread client_thread;
public static class ConfigError extends Exception {
public ConfigError(String msg) { super(msg); }
@ -16,12 +16,12 @@ public class Client implements OpenVPNClientThread.EventReceiver {
public Client(String config_text, String username, String password) throws ConfigError, CredsUnspecifiedError {
// init client implementation object
client_impl = new OpenVPNClientThread();
client_thread = new OpenVPNClientThread();
// load/eval config
Config config = new Config();
config.setContent(config_text);
EvalConfig ec = client_impl.eval_config(config);
EvalConfig ec = client_thread.eval_config(config);
if (ec.getError())
throw new ConfigError("OpenVPN config file parse error: " + ec.getMessage());
@ -37,27 +37,30 @@ public class Client implements OpenVPNClientThread.EventReceiver {
else
throw new CredsUnspecifiedError("OpenVPN config file requires username/password but none provided");
}
client_impl.provide_creds(creds);
client_thread.provide_creds(creds);
}
public void connect() {
// connect
Status status = client_impl.connect(this);
client_thread.connect(this);
// wait for worker thread to exit
Status status = client_thread.wait_thread();
// show connect status
System.out.format("END Status: err=%b msg='%s'%n", status.getError(), status.getMessage());
}
public void stop() {
client_impl.stop();
client_thread.stop();
}
public void show_stats() {
int n = client_impl.stats_n();
int n = client_thread.stats_n();
for (int i = 0; i < n; ++i)
{
String name = client_impl.stats_name(i);
long value = client_impl.stats_value(i);
String name = client_thread.stats_name(i);
long value = client_thread.stats_value(i);
if (value > 0)
System.out.format("STAT %s=%s%n", name, value);
}

View File

@ -1,6 +1,7 @@
public class OpenVPNClientThread extends OpenVPNClientBase implements Runnable {
private EventReceiver parent;
private Status connect_status;
private Thread thread;
public interface EventReceiver {
void event(Event event);
@ -11,15 +12,22 @@ public class OpenVPNClientThread extends OpenVPNClientBase implements Runnable {
parent = null;
}
public Status connect(EventReceiver parent_arg) {
// start connect session in worker thread
public void connect(EventReceiver parent_arg) {
// direct client callbacks to parent
parent = parent_arg;
// execute client in a worker thread
Thread thread = new Thread(this, "OpenVPNClientThread");
thread.start();
// clear status
connect_status = null;
// wait for worker thread to complete
// execute client in a worker thread
thread = new Thread(this, "OpenVPNClientThread");
thread.start();
}
// wait for worker thread to complete; to stop thread,
// first call super stop() method then wait_thread().
public Status wait_thread() {
boolean interrupted;
do {
interrupted = false;
@ -34,6 +42,7 @@ public class OpenVPNClientThread extends OpenVPNClientBase implements Runnable {
// dissassociate client callbacks from parent
parent = null;
thread = null;
return connect_status;
}