⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 roomserver.java

📁 piweurrrrq i o fhsadhfka fd dskajc zxkjcnkjsahc
💻 JAVA
字号:
/* * Copyright (c) 2000 Lyrisoft Solutions, Inc. * Used by permission */package com.lyrisoft.chat.server.remote;import java.util.HashMap;import java.util.HashSet;import java.util.ArrayList;import java.util.Iterator;import com.lyrisoft.chat.ICommands;/** * RoomServer.  This represents a room in the system, and is sort of a little server of * its own. */public class RoomServer implements ICommands {    protected String _roomName;    protected HashMap _users;    protected ChatClient _owner;    protected ChatServer _server;    private String _password;    private HashSet _invitations = new HashSet();    public RoomServer(String name, ChatServer server) {        _server = server;        _roomName = name;        _users = new HashMap();    }    public RoomServer(String name, String password, ChatServer server) {        _server = server;        _roomName = name;        _password = password;        _users = new HashMap();    }    /**     * Get the name of this room     */    public String getName() {        return _roomName;    }    /**     * Tells whether or not this room is empty     * @return true if emtpy, false otherwise     */    public boolean isEmpty() {        return _users.size() == 0;    }    /**     * Send a general message to all the clients in this room     * @param message the general message     */    public void broadcast(String message) {        synchronized (_users) {            for (Iterator i = _users.values().iterator(); i.hasNext(); ) {                ChatClient rcpt = (ChatClient)i.next();                rcpt.generalRoomMessage(_roomName, message);            }        }    }    /**     * Send an emote to the room.     * @param sender the person doing the emote     * @param message the emote text     */    public void emote(ChatClient sender, String message) {        emote(sender.getUserId(), message);    }    public void emote(String from, String message) {        synchronized (_users) {            for (Iterator i = _users.values().iterator(); i.hasNext(); ) {                ChatClient rcpt = (ChatClient)i.next();                rcpt.emote(from, _roomName, message);            }        }    }    /**     * Say something to everyone in the room     * @param sender the person doing the talking     * @param message the message text     */    public void say(ChatClient sender, String message) {        String from = sender.getUserId();        say(from, message);    }    public void say(String from, String message) {        synchronized (_users) {            for (Iterator i = _users.values().iterator(); i.hasNext(); ) {                ChatClient rcpt = (ChatClient)i.next();                rcpt.messageFromUser(from, _roomName, message);            }        }    }    protected void notifyJoin(String userId) {        synchronized (_users) {            // notify the other people            for (Iterator i = _users.values().iterator(); i.hasNext(); ) {                ((ChatClient)i.next()).userJoinedRoom(userId, _roomName);            }        }    }    public void remoteJoin(String username, String password) {        notifyJoin(username);    }    protected void notifyPart(String userId, boolean isSignoff) {        synchronized (_users) {            // inform the other users here that someone signed off            ChatClient rcpt = null;            for (Iterator i = _users.values().iterator(); i.hasNext(); ) {                rcpt = (ChatClient)i.next();                rcpt.userPartedRoom(userId, _roomName, isSignoff);            }        }    }    public void remotePart(String username, boolean isSignoff) {        notifyPart(username, isSignoff);    }    /**     * Add a user to this room.  All other room members are notified     * @param client the client that is joining     * @see ChatClient#userJoinedRoom     */    public void join(ChatClient client) throws RoomJoinException {        join(client, null);    }    public void join(ChatClient client, String password) throws RoomJoinException {        String userId = client.getUserId();        if (_password != null && !_password.equals(password)) {            throw new RoomJoinException(ROOM_ACCESS_DENIED);        }        if (_invitations.size() > 0 && !_invitations.contains(ChatServer.clientKey(client))) {            throw new RoomJoinException(NO_INVITE);        }        if (_users.get(ChatServer.clientKey(client)) == null) {            synchronized (_users) {                // add the user to the hashtable                _users.put(ChatServer.clientKey(client), client);                                client.ackJoinRoom(_roomName);                notifyJoin(client.getUserId());                if (_users.size() == 1) {                    _owner = client;                }            }        }    }    /**     * Remove a user from this room.  All other room members are notified     * @param client the client that is leaving     * @param isSignoff true if the client is signing off; false if the client is leaving only     *                  this room.     * @see ChatClient#userPartedRoom     */    public void part(ChatClient client, boolean isSignoff) {        part(client.getUserId(), isSignoff);    }    public void part(String userId, boolean isSignoff) {        synchronized (_users) {            if (_users.remove(ChatServer.clientKey(userId)) != null) {                ChatClient client = _server.getClient(userId);                if (client != null) {                    client.ackPartRoom(_roomName);                }                notifyPart(userId, isSignoff);                // if there is only 1 person left, make them the owner                if (_users.size() == 1) {                    _owner = (ChatClient)_users.values().iterator().next();                                    }            }        }    }    public void kick(ChatClient kicker, String victim) {        if (kicker != _owner) {            kicker.error(ACCESS_DENIED, ICommands.KICK + " " + victim);        } else {            ChatClient victimClient = (ChatClient)_users.get(ChatServer.clientKey(victim));            if (victimClient == null) {                kicker.error(NO_SUCH_USER, victim);            } else {                part(victimClient, false);            }        }    }        /**     * Get the number of users in this room     */    public int getUserCount() {        return _users.size();    }    /**     * Get a string array containing the names of all the users in this room     */    public String[] getUsers() {        ArrayList l = new ArrayList(_users.size());        synchronized (_users) {            for (Iterator i = _users.values().iterator(); i.hasNext(); ) {                String name = ((ChatClient)i.next()).getUserId();                l.add(name);            }        }        String[] names = new String[l.size()];        l.toArray(names);        return names;    }    public boolean contains(ChatClient client) {        return _users.containsKey(ChatServer.clientKey(client));    }    public void invite(ChatClient inviter, String invitee) {        if (inviter == _owner) {            synchronized (_invitations) {                _invitations.add(ChatServer.clientKey(invitee));            }        } else {            inviter.error(ACCESS_DENIED, ICommands.INVITE + " " + invitee);        }    }    public void uninvite(ChatClient inviter, String invitee) {        if (inviter == _owner) {            synchronized (_invitations) {                _invitations.remove(ChatServer.clientKey(invitee));            }        } else {            inviter.error(ACCESS_DENIED, ICommands.UNINVITE + " " + invitee);        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -