-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from RADAR-CNS/0.3_release
0.3 release
- Loading branch information
Showing
40 changed files
with
1,819 additions
and
1,396 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/main/java/org/radarcns/producer/rest/ConnectionState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2017 The Hyve and King's College London | ||
* | ||
* 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 org.radarcns.producer.rest; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Current connection status of a KafkaSender. After a timeout occurs this will turn to | ||
* disconnected. When the connection is dropped, the associated KafkaSender should set this to | ||
* disconnected, when it successfully connects, it should set it to connected. This class is | ||
* thread-safe. The state transition diagram is CONNECTED to and from DISCONNECTED with | ||
* {@link #didConnect()} and {@link #didDisconnect()}; CONNECTED to and from UNKNOWN with | ||
* {@link #getState()} after a timeout occurs and {@link #didConnect()}; and UNKNOWN to DISCONNECTED | ||
* with {@link #didDisconnect()}. | ||
* | ||
* A connection state could be shared with multiple HTTP clients if they are talking to the same | ||
* server. | ||
*/ | ||
public final class ConnectionState { | ||
|
||
/** State symbols of the connection. */ | ||
public enum State { | ||
CONNECTED, DISCONNECTED, UNKNOWN | ||
} | ||
|
||
private long timeout; | ||
private long lastConnection; | ||
private State state; | ||
|
||
/** | ||
* Connection state with given timeout. The state will start as connected. | ||
* @param timeout timeout | ||
* @param unit unit of the timeout | ||
* @throws IllegalArgumentException if the timeout is not strictly positive. | ||
*/ | ||
public ConnectionState(long timeout, TimeUnit unit) { | ||
lastConnection = -1L; | ||
state = State.UNKNOWN; | ||
setTimeout(timeout, unit); | ||
} | ||
|
||
/** Current state of the connection. */ | ||
public synchronized State getState() { | ||
if (state == State.CONNECTED && System.currentTimeMillis() - lastConnection >= timeout) { | ||
state = State.UNKNOWN; | ||
} | ||
return state; | ||
} | ||
|
||
/** For a sender to indicate that a connection attempt succeeded. */ | ||
public synchronized void didConnect() { | ||
state = State.CONNECTED; | ||
lastConnection = System.currentTimeMillis(); | ||
} | ||
|
||
/** For a sender to indicate that a connection attempt failed. */ | ||
public synchronized void didDisconnect() { | ||
state = State.DISCONNECTED; | ||
} | ||
|
||
/** | ||
* Set the timeout after which the state will go from CONNECTED to UNKNOWN. | ||
* @param timeout timeout | ||
* @param unit unit of the timeout | ||
* @throws IllegalArgumentException if the timeout is not strictly positive | ||
*/ | ||
public synchronized void setTimeout(long timeout, TimeUnit unit) { | ||
if (timeout <= 0) { | ||
throw new IllegalArgumentException("Timeout must be strictly positive"); | ||
} | ||
this.timeout = TimeUnit.MILLISECONDS.convert(timeout, unit); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/org/radarcns/producer/rest/ManagedConnectionPool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2017 The Hyve and King's College London | ||
* | ||
* 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 org.radarcns.producer.rest; | ||
|
||
import okhttp3.ConnectionPool; | ||
|
||
/** | ||
* Manages a connection pool. Using this class properly ensures that all resources are released | ||
* after a global ConnectionPool is no longer used. Each call to {@link #acquire()} must be matched | ||
* with exactly one call to {@link #release()} once the acquired ConnectionPool is no longer used. | ||
* This class is thread-safe. | ||
*/ | ||
public class ManagedConnectionPool { | ||
public static final ManagedConnectionPool GLOBAL_POOL = new ManagedConnectionPool(); | ||
private ConnectionPool connectionPool; | ||
private int references; | ||
|
||
public ManagedConnectionPool() { | ||
references = 0; | ||
} | ||
|
||
/** | ||
* Acquire access to a connection pool. A call to this must be matched with exactly one call | ||
* to {@link #release()}. | ||
* @return the single connection pool managed by this object | ||
*/ | ||
public synchronized ConnectionPool acquire() { | ||
if (references == 0) { | ||
connectionPool = new ConnectionPool(); | ||
} | ||
references++; | ||
return connectionPool; | ||
} | ||
|
||
/** | ||
* Release access to a connection pool once it is no longer used. | ||
* @throws IllegalStateException if release is called more often than acquire. | ||
*/ | ||
public synchronized void release() { | ||
if (references == 0) { | ||
throw new IllegalStateException( | ||
"Cannot release a connection pool that was not acquired."); | ||
} | ||
references--; | ||
if (references == 0) { | ||
connectionPool = null; | ||
} | ||
} | ||
} |
Oops, something went wrong.