Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update high-level API to manage multiple resource clusters separately…
… from the core implementation.
- Loading branch information
Showing
8 changed files
with
434 additions
and
78 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
55 changes: 55 additions & 0 deletions
55
api/src/main/java/net/kuujo/copycat/internal/CoordinatedProtocol.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,55 @@ | ||
/* | ||
* Copyright 2015 the original author or authors. | ||
* | ||
* 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 net.kuujo.copycat.internal; | ||
|
||
import net.kuujo.copycat.protocol.AbstractProtocol; | ||
import net.kuujo.copycat.protocol.Protocol; | ||
import net.kuujo.copycat.protocol.ProtocolClient; | ||
import net.kuujo.copycat.protocol.ProtocolServer; | ||
import net.kuujo.copycat.util.internal.Assert; | ||
|
||
import java.net.URI; | ||
import java.util.concurrent.Executor; | ||
|
||
/** | ||
* Coordinated protocol implementation. | ||
* | ||
* @author <a href="http://github.com/kuujo">Jordan Halterman</a> | ||
*/ | ||
public class CoordinatedProtocol extends AbstractProtocol { | ||
private final int id; | ||
private final Protocol protocol; | ||
private final ProtocolServerRegistry registry; | ||
private final Executor executor; | ||
|
||
public CoordinatedProtocol(int id, Protocol protocol, ProtocolServerRegistry registry, Executor executor) { | ||
this.id = id; | ||
this.protocol = Assert.isNotNull(protocol, "protocol"); | ||
this.registry = Assert.isNotNull(registry, "registry"); | ||
this.executor = Assert.isNotNull(executor, "executor"); | ||
} | ||
|
||
@Override | ||
public ProtocolClient createClient(URI uri) { | ||
return new CoordinatedProtocolClient(id, protocol.createClient(uri)); | ||
} | ||
|
||
@Override | ||
public ProtocolServer createServer(URI uri) { | ||
return new CoordinatedProtocolServer(id, registry.get(uri), executor); | ||
} | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
api/src/main/java/net/kuujo/copycat/internal/CoordinatedProtocolClient.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,68 @@ | ||
/* | ||
* Copyright 2015 the original author or authors. | ||
* | ||
* 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 net.kuujo.copycat.internal; | ||
|
||
import net.kuujo.copycat.protocol.ProtocolClient; | ||
import net.kuujo.copycat.protocol.ProtocolConnection; | ||
import net.kuujo.copycat.protocol.ProtocolException; | ||
import net.kuujo.copycat.util.internal.Bytes; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* Protocol client coordinator. | ||
* | ||
* @author <a href="http://github.com/kuujo">Jordan Halterman</a> | ||
*/ | ||
public class CoordinatedProtocolClient implements ProtocolClient { | ||
private final int id; | ||
private final ProtocolClient client; | ||
|
||
public CoordinatedProtocolClient(int id, ProtocolClient client) { | ||
this.id = id; | ||
this.client = client; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<ProtocolConnection> connect() { | ||
CompletableFuture<ProtocolConnection> future = new CompletableFuture<>(); | ||
client.connect().whenComplete((connection, connectionError) -> { | ||
if (connectionError == null) { | ||
connection.write(Bytes.of(id)).whenComplete((response, responseError) -> { | ||
if (responseError == null) { | ||
byte result = response.get(); | ||
if (result == 1) { | ||
future.complete(connection); | ||
} else { | ||
future.completeExceptionally(new ProtocolException("Server not found")); | ||
} | ||
} else { | ||
future.completeExceptionally(responseError); | ||
} | ||
}); | ||
} else { | ||
future.completeExceptionally(connectionError); | ||
} | ||
}); | ||
return future; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Void> close() { | ||
return client.close(); | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
api/src/main/java/net/kuujo/copycat/internal/CoordinatedProtocolServer.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,57 @@ | ||
/* | ||
* Copyright 2015 the original author or authors. | ||
* | ||
* 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 net.kuujo.copycat.internal; | ||
|
||
import net.kuujo.copycat.EventListener; | ||
import net.kuujo.copycat.protocol.ProtocolConnection; | ||
import net.kuujo.copycat.protocol.ProtocolServer; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Executor; | ||
|
||
/** | ||
* Coordinates protocol server. | ||
* | ||
* @author <a href="http://github.com/kuujo">Jordan Halterman</a> | ||
*/ | ||
public class CoordinatedProtocolServer implements ProtocolServer { | ||
private final int id; | ||
private final ProtocolServerCoordinator coordinator; | ||
private final Executor executor; | ||
|
||
public CoordinatedProtocolServer(int id, ProtocolServerCoordinator coordinator, Executor executor) { | ||
this.id = id; | ||
this.coordinator = coordinator; | ||
this.executor = executor; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Void> listen() { | ||
return coordinator.listen(); | ||
} | ||
|
||
@Override | ||
public ProtocolServer connectListener(EventListener<ProtocolConnection> handler) { | ||
coordinator.connectListener(id, handler, executor); | ||
return this; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Void> close() { | ||
return coordinator.close(); | ||
} | ||
|
||
} |
Oops, something went wrong.