-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathSession.java
More file actions
186 lines (164 loc) · 5.72 KB
/
Session.java
File metadata and controls
186 lines (164 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
// MessagePack-RPC for Java
//
// Copyright (C) 2010 FURUHASHI Sadayuki
//
// 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 org.msgpack.rpc;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import org.msgpack.rpc.address.Address;
import org.msgpack.rpc.message.RequestMessage;
import org.msgpack.rpc.message.NotifyMessage;
import org.msgpack.rpc.reflect.Reflect;
import org.msgpack.rpc.transport.ClientTransport;
import org.msgpack.rpc.config.ClientConfig;
import org.msgpack.rpc.loop.EventLoop;
import org.msgpack.type.Value;
import org.msgpack.type.ValueFactory;
public class Session {
protected Address address;
protected EventLoop loop;
private ClientTransport transport;
private Reflect reflect;
private int requestTimeout;
private AtomicInteger seqid = new AtomicInteger(0); // FIXME rand()?
private Map<Integer, FutureImpl> reqtable = new HashMap<Integer, FutureImpl>();
Session(Address address, ClientConfig config, EventLoop loop) {
this(address, config, loop, new Reflect(loop.getMessagePack()));
}
Session(Address address, ClientConfig config, EventLoop loop, Reflect reflect) {
this.address = address;
this.loop = loop;
this.requestTimeout = config.getRequestTimeout();
this.transport = loop.openTransport(config, this);
this.reflect = reflect;
}
public <T> T proxy(Class<T> iface) {
return reflect.getProxy(iface).newProxyInstance(this);
// Reflect.reflectProxy(iface,loop.getMessagePack()).newProxyInstance(this);
}
public Address getAddress() {
return address;
}
// FIXME EventLoopHolder interface?
public EventLoop getEventLoop() {
return loop;
}
/**
* Timeout seconds
* @return
*/
public int getRequestTimeout() {
return requestTimeout;
}
public void setRequestTimeout(int requestTimeout) {
this.requestTimeout = requestTimeout;
}
public Value callApply(String method, Object[] args) {
Future<Value> f = sendRequest(method, args);
while (true) {
try {
if (requestTimeout <= 0) {
return f.get();
} else {
return f.get(requestTimeout, TimeUnit.SECONDS);
}
} catch (InterruptedException e) {
// FIXME
} catch (TimeoutException e) {
// FIXME
throw new RuntimeException("Time out to call method:" + method, e);
}
}
}
public Future<Value> callAsyncApply(String method, Object[] args) {
return sendRequest(method, args);
}
public void notifyApply(String method, Object[] args) {
sendNotify(method, args);
}
public Future<Value> sendRequest(String method, Object[] args) {
int msgid = seqid.getAndAdd(1);
RequestMessage msg = new RequestMessage(msgid, method, args);
FutureImpl f = new FutureImpl(this);
synchronized (reqtable) {
reqtable.put(msgid, f);
}
transport.sendMessage(msg);
return new Future<Value>(loop.getMessagePack(), f);
}
public void sendNotify(String method, Object[] args) {
NotifyMessage msg = new NotifyMessage(method, args);
transport.sendMessage(msg);
}
void closeSession() {
transport.close();
synchronized (reqtable) {
for (Map.Entry<Integer, FutureImpl> pair : reqtable.entrySet()) {
// FIXME error result
FutureImpl f = pair.getValue();
f.setResult(null, ValueFactory.createRawValue("session closed"));
}
reqtable.clear();
}
}
public void transportConnectFailed() { // FIXME error rseult
/*
synchronized(reqtable) {
for(Map.Entry<Integer,FutureImpl> pair : reqtable.entrySet()) {
// FIXME
FutureImpl f = pair.getValue();
f.setResult(null,null);
}
reqtable.clear();
}
*/
}
public void onResponse(int msgid, Value result, Value error) {
FutureImpl f;
synchronized (reqtable) {
f = reqtable.remove(msgid);
}
if (f == null) {
// FIXME log
return;
}
f.setResult(result, error);
}
void stepTimeout() {
List<FutureImpl> timedout = new ArrayList<FutureImpl>();
synchronized (reqtable) {
for (Iterator<Map.Entry<Integer, FutureImpl>> it = reqtable
.entrySet().iterator(); it.hasNext();) {
Map.Entry<Integer, FutureImpl> pair = it.next();
FutureImpl f = pair.getValue();
if (f.stepTimeout()) {
it.remove();
timedout.add(f);
}
}
}
for (FutureImpl f : timedout) {
// FIXME error result
f.setResult(null, ValueFactory.createRawValue("timedout"));
}
}
}