Server API
Page Contents
Introduction
The main entry point object for the server API is the SocksServer
object. To
construct the SocksServer
object, a Configuration
object must be
provided. Depending on how a Configuration
object is constructed, a
Settings
object containing zero to many Setting
objects must be provided.
The following is a simple example of constructing a SocksServer
object:
package com.example;
import com.github.jh3nd3rs0n.jargyle.server.Configuration;
import com.github.jh3nd3rs0n.jargyle.server.Setting;
import com.github.jh3nd3rs0n.jargyle.server.Settings;
import com.github.jh3nd3rs0n.jargyle.server.SocksServer;
import java.io.IOException;
public class ServerApp {
public static void main(String[] args) throws IOException {
Setting<Object> setting =
Setting.newInstanceWithParsedValue(
"port", "1234");
Settings settings = Settings.of(setting);
Configuration configuration =
Configuration.newUnmodifiableInstance(settings);
SocksServer socksServer = new SocksServer(configuration);
// ...
}
}
The Setting Object and the Settings Object
The simplest way to create a Setting
object is to use the method
Setting.newInstanceWithParsedValue(String, String)
. The first
String
parameter would be the name of the setting. The second String
parameter would be the value of the setting to be parsed.
Server API example:
package com.example;
import com.github.jh3nd3rs0n.jargyle.server.Configuration;
import com.github.jh3nd3rs0n.jargyle.server.Setting;
import com.github.jh3nd3rs0n.jargyle.server.Settings;
import com.github.jh3nd3rs0n.jargyle.server.SocksServer;
import java.io.IOException;
public class ServerApp {
public static void main(String[] args) throws IOException {
Setting<Object> port =
Setting.newInstanceWithParsedValue("port", "1234");
Setting<Object> backlog =
Setting.newInstanceWithParsedValue(
"backlog", "100");
Setting<Object> socksServerSocketSettings =
Setting.newInstanceWithParsedValue(
"socksServerSocketSettings", "SO_TIMEOUT=0");
// ...
}
}
A complete listing of the settings can be found here.
A Settings
object can be created by using the method
Settings.of(Setting...)
. The parameter is a varargs parameter
of Setting
objects.
Server API example:
package com.example;
import com.github.jh3nd3rs0n.jargyle.server.Configuration;
import com.github.jh3nd3rs0n.jargyle.server.Setting;
import com.github.jh3nd3rs0n.jargyle.server.Settings;
import com.github.jh3nd3rs0n.jargyle.server.SocksServer;
import java.io.IOException;
public class ServerApp {
public static void main(String[] args) throws IOException {
Setting<Object> port = Setting.newInstanceWithParsedValue(
"port", "1234");
Setting<Object> backlog =
Setting.newInstanceWithParsedValue(
"backlog", "100");
Setting<Object> socksServerSocketSettings =
Setting.newInstanceWithParsedValue(
"socksServerSocketSettings", "SO_TIMEOUT=0");
Settings settings = Settings.of(
port, backlog, socksServerSocketSettings);
// ...
}
}
The Configuration Object
A Configuration
object can be constructed by any of the following methods:
Configuration.newModifiableInstance(Settings)
: Constructs a modifiableConfiguration
object with the providedSettings
object.Configuration.newUnmodifiableInstance(Settings)
: Constructs an unmodifiableConfiguration
object with the providedSettings
object. Any method that is to modify theConfiguration
object will throw aUnsupportedOperationException
.Configuration.newUpdatedInstance(ConfigurationRepository)
: Constructs an updatedConfiguration
object with the providedConfigurationRepository
object. AConfigurationRepository
object provides theConfiguration
object from another source such as a file.
Server API example:
package com.example;
import com.github.jh3nd3rs0n.jargyle.server.Configuration;
import com.github.jh3nd3rs0n.jargyle.server.Setting;
import com.github.jh3nd3rs0n.jargyle.server.Settings;
import com.github.jh3nd3rs0n.jargyle.server.SocksServer;
import java.io.IOException;
public class ServerApp {
public static void main(String[] args) throws IOException {
Setting<Object> port = Setting.newInstanceWithParsedValue(
"port", "1234");
Setting<Object> backlog =
Setting.newInstanceWithParsedValue(
"backlog", "100");
Setting<Object> socksServerSocketSettings =
Setting.newInstanceWithParsedValue(
"socksServerSocketSettings", "SO_TIMEOUT=0");
Settings settings = Settings.of(
port, backlog, socksServerSocketSettings);
Configuration configuration =
Configuration.newUnmodifiableInstance(settings);
// ...
}
}
The SocksServer Object
As mentioned earlier, to construct the SocksServer
object, a
Configuration
object must be provided.
Server API example:
package com.example;
import com.github.jh3nd3rs0n.jargyle.server.Configuration;
import com.github.jh3nd3rs0n.jargyle.server.Setting;
import com.github.jh3nd3rs0n.jargyle.server.Settings;
import com.github.jh3nd3rs0n.jargyle.server.SocksServer;
import java.io.IOException;
public class ServerApp {
public static void main(String[] args) throws IOException {
Setting<Object> port = Setting.newInstanceWithParsedValue(
"port", "1234");
Setting<Object> backlog =
Setting.newInstanceWithParsedValue(
"backlog", "100");
Setting<Object> socksServerSocketSettings =
Setting.newInstanceWithParsedValue(
"socksServerSocketSettings", "SO_TIMEOUT=0");
Settings settings = Settings.of(
port, backlog, socksServerSocketSettings);
Configuration configuration =
Configuration.newUnmodifiableInstance(settings);
SocksServer socksServer = new SocksServer(configuration);
// ...
}
}
The SocksServer
object has the following methods:
getConfiguration()
: Returns theConfiguration
object provided to thisSocksServer
objectgetHost()
: Returns theHost
object that thisSocksServer
object is bound to when the state of thisSocksServer
is set toSTARTED
. Otherwise, it returnsnull
getPort()
: Returns thePort
object that thisSocksServer
object is bound to when the state of thisSocksServer
is set toSTARTED
. Otherwise, it returnsnull
getState()
: Returns the state of thisSocksServer
objectstart()
: Starts theSocksServer
objectstop()
: Stops theSocksServer
object
Server API example:
package com.example;
import com.github.jh3nd3rs0n.jargyle.server.Configuration;
import com.github.jh3nd3rs0n.jargyle.server.Setting;
import com.github.jh3nd3rs0n.jargyle.server.Settings;
import com.github.jh3nd3rs0n.jargyle.server.SocksServer;
import java.io.IOException;
public class ServerApp {
public static void main(String[] args) throws IOException {
Setting<Object> port = Setting.newInstanceWithParsedValue(
"port", "1234");
Setting<Object> backlog =
Setting.newInstanceWithParsedValue(
"backlog", "100");
Setting<Object> socksServerSocketSettings =
Setting.newInstanceWithParsedValue(
"socksServerSocketSettings", "SO_TIMEOUT=0");
Settings settings = Settings.of(
port, backlog, socksServerSocketSettings);
Configuration configuration =
Configuration.newUnmodifiableInstance(settings);
SocksServer socksServer = new SocksServer(configuration);
socksServer.start();
System.out.println(
"Listening on port "
+ socksServer.getPort()
+ " at "
+ socksServer.getHost());
}
}