xyz.thepathfinder.android.Connection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pathfinder-android Show documentation
Show all versions of pathfinder-android Show documentation
Android/Java client library for the Pathfinder service
package xyz.thepathfinder.android;
import javax.websocket.CloseReason;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.Session;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Controls access the web socket connection with the Pathfinder sever.
* To gain access to the connection use {@link PathfinderServices#getConnection()}.
*
* @author David Robinson
*/
class Connection extends Endpoint {
/**
* The application identifier sent to the Pathfinder server.
*/
private final String applictionIdentifier;
/**
* The user's credentials to the Pathfinder server.
*/
private final String userCredentials;
/**
* Access to the model registry to notify {@link Model}s of incoming web socket messages.
*/
private final ModelRegistry registry;
/**
* The web socket session used to send messages through the web socket.
*/
private Session session;
/**
* Number of messages sent through the web socket.
*/
private long sentMessageCount;
/**
* Handles incoming web socket messages.
*/
private MessageHandler messageHandler;
/**
* Logs all outgoing messages through the web socket.
*/
private Logger logger = Logger.getLogger(Connection.class.getName());
/**
* Constructs a connection object that controls access to the web socket connection
* with the Pathfinder Server.
*
* @param applictionIdentifier the application identifier provided by the Pathfinder server for the application
* @param userCredentials the user's credentials for this application
* @param registry a model registry
*/
protected Connection(String applictionIdentifier, String userCredentials, ModelRegistry registry) {
this.applictionIdentifier = applictionIdentifier;
this.userCredentials = userCredentials;
this.registry = registry;
this.sentMessageCount = 0L;
}
/**
* Sends a text message through the web socket to the Pathfinder server.
*
* @param message the message to be sent.
* @throws IllegalStateException the web socket is not connected.
*/
public void sendMessage(String message) {
logger.log(Level.INFO, "Sending json: " + message);
if (this.isConnected()) {
this.session.getAsyncRemote().sendText(message);
this.sentMessageCount++;
} else {
throw new IllegalStateException("The connection to Pathfinder was closed.");
}
}
/**
* {@inheritDoc}
*/
@Override
public void onOpen(Session session, EndpointConfig config) {
logger.log(Level.INFO, "Connection opened");
this.session = session;
this.messageHandler = new MessageHandler(this.registry);
this.session.addMessageHandler(this.messageHandler);
}
/**
* {@inheritDoc}
*/
@Override
public void onClose(Session session, CloseReason closeReason) {
logger.log(Level.INFO, "Connection closed: " + closeReason);
this.session = session;
}
/**
* Returns if the web socket is connected.
*
* @return true if the web socket is connected, false otherwise.
*/
public boolean isConnected() {
return this.session != null && this.session.isOpen();
}
/**
* Returns the number of messages sent through the web socket.
*
* @return the number of messages sent.
*/
public long getSentMessageCount() {
return this.sentMessageCount;
}
/**
* Returns the number of messages receive through the web socket.
*
* @return the number of messages received.
*/
public long getReceivedMessageCount() {
return this.messageHandler.getReceivedMessageCount();
}
/**
* Closes the web socket connection with the Pathfinder server.
*
* @param reason the reason for closing the web socket.
* @throws IOException if the web socket failed to close properly.
*/
public void close(CloseReason reason) throws IOException {
this.session.close(reason);
}
}