Skip to content

Commit

Permalink
Introduce ObservableClient interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Noa Resare committed Feb 20, 2015
1 parent cea60a7 commit 46ff4e2
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 28 deletions.
10 changes: 5 additions & 5 deletions src/main/java/com/spotify/folsom/ConnectFuture.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,26 @@ public class ConnectFuture
* @param client
* @param awaitedState
*/
private ConnectFuture(RawMemcacheClient client, boolean awaitedState) {
private ConnectFuture(ObservableClient client, boolean awaitedState) {
this.awaitedState = awaitedState;
client.registerForConnectionChanges(this);
check(client);
}

public static ListenableFuture<Void> disconnectFuture(RawMemcacheClient client) {
public static ListenableFuture<Void> disconnectFuture(ObservableClient client) {
return new ConnectFuture(client, false);
}

public static ListenableFuture<Void> connectFuture(RawMemcacheClient client) {
public static ListenableFuture<Void> connectFuture(ObservableClient client) {
return new ConnectFuture(client, true);
}

@Override
public void connectionChanged(RawMemcacheClient client) {
public void connectionChanged(ObservableClient client) {
check(client);
}

private void check(RawMemcacheClient client) {
private void check(ObservableClient client) {
if (awaitedState == client.isConnected()) {
if (set(null)) {
client.unregisterForConnectionChanges(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
public interface ConnectionChangeListener {

@Subscribe
void connectionChanged(RawMemcacheClient client);
void connectionChanged(ObservableClient client);
}
45 changes: 45 additions & 0 deletions src/main/java/com/spotify/folsom/ObservableClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2015 Spotify AB
*
* 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 com.spotify.folsom;

/**
* Implementations of this interface has a notion of connectedness to a remote
* and the ability to notify listeners of connection state changes.
*/
public interface ObservableClient {
/**
* Register for connection change events. This should trigger at least once for every
* connection change. You should immediately get an initial callback.
* @param listener the listener to notify of connection changes
*/
void registerForConnectionChanges(ConnectionChangeListener listener);

/**
* Unregister for connection change events.
* @param listener the listener that should no longer receive connection change
* notifications
*/
void unregisterForConnectionChanges(ConnectionChangeListener listener);


/**
* Is the client connected to a server?
* @return true if the client is connected
*/
boolean isConnected();
}
21 changes: 1 addition & 20 deletions src/main/java/com/spotify/folsom/RawMemcacheClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/**
* A raw memcache client, mostly useful internally
*/
public interface RawMemcacheClient {
public interface RawMemcacheClient extends ObservableClient {

<T> ListenableFuture<T> send(Request<T> request);

Expand All @@ -32,12 +32,6 @@ public interface RawMemcacheClient {
*/
void shutdown();

/**
* Is the client connected to a server?
* @return true if the client is connected
*/
boolean isConnected();

/**
* How many actual socket connections do we have, including currently disconnected clients.
* @return the number of total connections
Expand All @@ -49,17 +43,4 @@ public interface RawMemcacheClient {
* @return the number of active connections
*/
int numActiveConnections();

/**
* Register for connection change events. This should trigger at least once for every
* connection change. You should immediately get an initial callback.
* @param listener
*/
void registerForConnectionChanges(ConnectionChangeListener listener);

/**
* Unregister for connection change events.
* @param listener
*/
void unregisterForConnectionChanges(ConnectionChangeListener listener);
}
2 changes: 1 addition & 1 deletion src/main/java/com/spotify/folsom/SrvKetamaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public void run() {

private class MyConnectionChangeListener implements ConnectionChangeListener {
@Override
public void connectionChanged(RawMemcacheClient client) {
public void connectionChanged(ObservableClient client) {
notifyConnectionChange();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.base.Preconditions;
import com.spotify.folsom.AbstractRawMemcacheClient;
import com.spotify.folsom.ConnectionChangeListener;
import com.spotify.folsom.ObservableClient;
import com.spotify.folsom.RawMemcacheClient;

import java.util.Collection;
Expand Down Expand Up @@ -79,7 +80,7 @@ public String toString() {
}

@Override
public void connectionChanged(RawMemcacheClient client) {
public void connectionChanged(ObservableClient client) {
notifyConnectionChange();
}
}

1 comment on commit 46ff4e2

@spkrka
Copy link
Member

@spkrka spkrka commented on 46ff4e2 Feb 20, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.