Search in sources :

Example 71 with EzyServerContext

use of com.tvd12.ezyfoxserver.context.EzyServerContext in project ezyfox-server by youngmonkeys.

the class EzySimpleZoneContextTest method normalCaseTest.

@SuppressWarnings("unchecked")
@Test
public void normalCaseTest() {
    EzyServerContext parent = mock(EzyServerContext.class);
    EzySimpleZoneSetting zoneSetting = new EzySimpleZoneSetting();
    zoneSetting.setName("test");
    EzySimpleZone zone = new EzySimpleZone();
    zone.setUserManager(mock(EzyZoneUserManager.class));
    zone.setSetting(zoneSetting);
    EzySimpleZoneContext context = new EzySimpleZoneContext();
    context.setZone(zone);
    context.setParent(parent);
    context.init();
    assert context.get(EzyBroadcastEvent.class) != null;
    Asserts.assertNull(context.get(Void.class));
    context.addCommand(ExCommand.class, ExCommand::new);
    assert context.cmd(ExCommand.class) != null;
    Asserts.assertNull(context.cmd(Void.class));
    context.broadcast(EzyEventType.SERVER_INITIALIZING, new EzySimpleServerInitializingEvent(), true);
    context.broadcastApps(EzyEventType.SERVER_READY, new EzySimpleServerReadyEvent(), true);
    EzySimpleUser user = new EzySimpleUser();
    user.setName("dungtv");
    EzyUserAccessAppEvent accessAppEvent = new EzySimpleUserAccessAppEvent(user);
    context.broadcastApps(EzyEventType.USER_ACCESS_APP, accessAppEvent, "dungtv", true);
    context.broadcastApps(EzyEventType.USER_ACCESS_APP, accessAppEvent, user, true);
    context.broadcastApps(EzyEventType.USER_ACCESS_APP, accessAppEvent, EzyPredicates.ALWAYS_TRUE, true);
    // noinspection EqualsWithItself
    assert context.equals(context);
    EzyZoneContext zoneContext2 = mock(EzyZoneContext.class);
    EzySimpleZone zone2 = new EzySimpleZone();
    when(zoneContext2.getZone()).thenReturn(zone2);
    assert !zoneContext2.equals(context);
    System.out.println(context.hashCode() + ", " + context.hashCode());
    assert context.hashCode() == context.hashCode();
    EzyResponse response = mock(EzyResponse.class);
    EzySession recipient = spy(EzyAbstractSession.class);
    context.send(response, recipient, false);
    context.send(response, Lists.newArrayList(recipient), false);
    context.stream(new byte[0], recipient);
    context.stream(new byte[0], Lists.newArrayList(recipient));
    context.destroy();
}
Also used : EzySimpleServerInitializingEvent(com.tvd12.ezyfoxserver.event.EzySimpleServerInitializingEvent) EzyZoneContext(com.tvd12.ezyfoxserver.context.EzyZoneContext) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzySimpleZoneContext(com.tvd12.ezyfoxserver.context.EzySimpleZoneContext) EzyResponse(com.tvd12.ezyfoxserver.response.EzyResponse) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzyZoneUserManager(com.tvd12.ezyfoxserver.wrapper.EzyZoneUserManager) EzyBroadcastEvent(com.tvd12.ezyfoxserver.command.EzyBroadcastEvent) EzySimpleZone(com.tvd12.ezyfoxserver.EzySimpleZone) EzySimpleUser(com.tvd12.ezyfoxserver.entity.EzySimpleUser) EzySimpleServerReadyEvent(com.tvd12.ezyfoxserver.event.EzySimpleServerReadyEvent) EzySimpleZoneSetting(com.tvd12.ezyfoxserver.setting.EzySimpleZoneSetting) EzyUserAccessAppEvent(com.tvd12.ezyfoxserver.event.EzyUserAccessAppEvent) EzySimpleUserAccessAppEvent(com.tvd12.ezyfoxserver.event.EzySimpleUserAccessAppEvent) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 72 with EzyServerContext

use of com.tvd12.ezyfoxserver.context.EzyServerContext in project ezyfox-server by youngmonkeys.

the class EzyCloseSessionImplTest method noSendToClient.

@Test
public void noSendToClient() {
    EzyServerContext serverContext = mock(EzyServerContext.class);
    EzyCloseSessionImpl cmd = new EzyCloseSessionImpl(serverContext);
    EzyAbstractSession session = spy(EzyAbstractSession.class);
    EzyChannel channel = mock(EzyChannel.class);
    session.setChannel(channel);
    cmd.close(session, EzyDisconnectReason.UNKNOWN);
}
Also used : EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) EzyChannel(com.tvd12.ezyfoxserver.socket.EzyChannel) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzyCloseSessionImpl(com.tvd12.ezyfoxserver.command.impl.EzyCloseSessionImpl) BaseTest(com.tvd12.test.base.BaseTest) Test(org.testng.annotations.Test)

Example 73 with EzyServerContext

use of com.tvd12.ezyfoxserver.context.EzyServerContext in project ezyfox-server by youngmonkeys.

the class EzyLoginController method handle.

@Override
public void handle(EzyServerContext ctx, EzyLoginRequest request) {
    try {
        EzySession session = request.getSession();
        EzyLoginParams params = request.getParams();
        EzyZoneContext zoneContext = ctx.getZoneContext(params.getZoneName());
        EzyUserLoginEvent loginEvent = newLoginEvent(session, params);
        try {
            control(ctx, zoneContext, loginEvent);
        } finally {
            loginEvent.release();
        }
    } catch (EzyLoginErrorException e) {
        processException(ctx, request.getSession(), e);
        throw e;
    } catch (EzyMaxUserException e) {
        processException(ctx, request.getSession(), maximumUsers(e));
        throw e;
    } catch (EzyZoneNotFoundException e) {
        processException(ctx, request.getSession(), zoneNotFound(e));
        throw e;
    } catch (Exception e) {
        processException(ctx, request.getSession(), serverError(e));
        throw e;
    }
}
Also used : EzyMaxUserException(com.tvd12.ezyfoxserver.exception.EzyMaxUserException) EzyZoneContext(com.tvd12.ezyfoxserver.context.EzyZoneContext) EzyZoneNotFoundException(com.tvd12.ezyfoxserver.exception.EzyZoneNotFoundException) EzyLoginParams(com.tvd12.ezyfoxserver.request.EzyLoginParams) EzyLoginErrorException(com.tvd12.ezyfoxserver.exception.EzyLoginErrorException) EzyUserLoginEvent(com.tvd12.ezyfoxserver.event.EzyUserLoginEvent) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzyLoginErrorException(com.tvd12.ezyfoxserver.exception.EzyLoginErrorException) EzyMaxUserException(com.tvd12.ezyfoxserver.exception.EzyMaxUserException) EzyZoneNotFoundException(com.tvd12.ezyfoxserver.exception.EzyZoneNotFoundException)

Example 74 with EzyServerContext

use of com.tvd12.ezyfoxserver.context.EzyServerContext in project ezyfox-server by youngmonkeys.

the class EzyRequestAppController method responseRequestAppError.

protected void responseRequestAppError(EzyServerContext ctx, EzySession session) {
    EzyResponse response = newRequestAppErrorResponse();
    ctx.send(response, session, false);
}
Also used : EzyResponse(com.tvd12.ezyfoxserver.response.EzyResponse)

Example 75 with EzyServerContext

use of com.tvd12.ezyfoxserver.context.EzyServerContext in project ezyfox-server by youngmonkeys.

the class EzyRequestAppController method handle.

@Override
public void handle(EzyServerContext ctx, EzyRequestAppRequest request) {
    EzyRequestAppParams params = request.getParams();
    EzyAppContext appCtx = ctx.getAppContext(params.getAppId());
    EzyApplication app = appCtx.getApp();
    EzyAppRequestController requestController = app.getRequestController();
    // user manager for checking, user must be managed
    EzyUserManager userManger = appCtx.getApp().getUserManager();
    EzyUser user = request.getUser();
    // check user joined app or not to prevent spam request
    boolean hasAccessed = userManger.containsUser(user);
    if (hasAccessed) {
        // redirect handling to app
        EzyUserRequestAppEvent event = newRequestAppEvent(request);
        requestController.handle(appCtx, event);
    } else {
        EzySession session = request.getSession();
        responseRequestAppError(ctx, session);
    }
}
Also used : EzyApplication(com.tvd12.ezyfoxserver.EzyApplication) EzyUser(com.tvd12.ezyfoxserver.entity.EzyUser) EzyRequestAppParams(com.tvd12.ezyfoxserver.request.EzyRequestAppParams) EzyAppContext(com.tvd12.ezyfoxserver.context.EzyAppContext) EzyAppRequestController(com.tvd12.ezyfoxserver.app.EzyAppRequestController) EzyUserManager(com.tvd12.ezyfoxserver.wrapper.EzyUserManager) EzyUserRequestAppEvent(com.tvd12.ezyfoxserver.event.EzyUserRequestAppEvent) EzySession(com.tvd12.ezyfoxserver.entity.EzySession)

Aggregations

EzyServerContext (com.tvd12.ezyfoxserver.context.EzyServerContext)72 Test (org.testng.annotations.Test)67 EzyZoneContext (com.tvd12.ezyfoxserver.context.EzyZoneContext)46 EzyAbstractSession (com.tvd12.ezyfoxserver.entity.EzyAbstractSession)25 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)25 BaseTest (com.tvd12.test.base.BaseTest)23 EzySessionManager (com.tvd12.ezyfoxserver.wrapper.EzySessionManager)22 EzyZoneUserManager (com.tvd12.ezyfoxserver.wrapper.EzyZoneUserManager)21 EzySimpleServer (com.tvd12.ezyfoxserver.EzySimpleServer)20 EzyServer (com.tvd12.ezyfoxserver.EzyServer)17 EzySettings (com.tvd12.ezyfoxserver.setting.EzySettings)16 EzyArray (com.tvd12.ezyfox.entity.EzyArray)14 EzyAppContext (com.tvd12.ezyfoxserver.context.EzyAppContext)14 EzyResponse (com.tvd12.ezyfoxserver.response.EzyResponse)13 BaseCoreTest (com.tvd12.ezyfoxserver.testing.BaseCoreTest)13 EzySimpleZone (com.tvd12.ezyfoxserver.EzySimpleZone)12 EzyChannel (com.tvd12.ezyfoxserver.socket.EzyChannel)12 EzyZone (com.tvd12.ezyfoxserver.EzyZone)11 EzyController (com.tvd12.ezyfoxserver.controller.EzyController)11 EzyLoggerSetting (com.tvd12.ezyfoxserver.setting.EzyLoggerSetting)11