use of com.tvd12.ezyfoxserver.setting.EzySimpleZoneSetting in project ezyfox-server by youngmonkeys.
the class EzyLoginProcessorTest method processUserSessionsMaxSessionPerUserGreaterThan1.
@SuppressWarnings("rawtypes")
@Test
public void processUserSessionsMaxSessionPerUserGreaterThan1() {
// given
EzySimpleZoneSetting zoneSetting = new EzySimpleZoneSetting();
zoneSetting.getUserManagement().setMaxSessionPerUser(2);
EzySimpleZone zone = new EzySimpleZone();
zone.setSetting(zoneSetting);
EzyZoneContext zoneContext = mock(EzyZoneContext.class);
when(zoneContext.getZone()).thenReturn(zone);
EzySimpleServer server = new EzySimpleServer();
EzySessionManager sessionManager = mock(EzySessionManager.class);
server.setSessionManager(sessionManager);
EzyZoneUserManager userManager = EzyZoneUserManagerImpl.builder().build();
EzyUser user = mock(EzyUser.class);
when(user.getName()).thenReturn("monkey");
when(user.getSessionCount()).thenReturn(2);
userManager.addUser(user);
zone.setUserManager(userManager);
EzyStatistics statistics = mock(EzyStatistics.class);
EzyUserStatistics userStatistics = mock(EzyUserStatistics.class);
when(statistics.getUserStats()).thenReturn(userStatistics);
server.setStatistics(statistics);
EzyServerContext serverContext = mock(EzyServerContext.class);
when(serverContext.getServer()).thenReturn(server);
EzyLoginProcessor sut = new EzyLoginProcessor(serverContext);
EzySessionDelegate sessionDelegate = mock(EzySessionDelegate.class);
EzyAbstractSession session = spy(EzyAbstractSession.class);
session.setDelegate(sessionDelegate);
EzyUserLoginEvent event = mock(EzyUserLoginEvent.class);
when(event.getUsername()).thenReturn("monkey");
when(event.getSession()).thenReturn(session);
// when
Throwable e = Asserts.assertThrows(() -> sut.apply(zoneContext, event));
// then
Asserts.assertEquals(EzyLoginErrorException.class, e.getClass());
}
use of com.tvd12.ezyfoxserver.setting.EzySimpleZoneSetting in project ezyfox-server by youngmonkeys.
the class EzyLoginProcessorTest method processUserSessionsOk.
@SuppressWarnings("rawtypes")
@Test
public void processUserSessionsOk() {
// given
EzySimpleZoneSetting zoneSetting = new EzySimpleZoneSetting();
zoneSetting.getUserManagement().setMaxSessionPerUser(1);
zoneSetting.getUserManagement().setAllowChangeSession(true);
EzySimpleZone zone = new EzySimpleZone();
zone.setSetting(zoneSetting);
EzyZoneContext zoneContext = mock(EzyZoneContext.class);
when(zoneContext.getZone()).thenReturn(zone);
EzySimpleServer server = new EzySimpleServer();
EzySessionManager sessionManager = mock(EzySessionManager.class);
server.setSessionManager(sessionManager);
EzyZoneUserManager userManager = EzyZoneUserManagerImpl.builder().build();
EzyUser user = mock(EzyUser.class);
when(user.getName()).thenReturn("monkey");
when(user.getSessionCount()).thenReturn(1);
userManager.addUser(user);
zone.setUserManager(userManager);
EzyStatistics statistics = mock(EzyStatistics.class);
EzyUserStatistics userStatistics = mock(EzyUserStatistics.class);
when(statistics.getUserStats()).thenReturn(userStatistics);
server.setStatistics(statistics);
EzyServerContext serverContext = mock(EzyServerContext.class);
when(serverContext.getServer()).thenReturn(server);
EzyLoginProcessor sut = new EzyLoginProcessor(serverContext);
EzySessionDelegate sessionDelegate = mock(EzySessionDelegate.class);
EzyAbstractSession session = spy(EzyAbstractSession.class);
session.setDelegate(sessionDelegate);
EzyUserLoginEvent event = mock(EzyUserLoginEvent.class);
when(event.getUsername()).thenReturn("monkey");
when(event.getSession()).thenReturn(session);
// when
sut.apply(zoneContext, event);
// then
Asserts.assertTrue(session.isLoggedIn());
}
use of com.tvd12.ezyfoxserver.setting.EzySimpleZoneSetting in project ezyfox-server by youngmonkeys.
the class EzySettingsBuilderTest method test.
@Test
public void test() {
EzySimpleStreamingSetting streamingSetting = new EzySimpleStreamingSetting();
EzySimpleHttpSetting httpSetting = new EzySimpleHttpSetting();
EzySimpleSocketSetting socketSetting = new EzySocketSettingBuilder().active(true).address("1.1.1.1").codecCreator(TestCodecCreator.class).maxRequestSize(1024).port(12345).tcpNoDelay(true).writerThreadPoolSize(3).build();
EzySimpleUdpSetting udpSetting = new EzyUdpSettingBuilder().active(true).address("2.2.2.2").channelPoolSize(3).codecCreator(TestCodecCreator.class).handlerThreadPoolSize(3).maxRequestSize(2048).port(23456).build();
EzySimpleSslConfigSetting sslConfigSetting = new EzySimpleSslConfigSetting();
EzySimpleWebSocketSetting webSocketSetting = new EzyWebSocketSettingBuilder().active(true).address("1.1.1.1").codecCreator(TestCodecCreator.class).maxFrameSize(1024).port(12345).sslActive(true).sslConfig(sslConfigSetting).sslPort(23456).writerThreadPoolSize(3).build();
EzySimpleAdminsSetting adminsSetting = new EzySimpleAdminsSetting();
EzySimpleAdminSetting adminSetting = new EzyAdminSettingBuilder().accessToken("123").username("admin").password("123456").build();
EzySimpleLoggerSetting loggerSetting = new EzySimpleLoggerSetting();
EzySimpleThreadPoolSizeSetting threadPoolSizeSetting = new EzyThreadPoolSizeSettingBuilder().socketDataReceiver(1).extensionRequestHandler(2).socketDisconnectionHandler(3).socketUserRemovalHandler(4).statistics(5).streamHandler(6).systemRequestHandler(7).build();
EzySimpleMaxRequestPerSecond maxRequestPerSecond = new EzyMaxRequestPerSecondBuilder().value(15).action(EzyMaxRequestPerSecondAction.DROP_REQUEST).build();
EzySimpleSessionManagementSetting sessionManagementSetting = new EzySessionManagementSettingBuilder().sessionMaxIdleTimeInSecond(100).sessionMaxRequestPerSecond(maxRequestPerSecond).sessionMaxWaitingTimeInSecond(200).build();
EzySimpleEventControllersSetting eventControllersSetting = new EzySimpleEventControllersSetting();
EzySimpleZonesSetting zonesSetting = new EzySimpleZonesSetting();
EzySimpleZoneSetting zoneSetting = new EzyZoneSettingBuilder().name("test").build();
EzySimpleSettings settings = new EzySettingsBuilder().debug(true).nodeName("test").maxSessions(100).socket(socketSetting).udp(udpSetting).streaming(streamingSetting).http(httpSetting).websocket(webSocketSetting).admins(adminsSetting).admin(adminSetting).logger(loggerSetting).threadPoolSize(threadPoolSizeSetting).sessionManagement(sessionManagementSetting).eventControllers(eventControllersSetting).zones(zonesSetting).zone(zoneSetting).addEventController(EzyEventType.SERVER_INITIALIZING, HelloServerInitializingReadyController.class).build();
assertTrue(settings.isDebug());
assertEquals(settings.getNodeName(), "test");
assertEquals(settings.getMaxSessions(), 100);
assertEquals(settings.getStreaming(), streamingSetting);
assertEquals(settings.getHttp(), httpSetting);
socketSetting = settings.getSocket();
assertTrue(socketSetting.isActive());
assertEquals(socketSetting.getAddress(), "1.1.1.1");
assertEquals(socketSetting.getCodecCreator(), TestCodecCreator.class.getName());
assertEquals(socketSetting.getMaxRequestSize(), 1024);
assertEquals(socketSetting.getPort(), 12345);
assertTrue(socketSetting.isTcpNoDelay());
assertEquals(socketSetting.getWriterThreadPoolSize(), 3);
udpSetting = settings.getUdp();
assertTrue(udpSetting.isActive());
assertEquals(udpSetting.getAddress(), "2.2.2.2");
assertEquals(udpSetting.getCodecCreator(), TestCodecCreator.class.getName());
assertEquals(udpSetting.getMaxRequestSize(), 2048);
assertEquals(udpSetting.getPort(), 23456);
assertEquals(udpSetting.getChannelPoolSize(), 3);
assertEquals(udpSetting.getHandlerThreadPoolSize(), 3);
webSocketSetting = settings.getWebsocket();
assertTrue(webSocketSetting.isActive());
assertEquals(webSocketSetting.getAddress(), "1.1.1.1");
assertEquals(webSocketSetting.getCodecCreator(), TestCodecCreator.class.getName());
assertEquals(webSocketSetting.getMaxFrameSize(), 1024);
assertEquals(webSocketSetting.getPort(), 12345);
assertTrue(webSocketSetting.isSslActive());
assertEquals(webSocketSetting.getSslConfig(), sslConfigSetting);
assertEquals(webSocketSetting.getSslPort(), 23456);
assertEquals(webSocketSetting.getWriterThreadPoolSize(), 3);
assertEquals(settings.getAdmins(), adminsSetting);
adminSetting = (EzySimpleAdminSetting) settings.getAdmins().getAdminByName("admin");
assertEquals(adminSetting.getAccessToken(), "123");
assertEquals(adminSetting.getUsername(), "admin");
assertEquals(adminSetting.getPassword(), "123456");
assertEquals(settings.getLogger(), loggerSetting);
threadPoolSizeSetting = settings.getThreadPoolSize();
assertEquals(threadPoolSizeSetting.getSocketDataReceiver(), 1);
assertEquals(threadPoolSizeSetting.getExtensionRequestHandler(), 2);
assertEquals(threadPoolSizeSetting.getSocketDisconnectionHandler(), 3);
assertEquals(threadPoolSizeSetting.getSocketUserRemovalHandler(), 4);
assertEquals(threadPoolSizeSetting.getStatistics(), 5);
assertEquals(threadPoolSizeSetting.getStreamHandler(), 6);
assertEquals(threadPoolSizeSetting.getSystemRequestHandler(), 7);
sessionManagementSetting = settings.getSessionManagement();
assertEquals(sessionManagementSetting.getSessionMaxIdleTimeInSecond(), 100);
assertEquals(sessionManagementSetting.getSessionMaxRequestPerSecond(), maxRequestPerSecond);
assertEquals(maxRequestPerSecond.getValue(), 15);
assertEquals(maxRequestPerSecond.getAction(), EzyMaxRequestPerSecondAction.DROP_REQUEST);
assertEquals(settings.getEventControllers(), eventControllersSetting);
assertEquals(settings.getZones().getSize(), 1);
assertEquals(zonesSetting.getZoneByName("test"), zoneSetting);
}
use of com.tvd12.ezyfoxserver.setting.EzySimpleZoneSetting in project ezyfox-server by youngmonkeys.
the class HelloEmbeddedServer3 method main.
public static void main(String[] args) throws Exception {
EzySimplePluginSetting pluginSetting = new EzyPluginSettingBuilder().name(// plugin name
"hello").addListenEvent(// listen able events USER_LOGIN, USER_ADDED, USER_REMOVED
EzyEventType.USER_LOGIN).configFile("config.properties").entryLoader(HelloPluginEntryLoader.class).priority(// set priority, bigger number is lower, default 0
1).threadPoolSize(// set thread pool size to create executor service for this plugin, default 0
3).build();
EzySimpleAppSetting appSetting = new EzyAppSettingBuilder().name(// app's name
"hello").configFile("config.properties").entryLoader(HelloAppEntryLoader.class).maxUsers(// set max user in this app, default 999999
9999).threadPoolSize(// set thread pool size to create executor service for this app, default 0
3).build();
EzySimpleUserManagementSetting userManagementSetting = new EzyUserManagementSettingBuilder().allowChangeSession(// allow change user's session, default true
true).allowGuestLogin(// allow guest login, default false
true).guestNamePrefix(// set name prefix for guest
"Guest#").maxSessionPerUser(// set number of max sessions per user // default 5
5).userMaxIdleTimeInSecond(// set max idle time of an user, default 0
15).userNamePattern(// set username pattern, default ^[a-z0-9_.]{3,36}$
"^[a-z0-9_.]{3,36}$").build();
EzySimpleZoneSetting zoneSetting = new EzyZoneSettingBuilder().name(// zone's name
"hello").plugin(// add a plug-in to zone
pluginSetting).application(// add an app to zone
appSetting).configFile(// set config file
"config.properties").maxUsers(// set maximum user for zone
999999).userManagement(// set user management settings
userManagementSetting).addEventController(EzyEventType.SERVER_READY, HelloZoneServerReadyController.class).build();
EzySimpleSocketSetting socketSetting = new EzySocketSettingBuilder().active(// active or not, default true
true).address(// loopback address, default 0.0.0.0
"0.0.0.0").codecCreator(// encoder/decoder creator, default MsgPackCodecCreator
MsgPackCodecCreator.class).maxRequestSize(// max request size, default 32768
1024).port(// port, default 3005
3005).tcpNoDelay(// tcp no delay, default false
true).writerThreadPoolSize(// thread pool size for socket writer, default 8
8).build();
EzySimpleWebSocketSetting webSocketSetting = new EzyWebSocketSettingBuilder().active(// active or not, default true
true).address(// loopback address, default 0.0.0.0
"0.0.0.0").codecCreator(// encoder/decoder creator, default JacksonCodecCreator
JacksonCodecCreator.class).maxFrameSize(// max frame size, default 32768
32678).port(// port, default 3005
2208).writerThreadPoolSize(// thread pool size for socket writer, default 8
8).build();
EzySimpleMaxRequestPerSecond maxRequestPerSecond = new EzyMaxRequestPerSecondBuilder().value(// max request in a second
15).action(// action when get max
EzyMaxRequestPerSecondAction.DROP_REQUEST).build();
EzySimpleSessionManagementSetting sessionManagementSetting = new EzySessionManagementSettingBuilder().sessionMaxIdleTimeInSecond(// set max idle time for session, default 30s
30).sessionMaxWaitingTimeInSecond(// set max waiting time to login for session, default 30s
30).sessionMaxRequestPerSecond(// set max request in a session for a session
maxRequestPerSecond).build();
EzySimpleUdpSetting udpSetting = new EzyUdpSettingBuilder().active(// active or not
true).address(// set loopback IP
"0.0.0.0").channelPoolSize(// set number of udp channel for socket writing, default 16
16).codecCreator(// encoder/decoder creator, default MsgPackCodecCreator
MsgPackCodecCreator.class).handlerThreadPoolSize(// set number of handler's thread, default 5
5).maxRequestSize(// set max request's size
1024).port(// set listen port
2611).build();
EzySimpleSettings settings = new EzySettingsBuilder().debug(// allow debug to print log or not, default false
true).nodeName(// for convenient
"hello").zone(// add a zone to server
zoneSetting).socket(// set socket setting
socketSetting).websocket(// set websocket setting
webSocketSetting).udp(// set udp setting
udpSetting).sessionManagement(// set session management setting
sessionManagementSetting).addEventController(EzyEventType.SERVER_INITIALIZING, HelloServerInitializingReadyController.class).build();
EzyEmbeddedServer server = EzyEmbeddedServer.builder().settings(settings).build();
server.start();
}
use of com.tvd12.ezyfoxserver.setting.EzySimpleZoneSetting in project ezyfox-server by youngmonkeys.
the class EzySimplePluginEntryTest method test2.
@Test
public void test2() throws Exception {
EzySimpleSettings settings = new EzySimpleSettings();
EzySimpleServer server = new EzySimpleServer();
server.setSettings(settings);
EzySimpleServerContext serverContext = new EzySimpleServerContext();
serverContext.setServer(server);
serverContext.init();
EzySimpleZoneSetting zoneSetting = new EzySimpleZoneSetting();
EzySimpleZone zone = new EzySimpleZone();
zone.setSetting(zoneSetting);
EzySimpleZoneContext zoneContext = new EzySimpleZoneContext();
zoneContext.setZone(zone);
zoneContext.init();
zoneContext.setParent(serverContext);
EzySimplePluginSetting pluginSetting = new EzySimplePluginSetting();
pluginSetting.setName("test");
EzyEventControllersSetting eventControllersSetting = new EzySimpleEventControllersSetting();
EzyEventControllers eventControllers = EzyEventControllersImpl.create(eventControllersSetting);
EzySimplePlugin plugin = new EzySimplePlugin();
plugin.setSetting(pluginSetting);
plugin.setEventControllers(eventControllers);
ScheduledExecutorService pluginScheduledExecutorService = new EzyErrorScheduledExecutorService("not implement");
EzySimplePluginContext pluginContext = new EzySimplePluginContext();
pluginContext.setPlugin(plugin);
pluginContext.setParent(zoneContext);
pluginContext.setExecutorService(pluginScheduledExecutorService);
pluginContext.init();
EzySimplePluginEntry entry = new EzyPluginEntryEx2();
entry.config(pluginContext);
entry.start();
entry.destroy();
}
Aggregations