Example usage for org.apache.wicket.protocol.ws.api.registry IWebSocketConnectionRegistry getConnection

List of usage examples for org.apache.wicket.protocol.ws.api.registry IWebSocketConnectionRegistry getConnection

Introduction

In this page you can find the example usage for org.apache.wicket.protocol.ws.api.registry IWebSocketConnectionRegistry getConnection.

Prototype

IWebSocketConnection getConnection(Application application, String sessionId, IKey key);

Source Link

Usage

From source file:org.apache.openmeetings.core.util.WebSocketHelper.java

License:Apache License

private static void sendClient(IWsClient client, Consumer<IWebSocketConnection> wsc) {
    Application app = (Application) getApp();
    WebSocketSettings settings = WebSocketSettings.Holder.get(app);
    IWebSocketConnectionRegistry reg = settings.getConnectionRegistry();
    Executor executor = settings.getWebSocketPushMessageExecutor();
    final IWebSocketConnection wc = reg.getConnection(app, client.getSessionId(),
            new PageIdKey(client.getPageId()));
    if (wc != null && wc.isOpen()) {
        executor.run(() -> wsc.accept(wc));
    }/*  www . jav  a 2  s. co m*/
}

From source file:org.apache.openmeetings.core.util.WebSocketHelper.java

License:Apache License

static void send(final Function<Application, Collection<Client>> func,
        BiConsumer<IWebSocketConnection, Client> consumer, Predicate<Client> check) {
    new Thread(() -> {
        Application app = (Application) getApp();
        if (app == null) {
            return; // Application is not ready
        }//from www.  j  av  a  2 s  . c  o  m
        WebSocketSettings settings = WebSocketSettings.Holder.get(app);
        IWebSocketConnectionRegistry reg = settings.getConnectionRegistry();
        Executor executor = settings.getWebSocketPushMessageExecutor();
        for (Client c : func.apply(app)) {
            if (check == null || check.test(c)) {
                final IWebSocketConnection wc = reg.getConnection(app, c.getSessionId(),
                        new PageIdKey(c.getPageId()));
                if (wc != null && wc.isOpen()) {
                    executor.run(() -> consumer.accept(wc, c));
                }
            }
        }
    }).start();
}

From source file:org.apache.openmeetings.web.user.ChatPanel.java

License:Apache License

private static void sendRoom(ChatMessage m, String msg) {
    IWebSocketConnectionRegistry reg = WebSocketSettings.Holder.get(Application.get()).getConnectionRegistry();
    for (Client c : getRoomUsers(m.getToRoom().getId())) {
        try {// w  w w. j av a  2s.  c  om
            if (!m.isNeedModeration() || (m.isNeedModeration() && c.hasRight(Client.Right.moderator))) {
                IWebSocketConnection con = reg.getConnection(Application.get(), c.getSessionId(),
                        new PageIdKey(c.getPageId()));
                if (con != null) {
                    con.sendMessage(msg);
                }
            }
        } catch (Exception e) {
            log.error("Error while sending message to room", e);
        }
    }
}

From source file:org.efaps.ui.wicket.connectionregistry.RegistryManager.java

License:Apache License

/**
 * Gets the connections 4 user.//from  www  .  ja v  a 2 s .  c om
 *
 * @param _login the login
 * @return the connections 4 user
 */
public static List<IWebSocketConnection> getConnections4User(final String _login) {
    final List<IWebSocketConnection> ret = new ArrayList<>();
    final SearchManager searchManager = Search.getSearchManager(getCache());
    final QueryBuilder qbldr = searchManager.buildQueryBuilderForClass(UserSession.class).get();
    final CacheQuery query = searchManager
            .getQuery(qbldr.keyword().onField("userName").matching(_login).createQuery());
    try (final ResultIterator iter = query.iterator()) {
        while (iter.hasNext()) {
            final UserSession userSession = (UserSession) iter.next();
            if (userSession.getConnectionKey() != null) {
                final IWebSocketConnectionRegistry registry = WebSocketSettings.Holder
                        .get(EFapsApplication.get()).getConnectionRegistry();
                final IWebSocketConnection conn = registry.getConnection(EFapsApplication.get(),
                        userSession.getSessionId(), userSession.getConnectionKey());
                if (conn != null) {
                    ret.add(conn);
                }
            }
        }
    }
    return ret;
}

From source file:org.efaps.ui.wicket.connectionregistry.RegistryManager.java

License:Apache License

/**
 * Gets the connection 4 session./*from   w ww .j  a  v a 2s .c  o  m*/
 *
 * @param _sessionId the session id
 * @return the connection 4 session
 */
public static IWebSocketConnection getConnection4Session(final String _sessionId) {
    IWebSocketConnection ret = null;
    if (getCache().containsKey(_sessionId)) {
        final UserSession userSession = getCache().get(_sessionId);
        if (userSession.getConnectionKey() != null) {
            final IWebSocketConnectionRegistry registry = WebSocketSettings.Holder.get(EFapsApplication.get())
                    .getConnectionRegistry();
            ret = registry.getConnection(EFapsApplication.get(), userSession.getSessionId(),
                    userSession.getConnectionKey());
        }
    }
    return ret;
}

From source file:org.efaps.ui.wicket.ConnectionRegistry.java

License:Apache License

/**
 * @param _login login of the user the session is wanted for
 * @return Connections for the user, empty list if not found
 *//* w ww .  j a  va2  s.  c om*/
public List<IWebSocketConnection> getConnections4User(final String _login) {
    final List<IWebSocketConnection> ret = new ArrayList<>();
    final ConcurrentMap<String, ConcurrentHashSet<String>> user2session = Session.get().getApplication()
            .getMetaData(ConnectionRegistry.USER2SESSION);
    final ConcurrentMap<String, IKey> sessionId2pageId = Session.get().getApplication()
            .getMetaData(ConnectionRegistry.SESSION2KEY);
    final ConcurrentHashSet<String> sessionIds = user2session.get(_login);

    if (sessionIds != null && !sessionIds.isEmpty()) {
        final Iterator<String> iter = sessionIds.iterator();
        while (iter.hasNext()) {
            final String sessionId = iter.next();
            final IKey key = sessionId2pageId.get(sessionId);
            if (key != null) {
                final IWebSocketConnectionRegistry registry = WebSocketSettings.Holder
                        .get(EFapsApplication.get()).getConnectionRegistry();
                final IWebSocketConnection conn = registry.getConnection(EFapsApplication.get(), sessionId,
                        key);
                if (conn != null) {
                    ret.add(conn);
                }
            }
        }
    }
    return ret;
}

From source file:org.efaps.ui.wicket.ConnectionRegistry.java

License:Apache License

/**
 * @param _login login of the user the session is wanted for
 * @param _sessionId sessionid the connection is wanted for
 * @return Connection for the user, <code>null</code> if not found
 *///from   ww w.jav  a  2  s .co  m
public IWebSocketConnection getConnection4UserSession(final String _login, final String _sessionId) {
    IWebSocketConnection ret = null;
    final ConcurrentMap<String, ConcurrentHashSet<String>> user2session = Session.get().getApplication()
            .getMetaData(ConnectionRegistry.USER2SESSION);
    final ConcurrentMap<String, IKey> sessionId2key = Session.get().getApplication()
            .getMetaData(ConnectionRegistry.SESSION2KEY);
    final ConcurrentHashSet<String> sessionIds = user2session.get(_login);

    if (sessionIds.contains(_sessionId)) {
        final IKey key = sessionId2key.get(_sessionId);
        if (key != null) {
            final IWebSocketConnectionRegistry registry = WebSocketSettings.Holder.get(EFapsApplication.get())
                    .getConnectionRegistry();
            ret = registry.getConnection(EFapsApplication.get(), _sessionId, key);
        }
    }
    return ret;
}

From source file:org.efaps.ui.wicket.ConnectionRegistry.java

License:Apache License

/**
 * Send the KeepAlive.//from w w  w.j a  v  a 2 s . c  o m
 * @param _application Application the KeepAlive will be send for
 */
public void sendKeepAlive(final Application _application) {
    final long reference = new Date().getTime();
    final ConcurrentMap<String, IKey> sessionId2key = _application.getMetaData(ConnectionRegistry.SESSION2KEY);
    final ConcurrentMap<String, Long> keepalive = _application.getMetaData(ConnectionRegistry.KEEPALIVE);
    if (keepalive != null) {
        for (final Entry<String, Long> entry : keepalive.entrySet()) {
            if (reference
                    - entry.getValue() > Configuration.getAttributeAsInteger(ConfigAttribute.WEBSOCKET_KATH)
                            * 1000) {
                final IKey key = sessionId2key.get(entry.getKey());
                if (key != null) {
                    final IWebSocketConnectionRegistry registry = WebSocketSettings.Holder.get(_application)
                            .getConnectionRegistry();
                    final IWebSocketConnection conn = registry.getConnection(_application, entry.getKey(), key);
                    if (conn != null) {
                        try {
                            conn.sendMessage(KeepAliveBehavior.MSG);
                            ConnectionRegistry.LOG.debug("Send KeepAlive for Session: {}", entry.getKey());
                        } catch (final IOException e) {
                            ConnectionRegistry.LOG.error("Catched error", e);
                        }
                    }
                }
            }
        }
    }
}