blob: ebbe615467f332dc3c2a9d597f9af25fca687e4c [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
Madan Jampanic26eede2015-04-16 11:42:16 -070016package org.onosproject.store.cluster.messaging;
Madan Jampaniab6d3112014-10-02 16:30:14 -070017
Jordan Halterman43300782019-05-21 11:27:50 -070018import java.time.Duration;
Madan Jampani2bfa94c2015-04-11 05:03:49 -070019import java.util.concurrent.CompletableFuture;
Madan Jampaniec5ae342015-04-13 15:43:10 -070020import java.util.concurrent.Executor;
Madan Jampanid36def02016-01-13 11:21:56 -080021import java.util.function.BiConsumer;
22import java.util.function.BiFunction;
Madan Jampaniab6d3112014-10-02 16:30:14 -070023
Jordan Halterman43300782019-05-21 11:27:50 -070024import com.google.common.util.concurrent.MoreExecutors;
25
Madan Jampaniab6d3112014-10-02 16:30:14 -070026/**
27 * Interface for low level messaging primitives.
28 */
29public interface MessagingService {
Madan Jampanic26eede2015-04-16 11:42:16 -070030
Madan Jampaniab6d3112014-10-02 16:30:14 -070031 /**
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 Jampani53e44e62014-10-07 12:39:51 -070036 * @param payload message payload bytes.
Madan Jampani175e8fd2015-05-20 14:10:45 -070037 * @return future that is completed when the message is sent
Madan Jampaniab6d3112014-10-02 16:30:14 -070038 */
Madan Jampani175e8fd2015-05-20 14:10:45 -070039 CompletableFuture<Void> sendAsync(Endpoint ep, String type, byte[] payload);
Madan Jampaniab6d3112014-10-02 16:30:14 -070040
41 /**
Madan Jampanid36def02016-01-13 11:21:56 -080042 * Sends a message asynchronously and expects a response.
Madan Jampaniab6d3112014-10-02 16:30:14 -070043 * @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 Jampaniab6d3112014-10-02 16:30:14 -070047 */
Jordan Halterman43300782019-05-21 11:27:50 -070048 default CompletableFuture<byte[]> sendAndReceive(Endpoint ep, String type, byte[] payload) {
pier93340912020-02-19 20:44:23 +010049 return sendAndReceive(ep, type, payload, null, MoreExecutors.directExecutor());
Jordan Halterman43300782019-05-21 11:27:50 -070050 }
Madan Jampaniab6d3112014-10-02 16:30:14 -070051
52 /**
Madan Jampanid36def02016-01-13 11:21:56 -080053 * 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 Jampaniab6d3112014-10-02 16:30:14 -070059 */
Jordan Halterman43300782019-05-21 11:27:50 -070060 default CompletableFuture<byte[]> sendAndReceive(Endpoint ep, String type, byte[] payload, Executor executor) {
pier93340912020-02-19 20:44:23 +010061 return sendAndReceive(ep, type, payload, null, executor);
Jordan Halterman43300782019-05-21 11:27:50 -070062 }
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 Jampani2af244a2015-02-22 13:12:01 -080087
88 /**
89 * Registers a new message handler for message type.
90 * @param type message type.
91 * @param handler message handler
Madan Jampanic26eede2015-04-16 11:42:16 -070092 * @param executor executor to use for running message handler logic.
Madan Jampani2af244a2015-02-22 13:12:01 -080093 */
Madan Jampanid36def02016-01-13 11:21:56 -080094 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 Jampaniab6d3112014-10-02 16:30:14 -0700103
104 /**
Madan Jampani27b69c62015-05-15 15:49:02 -0700105 * Registers a new message handler for message type.
106 * @param type message type.
107 * @param handler message handler
108 */
Madan Jampanid36def02016-01-13 11:21:56 -0800109 void registerHandler(String type, BiFunction<Endpoint, byte[], CompletableFuture<byte[]>> handler);
Madan Jampani27b69c62015-05-15 15:49:02 -0700110
111 /**
Madan Jampaniab6d3112014-10-02 16:30:14 -0700112 * Unregister current handler, if one exists for message type.
113 * @param type message type
114 */
Madan Jampanic26eede2015-04-16 11:42:16 -0700115 void unregisterHandler(String type);
Brian O'Connorabafb502014-12-02 22:26:20 -0800116}