This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/server/src/main/java/envoy/server/processors/IDGeneratorRequestProcessor...

48 lines
1.4 KiB
Java
Executable File

package envoy.server.processors;
import java.io.IOException;
import envoy.data.IDGenerator;
import envoy.event.IDGeneratorRequest;
import envoy.server.data.*;
import envoy.server.net.ObjectWriteProxy;
/**
* @author Kai S. K. Engelbart
* @author Maximilian Käfer
* @since Envoy Server Standalone v0.1-alpha
*/
public final class IDGeneratorRequestProcessor implements ObjectProcessor<IDGeneratorRequest> {
private static final long ID_RANGE = 200;
@Override
public void process(IDGeneratorRequest input, long socketID, ObjectWriteProxy writeProxy)
throws IOException {
writeProxy.write(socketID, createIDGenerator());
}
/**
* @return a new IDGenerator
* @since Envoy Server Standalone v0.1-beta
*/
public static IDGenerator createIDGenerator() {
return createIDGenerator(ID_RANGE);
}
/**
* @param range of IDs used by the new IDGenerator
* @return a new IDGenerator with a specific range of IDs
* @since Envoy Server Standalone v0.1-beta
*/
public static IDGenerator createIDGenerator(long range) {
ConfigItem currentID =
PersistenceManager.getInstance().getConfigItemByID("currentMessageId");
IDGenerator generator = new IDGenerator(Integer.parseInt(currentID.getValue()), range);
currentID.setValue(String.valueOf(Integer.parseInt(currentID.getValue()) + range));
PersistenceManager.getInstance().updateConfigItem(currentID);
return generator;
}
}