Skip to content

Instantly share code, notes, and snippets.

@AlexBHarley
Created July 25, 2017 12:57
Show Gist options
  • Save AlexBHarley/8cddba70614d18453a72f63dcadc2dc2 to your computer and use it in GitHub Desktop.
Save AlexBHarley/8cddba70614d18453a72f63dcadc2dc2 to your computer and use it in GitHub Desktop.
package io.deepstream;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import java.util.Properties;
public class test {
public static void main(String[] args) throws Exception {
DeepstreamCommunicationService service = DeepstreamCommunicationService.start("localhost:6020", null, null);
Thread.sleep(1000);
service.stop();
}
}
class DeepstreamCommunicationService implements ConnectionStateListener {
String host;
JsonObject loginData;
Gson gson;
static DeepstreamCommunicationService instance;
DeepstreamClient client;
boolean startupComplete;
boolean reconnecting;
private DeepstreamCommunicationService(String deepstreamHost, String deepstreamUser, String deepStreamPassword) throws Exception {
host = deepstreamHost;
if (deepstreamUser != null && !deepstreamUser.equals("")) {
loginData = new JsonObject();
loginData.addProperty("username", deepstreamUser);
loginData.addProperty("password", deepStreamPassword);
} else {
loginData = null;
}
gson = new Gson();
try {
connectOrReconnect();
} catch (final Exception e) {
System.out.println("Problem when connecting to deepstream: {}");
throw e;
}
}
public static DeepstreamCommunicationService start(String deepstreamHost, String deepstreamUser, String deepStreamPassword) throws Exception {
System.out.println("Starting service...");
if (instance == null) {
instance = new DeepstreamCommunicationService(deepstreamHost, deepstreamUser, deepStreamPassword);
}
System.out.println("Started.");
return instance;
}
public void stop() throws Exception {
System.out.println("Stopping service...");
try {
client.removeConnectionChangeListener(this);
client.close();
client = null;
} catch (final Exception e) {
System.out.println("Could not close deepstream connection: {}");
}
System.out.println("Stopped.");
}
private void connectOrReconnect() throws Exception {
System.out.println("Connecting to deepstream...");
final Properties properties = new Properties();
properties.setProperty(ConfigOptions.MAX_RECONNECT_ATTEMPTS.toString(), "10");
client = DeepstreamFactory.getInstance().getClient(host, properties);
client.addConnectionChangeListener(this);
LoginResult result = null;
do {
System.out.println("Trying to login...");
if (loginData != null) {
result = client.login(loginData);
} else {
result = client.login();
}
if (!result.loggedIn()) {
System.out.println("Login and authentication failed.");
try {
Thread.sleep(1000);
} catch (final InterruptedException e) {
// Do nothing
}
}
} while (startupComplete && !result.loggedIn());
if (result.loggedIn()) {
System.out.println("Login successful.");
startupComplete = true;
reconnecting = false;
System.out.println("Connection to deepstream established.");
} else {
System.out.println("Could not login to deepstream server at startup: {}");
throw new Exception("Could not connect to deepstream server at startup!");
}
}
public void connectionStateChanged(final ConnectionState connectionState) {
if (startupComplete && connectionState == ConnectionState.CLOSED) {
if (reconnecting) {
System.out.println("Deepstream connection still lost...");
} else {
System.out.println("Deepstream connection lost!");
reconnecting = true;
}
try {
connectOrReconnect();
} catch (final Exception e) {
System.out.println("Problem when reconnecting to deepstream deepstream: {}");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment