io.atomix.copycat.server.state.AbstractState Maven / Gradle / Ivy
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.atomix.copycat.server.state;
import io.atomix.catalyst.transport.Connection;
import io.atomix.catalyst.util.Managed;
import io.atomix.copycat.client.request.*;
import io.atomix.copycat.client.response.*;
import io.atomix.copycat.server.CopycatServer;
import io.atomix.copycat.server.request.*;
import io.atomix.copycat.server.response.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.CompletableFuture;
/**
* Abstract state.
*
* @author Jordan Halterman
*/
abstract class AbstractState implements Managed {
protected final Logger LOGGER = LoggerFactory.getLogger(getClass());
protected final ServerState context;
private boolean open = true;
protected AbstractState(ServerState context) {
this.context = context;
}
/**
* Returns the Copycat state represented by this state.
*
* @return The Copycat state represented by this state.
*/
public abstract CopycatServer.State type();
/**
* Logs a request.
*/
protected final R logRequest(R request) {
LOGGER.debug("{} - Received {}", context.getAddress(), request);
return request;
}
/**
* Logs a response.
*/
protected final R logResponse(R response) {
LOGGER.debug("{} - Sent {}", context.getAddress(), response);
return response;
}
@Override
public CompletableFuture open() {
context.checkThread();
open = true;
return CompletableFuture.completedFuture(null);
}
@Override
public boolean isOpen() {
return open;
}
/**
* Transitions to a new state.
*/
protected void transition(CopycatServer.State state) {
context.transition(state);
}
/**
* Handles a register request.
*/
protected abstract CompletableFuture register(RegisterRequest request);
/**
* Handles a connect request.
*/
protected abstract CompletableFuture connect(ConnectRequest request, Connection connection);
/**
* Handles an accept request.
*/
protected abstract CompletableFuture accept(AcceptRequest request);
/**
* Handles a keep alive request.
*/
protected abstract CompletableFuture keepAlive(KeepAliveRequest request);
/**
* Handles an unregister request.
*/
protected abstract CompletableFuture unregister(UnregisterRequest request);
/**
* Handles a publish request.
*/
protected abstract CompletableFuture publish(PublishRequest request);
/**
* Handles a join request.
*/
protected abstract CompletableFuture join(JoinRequest request);
/**
* Handles a leave request.
*/
protected abstract CompletableFuture leave(LeaveRequest request);
/**
* Handles an append request.
*/
protected abstract CompletableFuture append(AppendRequest request);
/**
* Handles a poll request.
*/
protected abstract CompletableFuture poll(PollRequest request);
/**
* Handles a vote request.
*/
protected abstract CompletableFuture vote(VoteRequest request);
/**
* Handles a command request.
*/
protected abstract CompletableFuture command(CommandRequest request);
/**
* Handles a query request.
*/
protected abstract CompletableFuture query(QueryRequest request);
@Override
public CompletableFuture close() {
context.checkThread();
open = false;
return CompletableFuture.completedFuture(null);
}
@Override
public boolean isClosed() {
return !open;
}
@Override
public String toString() {
return String.format("%s[context=%s]", getClass().getSimpleName(), context);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy