-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathEventLoop.java
More file actions
173 lines (145 loc) · 5.9 KB
/
EventLoop.java
File metadata and controls
173 lines (145 loc) · 5.9 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
//
// 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.loop;
import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.msgpack.MessagePack;
import org.msgpack.rpc.Session;
import org.msgpack.rpc.Server;
import org.msgpack.rpc.transport.ClientTransport;
import org.msgpack.rpc.transport.ServerTransport;
import org.msgpack.rpc.config.ClientConfig;
import org.msgpack.rpc.config.ServerConfig;
import org.msgpack.rpc.config.TcpClientConfig;
import org.msgpack.rpc.config.TcpServerConfig;
import org.msgpack.rpc.loop.netty.NettyEventLoopFactory;
public abstract class EventLoop {
static private EventLoopFactory loopFactory;
static private EventLoop defaultEventLoop;
static public synchronized void setFactory(EventLoopFactory factory) {
loopFactory = factory;
}
static private synchronized EventLoopFactory getFactory() {
if (loopFactory == null) {
loopFactory = new NettyEventLoopFactory();
}
return loopFactory;
}
static public synchronized void setDefaultEventLoop(EventLoop eventLoop) {
defaultEventLoop = eventLoop;
}
static public synchronized EventLoop defaultEventLoop() {
if (defaultEventLoop == null) {
defaultEventLoop = start();
}
return defaultEventLoop;
}
static public EventLoop start() {
return start(Executors.newCachedThreadPool());
}
static public EventLoop start(MessagePack messagePack) {
return start(Executors.newCachedThreadPool(),
Executors.newCachedThreadPool(),
Executors.newScheduledThreadPool(2), messagePack);
}
static public EventLoop start(ExecutorService workerExecutor) {
return start(workerExecutor, Executors.newCachedThreadPool());
}
static public EventLoop start(ExecutorService workerExecutor, ExecutorService ioExecutor) {
return start(workerExecutor, ioExecutor,
Executors.newScheduledThreadPool(2), new MessagePack());
}
static public EventLoop start(
ExecutorService workerExecutor, ExecutorService ioExecutor,
ScheduledExecutorService scheduledExecutor, MessagePack messagePack) {
return start(workerExecutor, ioExecutor, scheduledExecutor, messagePack,
2 * Runtime.getRuntime().availableProcessors());
}
static public EventLoop start(
ExecutorService workerExecutor, ExecutorService ioExecutor,
ScheduledExecutorService scheduledExecutor, MessagePack messagePack, int workerCount) {
return getFactory().make(workerExecutor, ioExecutor, scheduledExecutor, messagePack, workerCount);
}
private ExecutorService workerExecutor;
private ExecutorService ioExecutor;
private ScheduledExecutorService scheduledExecutor;
private MessagePack messagePack;
private int workerCount;
public MessagePack getMessagePack() {
return messagePack;
}
public void setMessagePack(MessagePack messagePack) {
this.messagePack = messagePack;
}
public EventLoop(ExecutorService workerExecutor, ExecutorService ioExecutor,
ScheduledExecutorService scheduledExecutor, MessagePack messagePack, int workerCount) {
this.workerExecutor = workerExecutor;
this.scheduledExecutor = scheduledExecutor;
this.ioExecutor = ioExecutor;
this.messagePack = messagePack;
this.workerCount = workerCount;
}
public ExecutorService getWorkerExecutor() {
return workerExecutor;
}
public ExecutorService getIoExecutor() {
return ioExecutor;
}
public ScheduledExecutorService getScheduledExecutor() {
return scheduledExecutor;
}
public int getWorkerCount() {
return workerCount;
}
public void shutdown() {
scheduledExecutor.shutdown();
ioExecutor.shutdown();
workerExecutor.shutdown();
}
public void join() throws InterruptedException {
// FIXME?
scheduledExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
ioExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
workerExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
}
// FIXME join? close?
public ClientTransport openTransport(ClientConfig config, Session session) {
if (config instanceof TcpClientConfig) {
return openTcpTransport((TcpClientConfig) config, session);
}
// FIXME exception
throw new RuntimeException("Unknown transport config: "
+ config.getClass().getName());
}
public ServerTransport listenTransport(ServerConfig config, Server server)
throws IOException {
if (config instanceof TcpServerConfig) {
return listenTcpTransport((TcpServerConfig) config, server);
}
// FIXME exception
throw new RuntimeException("Unknown transport config: "
+ config.getClass().getName());
}
protected abstract ClientTransport openTcpTransport(TcpClientConfig config,
Session session);
protected abstract ServerTransport listenTcpTransport(
TcpServerConfig config, Server server);
}