This repository has been archived by the owner on Sep 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 354
/
ServerMain.java
159 lines (133 loc) · 6.48 KB
/
ServerMain.java
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
package com.cloudhopper.smpp.demo;
/*
* #%L
* ch-smpp
* %%
* Copyright (C) 2009 - 2015 Cloudhopper by Twitter
* %%
* 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.
* #L%
*/
import com.cloudhopper.smpp.SmppServerConfiguration;
import com.cloudhopper.smpp.SmppServerHandler;
import com.cloudhopper.smpp.SmppServerSession;
import com.cloudhopper.smpp.SmppSession;
import com.cloudhopper.smpp.SmppSessionConfiguration;
import com.cloudhopper.smpp.impl.DefaultSmppServer;
import com.cloudhopper.smpp.impl.DefaultSmppSessionHandler;
import com.cloudhopper.smpp.pdu.BaseBind;
import com.cloudhopper.smpp.pdu.BaseBindResp;
import com.cloudhopper.smpp.pdu.BaseSm;
import com.cloudhopper.smpp.pdu.PduRequest;
import com.cloudhopper.smpp.pdu.PduResponse;
import com.cloudhopper.smpp.type.SmppProcessingException;
import java.lang.ref.WeakReference;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author joelauer (twitter: @jjlauer or <a href="http://twitter.com/jjlauer" target=window>http://twitter.com/jjlauer</a>)
*/
public class ServerMain {
private static final Logger logger = LoggerFactory.getLogger(ServerMain.class);
static public void main(String[] args) throws Exception {
//
// setup 3 things required for a server
//
// for monitoring thread use, it's preferable to create your own instance
// of an executor and cast it to a ThreadPoolExecutor from Executors.newCachedThreadPool()
// this permits exposing things like executor.getActiveCount() via JMX possible
// no point renaming the threads in a factory since underlying Netty
// framework does not easily allow you to customize your thread names
ThreadPoolExecutor executor = (ThreadPoolExecutor)Executors.newCachedThreadPool();
// to enable automatic expiration of requests, a second scheduled executor
// is required which is what a monitor task will be executed with - this
// is probably a thread pool that can be shared with between all client bootstraps
ScheduledThreadPoolExecutor monitorExecutor = (ScheduledThreadPoolExecutor)Executors.newScheduledThreadPool(1, new ThreadFactory() {
private AtomicInteger sequence = new AtomicInteger(0);
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("SmppServerSessionWindowMonitorPool-" + sequence.getAndIncrement());
return t;
}
});
// create a server configuration
SmppServerConfiguration configuration = new SmppServerConfiguration();
configuration.setPort(2776);
configuration.setMaxConnectionSize(10);
configuration.setNonBlockingSocketsEnabled(true);
configuration.setDefaultRequestExpiryTimeout(30000);
configuration.setDefaultWindowMonitorInterval(15000);
configuration.setDefaultWindowSize(5);
configuration.setDefaultWindowWaitTimeout(configuration.getDefaultRequestExpiryTimeout());
configuration.setDefaultSessionCountersEnabled(true);
configuration.setJmxEnabled(true);
// create a server, start it up
DefaultSmppServer smppServer = new DefaultSmppServer(configuration, new DefaultSmppServerHandler(), executor, monitorExecutor);
logger.info("Starting SMPP server...");
smppServer.start();
logger.info("SMPP server started");
System.out.println("Press any key to stop server");
System.in.read();
logger.info("Stopping SMPP server...");
smppServer.stop();
logger.info("SMPP server stopped");
logger.info("Server counters: {}", smppServer.getCounters());
}
public static class DefaultSmppServerHandler implements SmppServerHandler {
@Override
public void sessionBindRequested(Long sessionId, SmppSessionConfiguration sessionConfiguration, final BaseBind bindRequest) throws SmppProcessingException {
// test name change of sessions
// this name actually shows up as thread context....
sessionConfiguration.setName("Application.SMPP." + sessionConfiguration.getSystemId());
//throw new SmppProcessingException(SmppConstants.STATUS_BINDFAIL, null);
}
@Override
public void sessionCreated(Long sessionId, SmppServerSession session, BaseBindResp preparedBindResponse) throws SmppProcessingException {
logger.info("Session created: {}", session);
// need to do something it now (flag we're ready)
session.serverReady(new TestSmppSessionHandler(session));
}
@Override
public void sessionDestroyed(Long sessionId, SmppServerSession session) {
logger.info("Session destroyed: {}", session);
// print out final stats
if (session.hasCounters()) {
logger.info(" final session rx-submitSM: {}", session.getCounters().getRxSubmitSM());
}
// make sure it's really shutdown
session.destroy();
}
}
public static class TestSmppSessionHandler extends DefaultSmppSessionHandler {
private WeakReference<SmppSession> sessionRef;
public TestSmppSessionHandler(SmppSession session) {
this.sessionRef = new WeakReference<SmppSession>(session);
}
@Override
public PduResponse firePduRequestReceived(PduRequest pduRequest) {
SmppSession session = sessionRef.get();
// mimic how long processing could take on a slower smsc
try {
//Thread.sleep(50);
} catch (Exception e) { }
return pduRequest.createResponse();
}
}
}