Everything is ready now to create our first proxy.
First thing we need is to create our proxy that will contain all the application logic (spawning connections, closing connections).
public class ExampleProxy extends Proxy {
private ExampleProxyConfig config;
private ExampleConnectionManager connectionManager;
public ExampleProxy(ProxyModule module, PetepHelper helper, ExampleProxyConfig config) {
super(module, helper);
this.config = config;
}
@Override
public ConnectionManager getConnectionManager() {
return connectionManager;
}
@Override
public boolean prepare() {
Logger.getGlobal().info("Example proxy prepared!");
connectionManager = new ExampleConnectionManager();
return true;
}
@Override
public boolean start() {
Logger.getGlobal().info("Example proxy started!");
for (int i = 0; i < config.getConnectionCount(); ++i) {
int id = connectionManager.nextId();
ExampleConnection connection = new ExampleConnection(id, this, "EC_" + id);
if (connection.start()) {
connectionManager.add(connection);
}
}
return true;
}
@Override
public void stop() {
connectionManager.stop();
Logger.getGlobal().info("Example proxy stopped!");
}
}
Well done, our proxy logic is prepared. Now we need to create module that will allow user to add our proxy to the list of proxies in PETEP.
public class ExampleProxyModule extends ProxyModule implements Configurable<ExampleProxyConfig> {
private ExampleProxyConfig config;
public ExampleProxyModule(
ProxyModuleFactory factory, String code, String name, String description, boolean enabled) {
super(factory, code, name, description, enabled);
}
@Override
public Proxy createProxy(PetepHelper helper) {
return new ExampleProxy(this, helper, config);
}
@Override
public void loadConfig(ExampleProxyConfig config) {
Logger.getGlobal().info("Example proxy configuration loaded!");
this.config = config;
}
@Override
public ExampleProxyConfig saveConfig() {
Logger.getGlobal().info("Example proxy configuration saved!");
return config;
}
}
Notice that our module implements Configurable interface that allows it to store the proxy configuration in the project.
Now we can create a factory that will create the modules.
public class ExampleProxyModuleFactory extends ProxyModuleFactory
implements Configurator<ExampleProxyConfig>, ProxySerializer, ProxyDeserializer {
public ExampleProxyModuleFactory(Extension extension) {
super(extension);
}
@Override
public ProxyModule createModule(String code, String name, String description, boolean enabled) {
return new ExampleProxyModule(this, code, name, description, enabled);
}
@Override
public String getCode() {
return "example_proxy";
}
@Override
public String getName() {
return "Example Proxy";
}
@Override
public ConfigPane<ExampleProxyConfig> createConfigPane() throws IOException {
return new ExampleProxyConfigurator();
}
@Override
public PduMetadataPane createPduMetadataPane() throws IOException {
return new ExamplePduMetadataPane();
}
@Override
public ProxySerializer getSerializer() {
return this;
}
@Override
public ProxyDeserializer getDeserializer() {
return this;
}
@Override
public PDU deserializePdu(
Proxy proxy,
Connection connection,
PduDestination destination,
byte[] buffer,
int size,
Set<String> tags,
Map<String, String> metadata) {
return new ExamplePdu(
proxy,
connection,
destination,
buffer,
size,
tags,
metadata.get("StringParam"),
Integer.parseInt(metadata.get("IntegerParam")));
}
@Override
public Map<String, String> serializePduMetadata(PDU pdu) {
ExamplePdu temp = (ExamplePdu) pdu;
return Map.of(
"StringParam",
temp.getStringParam(),
"IntegerParam",
String.valueOf(temp.getIntegerParam()));
}
}
Notice the following things:
We are almost done, we just have to register our factory to the PETEP in out PetepExtension.init() method.
helper.registerProxyModuleFactory(new ExampleProxyModuleFactory(this));
Awesome! Now we can deploy our extension and enjoy our new proxy!