Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 16 | package org.onosproject.store.cluster.messaging; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 17 | |
Jordan Halterman | 1cf4f84 | 2019-05-21 11:27:50 -0700 | [diff] [blame] | 18 | import java.time.Duration; |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 19 | import java.util.concurrent.CompletableFuture; |
Madan Jampani | ec5ae34 | 2015-04-13 15:43:10 -0700 | [diff] [blame] | 20 | import java.util.concurrent.Executor; |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 21 | import java.util.function.BiConsumer; |
| 22 | import java.util.function.BiFunction; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 23 | |
Jordan Halterman | 1cf4f84 | 2019-05-21 11:27:50 -0700 | [diff] [blame] | 24 | import com.google.common.util.concurrent.MoreExecutors; |
| 25 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 26 | /** |
| 27 | * Interface for low level messaging primitives. |
| 28 | */ |
| 29 | public interface MessagingService { |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 30 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 31 | /** |
| 32 | * Sends a message asynchronously to the specified communication end point. |
| 33 | * The message is specified using the type and payload. |
| 34 | * @param ep end point to send the message to. |
| 35 | * @param type type of message. |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 36 | * @param payload message payload bytes. |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 37 | * @return future that is completed when the message is sent |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 38 | */ |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 39 | CompletableFuture<Void> sendAsync(Endpoint ep, String type, byte[] payload); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 42 | * Sends a message asynchronously and expects a response. |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 43 | * @param ep end point to send the message to. |
| 44 | * @param type type of message. |
| 45 | * @param payload message payload. |
| 46 | * @return a response future |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 47 | */ |
Jordan Halterman | 1cf4f84 | 2019-05-21 11:27:50 -0700 | [diff] [blame] | 48 | default CompletableFuture<byte[]> sendAndReceive(Endpoint ep, String type, byte[] payload) { |
pier | 3daed98 | 2020-02-19 20:44:23 +0100 | [diff] [blame] | 49 | return sendAndReceive(ep, type, payload, null, MoreExecutors.directExecutor()); |
Jordan Halterman | 1cf4f84 | 2019-05-21 11:27:50 -0700 | [diff] [blame] | 50 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 51 | |
| 52 | /** |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 53 | * Sends a message synchronously and expects a response. |
| 54 | * @param ep end point to send the message to. |
| 55 | * @param type type of message. |
| 56 | * @param payload message payload. |
| 57 | * @param executor executor over which any follow up actions after completion will be executed. |
| 58 | * @return a response future |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 59 | */ |
Jordan Halterman | 1cf4f84 | 2019-05-21 11:27:50 -0700 | [diff] [blame] | 60 | default CompletableFuture<byte[]> sendAndReceive(Endpoint ep, String type, byte[] payload, Executor executor) { |
pier | 3daed98 | 2020-02-19 20:44:23 +0100 | [diff] [blame] | 61 | return sendAndReceive(ep, type, payload, null, executor); |
Jordan Halterman | 1cf4f84 | 2019-05-21 11:27:50 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Sends a message asynchronously and expects a response. |
| 66 | * @param ep end point to send the message to. |
| 67 | * @param type type of message. |
| 68 | * @param payload message payload. |
| 69 | * @param timeout operation timeout |
| 70 | * @return a response future |
| 71 | */ |
| 72 | default CompletableFuture<byte[]> sendAndReceive(Endpoint ep, String type, byte[] payload, Duration timeout) { |
| 73 | return sendAndReceive(ep, type, payload, timeout, MoreExecutors.directExecutor()); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Sends a message synchronously and expects a response. |
| 78 | * @param ep end point to send the message to. |
| 79 | * @param type type of message. |
| 80 | * @param payload message payload. |
| 81 | * @param executor executor over which any follow up actions after completion will be executed. |
| 82 | * @param timeout operation timeout |
| 83 | * @return a response future |
| 84 | */ |
| 85 | CompletableFuture<byte[]> sendAndReceive(Endpoint ep, String type, byte[] payload, Duration timeout, |
| 86 | Executor executor); |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * Registers a new message handler for message type. |
| 90 | * @param type message type. |
| 91 | * @param handler message handler |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 92 | * @param executor executor to use for running message handler logic. |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 93 | */ |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 94 | void registerHandler(String type, BiConsumer<Endpoint, byte[]> handler, Executor executor); |
| 95 | |
| 96 | /** |
| 97 | * Registers a new message handler for message type. |
| 98 | * @param type message type. |
| 99 | * @param handler message handler |
| 100 | * @param executor executor to use for running message handler logic. |
| 101 | */ |
| 102 | void registerHandler(String type, BiFunction<Endpoint, byte[], byte[]> handler, Executor executor); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 103 | |
| 104 | /** |
Madan Jampani | 27b69c6 | 2015-05-15 15:49:02 -0700 | [diff] [blame] | 105 | * Registers a new message handler for message type. |
| 106 | * @param type message type. |
| 107 | * @param handler message handler |
| 108 | */ |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 109 | void registerHandler(String type, BiFunction<Endpoint, byte[], CompletableFuture<byte[]>> handler); |
Madan Jampani | 27b69c6 | 2015-05-15 15:49:02 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 112 | * Unregister current handler, if one exists for message type. |
| 113 | * @param type message type |
| 114 | */ |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 115 | void unregisterHandler(String type); |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 116 | } |