Skip to content

Commit

Permalink
Merge pull request #20 from janvidar/master
Browse files Browse the repository at this point in the history
Added API for disconnecting from the slack server
  • Loading branch information
bcorne committed Aug 5, 2015
2 parents 4832dd2 + e2aee31 commit bc6f76b
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
Empty file modified gradlew
100644 → 100755
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public interface SlackSession

void connect() throws IOException;

void disconnect() throws IOException;

SlackMessageHandle deleteMessage(String timeStamp, SlackChannel channel);

SlackMessageHandle sendMessage(SlackChannel channel, String message, SlackAttachment attachment, SlackChatConfiguration chatConfiguration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ private <E extends SlackEvent, L extends SlackEventListener<E>> void dispatchImp
private long lastConnectionTime = -1;

private boolean reconnectOnDisconnection;
private boolean wantDisconnect = false;

private Map<Long, SlackMessageHandleImpl> pendingMessageMap = new ConcurrentHashMap<Long, SlackMessageHandleImpl>();

Expand All @@ -150,11 +151,21 @@ private <E extends SlackEvent, L extends SlackEventListener<E>> void dispatchImp
@Override
public void connect() throws IOException
{
wantDisconnect = false;
connectImpl();
LOGGER.debug("starting connection monitoring");
startConnectionMonitoring();
}

@Override
public void disconnect()
{
wantDisconnect = true;
LOGGER.debug("Disconnecting from the Slack server");
disconnectImpl();
stopConnectionMonitoring();
}

private void connectImpl() throws IOException, ClientProtocolException, ConnectException
{
LOGGER.info("connecting to slack");
Expand Down Expand Up @@ -221,6 +232,25 @@ public void onOpen(Session session, EndpointConfig config)
}
}

private void disconnectImpl()
{
if (websocketSession != null)
{
try
{
websocketSession.close();
}
catch (IOException ex)
{
// ignored.
}
finally
{
websocketSession = null;
}
}
}

private void startConnectionMonitoring()
{
connectionMonitoringThread = new Thread()
Expand All @@ -235,6 +265,11 @@ public void run()
{
// heart beat of 30s (should be configurable in the future)
Thread.sleep(30000);

// disconnect() was called.
if (wantDisconnect)
this.interrupt();

if (lastPingSent != lastPingAck || websocketSession == null)
{
// disconnection happened
Expand Down Expand Up @@ -298,7 +333,29 @@ else if (reconnectOnDisconnection)
LOGGER.debug("monitoring thread stopped");
}
};
connectionMonitoringThread.start();

if (!wantDisconnect)
connectionMonitoringThread.start();
}

private void stopConnectionMonitoring()
{
if (connectionMonitoringThread != null)
{
while (true)
{
try
{
connectionMonitoringThread.interrupt();
connectionMonitoringThread.join();
break;
}
catch (InterruptedException ex)
{
// ouch - let's try again!
}
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public void connect()

}

@Override
public void disconnect()
{
}

@Override
public SlackMessageHandle sendMessage(SlackChannel channel, String message, SlackAttachment attachment, SlackChatConfiguration chatConfiguration)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public void connect()
channels.put(channel3.getId(), channel3);
}

@Override
public void disconnect()
{
}

@Override
public SlackMessageHandle sendMessage(SlackChannel channel, String message, SlackAttachment attachment, SlackChatConfiguration chatConfiguration)
{
Expand Down

0 comments on commit bc6f76b

Please sign in to comment.