Skip to content

Commit 28b682c

Browse files
committed
Add concept of caller context and message serilizer to messaging layer
1 parent 3b668d2 commit 28b682c

18 files changed

Lines changed: 171 additions & 75 deletions

‎framework/ipc/src/org/apache/cloudstack/framework/messaging/ComponentEndpoint.java‎

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
*/
1919
package org.apache.cloudstack.framework.messaging;
2020

21-
public class ComponentEndpoint implements RpcEndpoint, TransportMultiplexier, Subscriber {
22-
21+
public class ComponentEndpoint implements RpcEndpoint, Subscriber {
2322
private TransportEndpoint transportEndpoint;
2423
private RpcProvider rpcProvider;
2524

@@ -42,44 +41,33 @@ public void setRpcProvider(RpcProvider rpcProvider) {
4241
this.rpcProvider = rpcProvider;
4342
}
4443

45-
public void initialize(String[] multiplexiers) {
46-
if(multiplexiers != null) {
47-
for(String name : multiplexiers)
48-
transportEndpoint.registerMultiplexier(name, this);
49-
}
50-
44+
public void initialize() {
5145
rpcProvider.registerRpcEndpoint(this);
5246
}
5347

54-
@Override
55-
public void onTransportMessage(String senderEndpointAddress,
56-
String targetEndpointAddress, String multiplexer, String message) {
57-
}
58-
59-
@Override
60-
public String call(String targetAddress, String rpcMessage)
48+
// it will throw RpcRuntimeException in case of transport
49+
public String call(RpcCallContext callContext, String targetAddress, String command, Object cmdArg)
6150
{
62-
return null;
51+
return rpcProvider.call(this, callContext, targetAddress, command, cmdArg);
6352
}
6453

65-
@Override
66-
public RpcCall asyncCall(String targetAddress, String rpcMessage) {
67-
return null;
54+
public RpcClientCall asyncCall(RpcCallContext callContext, String targetAddress, String command, Object cmdArg) {
55+
return rpcProvider.asyncCall(this, callContext, targetAddress, command, cmdArg);
6856
}
6957

7058
@Override
71-
public void onCallReceive(RpcCall call) {
59+
public void onCallReceive(RpcServerCall call) {
7260
// TODO Auto-generated method stub
7361
// implement annotation based call dispatching
7462
}
7563

7664
@Override
77-
public void onCallReturn(RpcCall call, String rpcReturnMessage, RpcException e) {
65+
public void onCallReturn(RpcClientCall call, Object returnObject, RpcException e) {
7866
// ???
7967
}
8068

8169
@Override
82-
public void onPublishEvent(String subject, String senderAddress, String args) {
70+
public void onPublishEvent(String subject, String senderAddress, Object args) {
8371
// TODO
8472
}
8573
}

‎framework/ipc/src/org/apache/cloudstack/framework/messaging/EventBus.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
package org.apache.cloudstack.framework.messaging;
1919

2020
public interface EventBus {
21+
void setMessageSerializer(MessageSerializer messageSerializer);
22+
MessageSerializer getMessageSerializer();
23+
2124
void subscribe(String subject, Subscriber subscriber);
2225
void unsubscribe(String subject, Subscriber subscriber);
2326

24-
void publish(String subject, PublishScope scope, String senderAddress, String args);
27+
void publish(String senderAddress, String subject, PublishScope scope, Object args);
2528
}

‎framework/ipc/src/org/apache/cloudstack/framework/messaging/EventBusBase.java‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
import java.util.List;
2525
import java.util.Map;
2626

27-
2827
public class EventBusBase implements EventBus {
2928

3029
private Gate _gate;
3130
private List<ActionRecord> _pendingActions;
3231

3332
private SubscriptionNode _subscriberRoot;
33+
private MessageSerializer _messageSerializer;
3434

3535
public EventBusBase() {
3636
_gate = new Gate();
@@ -39,6 +39,16 @@ public EventBusBase() {
3939
_subscriberRoot = new SubscriptionNode("/", null);
4040
}
4141

42+
@Override
43+
public void setMessageSerializer(MessageSerializer messageSerializer) {
44+
_messageSerializer = messageSerializer;
45+
}
46+
47+
@Override
48+
public MessageSerializer getMessageSerializer() {
49+
return _messageSerializer;
50+
}
51+
4252
@Override
4353
public void subscribe(String subject, Subscriber subscriber) {
4454
assert(subject != null);
@@ -71,20 +81,20 @@ public void unsubscribe(String subject, Subscriber subscriber) {
7181
}
7282

7383
@Override
74-
public void publish(String subject, PublishScope scope, String senderAddress,
75-
String args) {
84+
public void publish(String senderAddress, String subject, PublishScope scope,
85+
Object args) {
7686

7787
if(_gate.enter(true)) {
7888

7989
List<SubscriptionNode> chainFromTop = new ArrayList<SubscriptionNode>();
8090
SubscriptionNode current = locate(subject, chainFromTop, false);
8191

8292
if(current != null)
83-
current.notifySubscribers(subject, senderAddress, args);
93+
current.notifySubscribers(senderAddress, subject, args);
8494

8595
Collections.reverse(chainFromTop);
8696
for(SubscriptionNode node : chainFromTop)
87-
node.notifySubscribers(subject, senderAddress, args);
97+
node.notifySubscribers(senderAddress, subject, args);
8898

8999
_gate.leave();
90100
}
@@ -283,9 +293,9 @@ public void addChild(String key, SubscriptionNode childNode) {
283293
_children.put(key, childNode);
284294
}
285295

286-
public void notifySubscribers(String subject, String senderAddress, String args) {
296+
public void notifySubscribers(String senderAddress, String subject, Object args) {
287297
for(Subscriber subscriber : _subscribers) {
288-
subscriber.onPublishEvent(subject, senderAddress, args);
298+
subscriber.onPublishEvent(senderAddress, subject, args);
289299
}
290300
}
291301
}

‎framework/ipc/src/org/apache/cloudstack/framework/messaging/EventHandler.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
117
package org.apache.cloudstack.framework.messaging;
218

319
import java.lang.annotation.ElementType;

framework/ipc/src/org/apache/cloudstack/framework/messaging/TransportAddressFactory.java renamed to framework/ipc/src/org/apache/cloudstack/framework/messaging/MessageSerializer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.apache.cloudstack.framework.messaging;
2020

21-
public interface TransportAddressFactory {
22-
String createServiceAddress(String serviceProvider);
21+
public interface MessageSerializer {
22+
String serializeTo(Object object);
23+
Object serializeFrom(String message);
2324
}

‎framework/ipc/src/org/apache/cloudstack/framework/messaging/PublishScope.java‎

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,6 @@
1717

1818
package org.apache.cloudstack.framework.messaging;
1919

20-
public class PublishScope {
21-
public enum Type { SINGLE, LOCAL, GLOBAL };
22-
23-
Type scope;
24-
String address;
25-
26-
public PublishScope(Type scope) {
27-
this.scope = scope;
28-
}
29-
30-
public PublishScope(String address) {
31-
scope = Type.SINGLE;
32-
this.address = address;
33-
}
34-
35-
public Type getType() {
36-
return scope;
37-
}
38-
39-
public String getAddress() {
40-
return this.address;
41-
}
20+
public enum PublishScope {
21+
LOCAL, GLOBAL
4222
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.framework.messaging;
18+
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
public class RpcCallContext {
23+
private final static int DEFAULT_RPC_TIMEOUT = 10000;
24+
25+
Map<String, Object> _contextMap = new HashMap<String, Object>();
26+
int _timeoutMilliSeconds = DEFAULT_RPC_TIMEOUT;
27+
28+
public RpcCallContext() {
29+
}
30+
31+
public int getTimeoutMilliSeconds() {
32+
return _timeoutMilliSeconds;
33+
}
34+
35+
public void setTimeoutMilliSeconds(int timeoutMilliseconds) {
36+
_timeoutMilliSeconds = timeoutMilliseconds;
37+
}
38+
39+
@SuppressWarnings("unchecked")
40+
public <T> T getContextParameter(String key) {
41+
return (T)_contextMap.get(key);
42+
}
43+
44+
public void setContextParameter(String key, Object object) {
45+
_contextMap.put(key, object);
46+
}
47+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.framework.messaging;
18+
19+
public interface RpcClientCall {
20+
String getCommand();
21+
Object getCommandArgument();
22+
RpcCallContext getCallContext();
23+
24+
void cancel();
25+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.apache.cloudstack.framework.messaging;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.METHOD)
10+
public @interface RpcClientCallHandler {
11+
String command();
12+
}

‎framework/ipc/src/org/apache/cloudstack/framework/messaging/RpcEndpoint.java‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
package org.apache.cloudstack.framework.messaging;
2020

2121
public interface RpcEndpoint {
22-
String call(String targetAddress, String rpcMessage);
23-
RpcCall asyncCall(String targetAddress, String rpcMessage);
24-
25-
void onCallReceive(RpcCall call);
26-
void onCallReturn(RpcCall call, String rpcReturnMessage, RpcException e);
22+
void onCallReceive(RpcServerCall call);
23+
void onCallReturn(RpcClientCall call, Object returnObject, RpcException e);
2724
}

0 commit comments

Comments
 (0)