/** * Returns the state machine's clock. * * @return The state machine's clock. */ protected Clock getClock() { return getWallClock(); }
@Override public final void close(SessionId sessionId) { Session session = sessions.remove(sessionId); if (session != null) { onClose(session); } }
@Override public final void expire(SessionId sessionId) { Session session = sessions.remove(sessionId); if (session != null) { onExpire(session); } }
@Override public final void init(ServiceContext context) { this.context = context; this.executor = new DefaultServiceExecutor(context, serializer()); this.log = ContextualLoggerFactory.getLogger(getClass(), LoggerContext.builder(PrimitiveService.class) .addValue(context.serviceId()) .add("type", context.serviceType()) .add("name", context.serviceName()) .build()); configure(executor); }
/** * Returns the session with the given identifier. * * @param sessionId the session identifier * @return the primitive session */ protected Session<C> getSession(long sessionId) { return getSession(SessionId.from(sessionId)); }
/** * Encodes the given object using the configured {@link #serializer()}. * * @param object the object to encode * @param <T> the object type * @return the encoded bytes */ protected <T> byte[] encode(T object) { return object != null ? serializer().encode(object) : null; }
/** * Configures the state machine. * <p> * By default, this method will configure state machine operations by extracting public methods with * a single {@link Commit} parameter via reflection. Override this method to explicitly register * state machine operations via the provided {@link ServiceExecutor}. * * @param executor The state machine executor. */ protected void configure(ServiceExecutor executor) { Operations.getOperationMap(getClass()).forEach(((operationId, method) -> configure(operationId, method, executor))); }
@Override @SuppressWarnings("unchecked") public final void register(Session session) { sessions.put(session.sessionId(), new ClientSession<>(clientInterface, session)); onOpen(session); }
/** * Returns the current session. * * @return the current session */ protected Session<C> getCurrentSession() { return getSession(context.currentSession().sessionId()); }
/** * Decodes the given object using the configured {@link #serializer()}. * * @param bytes the bytes to decode * @param <T> the object type * @return the decoded object */ protected <T> T decode(byte[] bytes) { return bytes != null ? serializer().decode(bytes) : null; }