All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.codeminders.socketio.server.SocketIOManager Maven / Gradle / Ivy

There is a newer version: 1.0.10
Show newest version
/**
 * The MIT License
 * Copyright (c) 2010 Tad Glines
 * Copyright (c) 2015 Alexander Sova ([email protected])
 * 

* Contributors: Ovea.com, Mycila.com *

* Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: *

* The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.codeminders.socketio.server; import javax.servlet.http.HttpSession; import java.util.Map; import java.util.concurrent.*; /** * Class to manage Socket.IO sessions and namespaces. */ public final class SocketIOManager { private static final int SESSION_ID_LEN = 20; private static final char[] SYMBOLS; static { StringBuilder sb = new StringBuilder(); for (char ch = 'A'; ch <= 'Z'; ch++) sb.append(ch); for (char ch = 'a'; ch <= 'z'; ch++) sb.append(ch); SYMBOLS = sb.toString().toCharArray(); } private final Map namespaces = new ConcurrentHashMap<>(); private final ConcurrentMap sessions = new ConcurrentHashMap<>(); private TransportProvider transportProvider; final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); private static SocketIOManager instance = new SocketIOManager(); private SocketIOManager() { } public static SocketIOManager getInstance() { return instance; } private String generateSessionId() { while(true) { StringBuilder sb = new StringBuilder(SESSION_ID_LEN); for (int i = 0; i < SESSION_ID_LEN; i++) sb.append(SYMBOLS[ThreadLocalRandom.current().nextInt(SYMBOLS.length)]); String id = sb.toString(); if(sessions.get(id) == null) return id; } } /** * Creates new session * * @deprecated use {@link SocketIOManager#createSession(HttpSession)} * @return new session */ @Deprecated public Session createSession() { return createSession(null); } /** * Creates new session * * @param httpSession The HTTP session of the connecting client * @return new session */ public Session createSession(HttpSession httpSession) { Session session = new Session(this, generateSessionId(), httpSession); sessions.put(session.getSessionId(), session); return session; } /** * Finds existing session * * @param sessionId session id * @return session object or null if not found */ public Session getSession(String sessionId) { return sessions.get(sessionId); } /** * Deletes the session * * @param sessionId session id */ public void deleteSession(String sessionId) { sessions.remove(sessionId); } /** * Creates new namespace * * @param id namespace in. Should always start with '/' * @return new namespace */ public Namespace createNamespace(String id) { Namespace ns = new Namespace(id); namespaces.put(ns.getId(), ns); return ns; } public Namespace getNamespace(String id) { return namespaces.get(id); } public TransportProvider getTransportProvider() { return transportProvider; } public void setTransportProvider(TransportProvider transportProvider) { this.transportProvider = transportProvider; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy