/** * Helper function to get value from map. * * @param map Map to take value from. * @param key Key to search in map. * @param ifNull Default value if {@code null} was returned by map. * @param <K> Key type. * @param <V> Value type. * @return Value from map or default value if map return {@code null}. */ public static <K, V> V getOrElse(Map<K, V> map, K key, V ifNull) { assert map != null; V res = map.get(key); return res != null ? res : ifNull; }
private synchronized String allocName(String type, String namePattern) { Map<String, Integer> patterns = nameTypes.get(type); if (patterns == null) { patterns = new HashMap<String, Integer>(); nameTypes.put(type, patterns); } Integer id = patterns.get(namePattern); if (id == null) id = 0; id++; patterns.put(namePattern, id); String agentName = namePattern.replaceFirst("#", id.toString()); if (agentName.equals(namePattern)) Log.warn("AllocName: missing '#' in name pattern '" + namePattern + "'"); else Log.debug( "AllocName: for type=" + type + " assigned '" + agentName + "' from pattern '" + namePattern + "'"); return agentName; }
public Object getAttribute(String name) { if (name == null) { throw new NullPointerException("null name parameter"); } if (attributes == null) { attributes = getHttpContext().getAttributes(); } return attributes.get(name); }
protected void handleClient(SelectionKey key) throws IOException { SocketChannel sc = (SocketChannel) key.channel(); ClientInfo ci = (ClientInfo) allClients.get(sc); if (ci == null) throw new IllegalStateException("Unknown client"); if (key.isWritable()) send(sc, ci); if (key.isReadable()) // 从一个客户端读进所有的可用数据 recv(sc, ci); }
public static TcpConnectionManager getConnectionPool(EndPoint from, EndPoint to) { String key = from + ":" + to; TcpConnectionManager cp = poolTable_.get(key); if (cp == null) { lock_.lock(); try { cp = poolTable_.get(key); if (cp == null) { cp = new TcpConnectionManager( MessagingConfig.getConnectionPoolInitialSize(), MessagingConfig.getConnectionPoolGrowthFactor(), MessagingConfig.getConnectionPoolMaxSize(), from, to); poolTable_.put(key, cp); } } finally { lock_.unlock(); } } return cp; }
/** * returns the RDPConnection that is registered for the given datagram channel and is connected to * the host/port in ConnectionInfo returns null if there is no matching registered rdpconnection */ static RDPConnection getConnection(DatagramChannel dc, ConnectionInfo conInfo) { lock.lock(); try { Map<ConnectionInfo, RDPConnection> dcConMap = allConMap.get(dc); if (dcConMap == null) { // there isnt even a datagram associated if (Log.loggingNet) Log.net("RDPServer.getConnection: could not find datagram"); return null; } return dcConMap.get(conInfo); } finally { lock.unlock(); } }
public static void kill(StringTokenizer st) { try { if (st.hasMoreTokens()) { String clientName = st.nextToken(); if (clients.containsKey(clientName)) { SocketChannel clientChannel = clients.get(clientName); clients.remove(clientName); sendMessage(clientChannel, MessageUtils.bye()); notifyAllClients(clientName + " leave this chatroom"); System.out.println(clientName + " leave this chatroom"); Utils.tryClose(clientChannel); } else { System.err.println("kill: no such client"); } } else { System.err.println("kill: no client name"); } } catch (Exception expt) { Utils.printErrorAndExit(expt.getMessage()); } }
public static void readCommand() { try { if (!reader.ready()) { return; } StringTokenizer tokens = new StringTokenizer(reader.readLine()); if (tokens.hasMoreTokens()) { switch (tokens.nextToken()) { case "/listen": listen(tokens); break; case "/stop": stop(); break; case "/list": if (port[0] == -1) { System.err.println("Error: start listening before"); } for (Entry<String, SocketChannel> client : clients.entrySet()) { System.out.println(client.getKey()); } if (clients.entrySet().isEmpty()) { System.out.println("list: no clients online"); } break; case "/send": if (tokens.hasMoreTokens()) { String name = tokens.nextToken(); if (clients.containsKey(name)) { if (tokens.hasMoreTokens()) { sendMessage( clients.get(name), MessageUtils.message("<server>", getMessageFromTokens(tokens))); } else { System.err.println("send: message is empty"); } } else { System.err.println("send: no such client"); } } else { System.err.println("send: no client name"); } break; case "/sendall": if (tokens.hasMoreTokens()) { notifyAllClients(getMessageFromTokens(tokens)); } else { System.err.println("sendall: message is empty"); } break; case "/kill": kill(tokens); break; case "/exit": if (port[0] != -1) { stop(); } Utils.tryClose(selector); System.exit(0); break; default: System.err.print("Error: unknown command"); break; } } else { System.err.println("Error: empty input"); } } catch (Exception expt) { Utils.printErrorAndExit(expt.getMessage()); } }
public IVerbHandler getVerbHandler(String type) { IVerbHandler handler = (IVerbHandler) verbHandlers_.get(type); return handler; }
private void checkForReservedVerb(String type) { if (reservedVerbs_.get(type) != null && verbHandlers_.get(type) != null) { throw new IllegalArgumentException(type + " is a reserved verb handler. Scram!"); } }