Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
| 20 | import java.util.Set; |
| 21 | import java.util.concurrent.CompletableFuture; |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 22 | |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 23 | import org.apache.commons.lang.math.RandomUtils; |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 24 | import org.onosproject.cluster.PartitionId; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 25 | import org.onosproject.store.cluster.messaging.MessagingService; |
| 26 | |
| 27 | import com.google.common.collect.Sets; |
| 28 | |
| 29 | import io.atomix.catalyst.transport.Address; |
| 30 | import io.atomix.catalyst.transport.Client; |
| 31 | import io.atomix.catalyst.transport.Connection; |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 32 | import io.atomix.catalyst.concurrent.ThreadContext; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * {@link Client} implementation for {@link CopycatTransport}. |
| 36 | */ |
| 37 | public class CopycatTransportClient implements Client { |
| 38 | |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 39 | private final PartitionId partitionId; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 40 | private final MessagingService messagingService; |
| 41 | private final CopycatTransport.Mode mode; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 42 | private final Set<CopycatTransportConnection> connections = Sets.newConcurrentHashSet(); |
| 43 | |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 44 | CopycatTransportClient(PartitionId partitionId, MessagingService messagingService, CopycatTransport.Mode mode) { |
| 45 | this.partitionId = checkNotNull(partitionId); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 46 | this.messagingService = checkNotNull(messagingService); |
| 47 | this.mode = checkNotNull(mode); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public CompletableFuture<Connection> connect(Address remoteAddress) { |
Madan Jampani | b06ccef | 2016-01-25 10:51:16 -0800 | [diff] [blame] | 52 | ThreadContext context = ThreadContext.currentContextOrThrow(); |
Madan Jampani | 3a9911c | 2016-02-21 11:25:45 -0800 | [diff] [blame] | 53 | CopycatTransportConnection connection = new CopycatTransportConnection( |
| 54 | nextConnectionId(), |
| 55 | CopycatTransport.Mode.CLIENT, |
| 56 | partitionId, |
| 57 | remoteAddress, |
| 58 | messagingService, |
| 59 | context); |
| 60 | if (mode == CopycatTransport.Mode.CLIENT) { |
| 61 | connection.setBidirectional(); |
| 62 | } |
| 63 | connections.add(connection); |
| 64 | return CompletableFuture.supplyAsync(() -> connection, context.executor()); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public CompletableFuture<Void> close() { |
| 69 | return CompletableFuture.allOf(connections.stream().map(Connection::close).toArray(CompletableFuture[]::new)); |
| 70 | } |
| 71 | |
| 72 | private long nextConnectionId() { |
| 73 | return RandomUtils.nextLong(); |
| 74 | } |
| 75 | } |