📄 multiuserchatserverimpl.java
字号:
}
else if (name != null && "x-roomuser-item".equals(node)) {
// Answer reserved nickname for the sender of the disco request in the requested room
MUCRoom room = getChatRoom(name);
if (room != null) {
String reservedNick = room.getReservedNickname(senderJID.toBareJID());
if (reservedNick != null) {
Element identity = DocumentHelper.createElement("identity");
identity.addAttribute("category", "conference");
identity.addAttribute("name", reservedNick);
identity.addAttribute("type", "text");
identities.add(identity);
}
}
}
return identities.iterator();
}
public Iterator<String> getFeatures(String name, String node, JID senderJID) {
ArrayList<String> features = new ArrayList<String>();
if (name == null && node == null) {
// Answer the features of the MUC service
features.add("http://jabber.org/protocol/muc");
features.add("http://jabber.org/protocol/disco#info");
features.add("http://jabber.org/protocol/disco#items");
features.add("jabber:iq:search");
features.add(ResultSet.NAMESPACE_RESULT_SET_MANAGEMENT);
}
else if (name != null && node == null) {
// Answer the features of a given room
MUCRoom room = getChatRoom(name);
if (room != null && canDiscoverRoom(room)) {
features.add("http://jabber.org/protocol/muc");
// Always add public since only public rooms can be discovered
features.add("muc_public");
if (room.isMembersOnly()) {
features.add("muc_membersonly");
}
else {
features.add("muc_open");
}
if (room.isModerated()) {
features.add("muc_moderated");
}
else {
features.add("muc_unmoderated");
}
if (room.canAnyoneDiscoverJID()) {
features.add("muc_nonanonymous");
}
else {
features.add("muc_semianonymous");
}
if (room.isPasswordProtected()) {
features.add("muc_passwordprotected");
}
else {
features.add("muc_unsecured");
}
if (room.isPersistent()) {
features.add("muc_persistent");
}
else {
features.add("muc_temporary");
}
}
}
return features.iterator();
}
public XDataFormImpl getExtendedInfo(String name, String node, JID senderJID) {
if (name != null && node == null) {
// Answer the extended info of a given room
MUCRoom room = getChatRoom(name);
if (room != null && canDiscoverRoom(room)) {
XDataFormImpl dataForm = new XDataFormImpl(DataForm.TYPE_RESULT);
XFormFieldImpl field = new XFormFieldImpl("FORM_TYPE");
field.setType(FormField.TYPE_HIDDEN);
field.addValue("http://jabber.org/protocol/muc#roominfo");
dataForm.addField(field);
field = new XFormFieldImpl("muc#roominfo_description");
field.setLabel(LocaleUtils.getLocalizedString("muc.extended.info.desc"));
field.addValue(room.getDescription());
dataForm.addField(field);
field = new XFormFieldImpl("muc#roominfo_subject");
field.setLabel(LocaleUtils.getLocalizedString("muc.extended.info.subject"));
field.addValue(room.getSubject());
dataForm.addField(field);
field = new XFormFieldImpl("muc#roominfo_occupants");
field.setLabel(LocaleUtils.getLocalizedString("muc.extended.info.occupants"));
field.addValue(Integer.toString(room.getOccupantsCount()));
dataForm.addField(field);
/*field = new XFormFieldImpl("muc#roominfo_lang");
field.setLabel(LocaleUtils.getLocalizedString("muc.extended.info.language"));
field.addValue(room.getLanguage());
dataForm.addField(field);*/
field = new XFormFieldImpl("x-muc#roominfo_creationdate");
field.setLabel(LocaleUtils.getLocalizedString("muc.extended.info.creationdate"));
field.addValue(dateFormatter.format(room.getCreationDate()));
dataForm.addField(field);
return dataForm;
}
}
return null;
}
public boolean hasInfo(String name, String node, JID senderJID) {
// Check if the service is disabled. Info is not available when disabled.
if (!isServiceEnabled()) {
return false;
}
if (name == null && node == null) {
// We always have info about the MUC service
return true;
}
else if (name != null && node == null) {
// We only have info if the room exists
return hasChatRoom(name);
}
else if (name != null && "x-roomuser-item".equals(node)) {
// We always have info about reserved names as long as the room exists
return hasChatRoom(name);
}
return false;
}
public Iterator<DiscoItem> getItems(String name, String node, JID senderJID) {
// Check if the service is disabled. Info is not available when disabled.
if (!isServiceEnabled()) {
return null;
}
List<DiscoItem> answer = new ArrayList<DiscoItem>();
if (name == null && node == null)
{
// Answer all the public rooms as items
for (MUCRoom room : rooms.values())
{
if (canDiscoverRoom(room))
{
answer.add(new DiscoItem(room.getRole().getRoleAddress(),
room.getNaturalLanguageName(), null, null));
}
}
}
else if (name != null && node == null) {
// Answer the room occupants as items if that info is publicly available
MUCRoom room = getChatRoom(name);
if (room != null && canDiscoverRoom(room)) {
for (MUCRole role : room.getOccupants()) {
// TODO Should we filter occupants that are invisible (presence is not broadcasted)?
answer.add(new DiscoItem(role.getRoleAddress(), null, null, null));
}
}
}
return answer.iterator();
}
private boolean canDiscoverRoom(MUCRoom room) {
// Check if locked rooms may be discovered
if (!allowToDiscoverLockedRooms && room.isLocked()) {
return false;
}
return room.isPublicRoom();
}
public void addListener(MUCEventListener listener) {
listeners.add(listener);
}
public void removeListener(MUCEventListener listener) {
listeners.remove(listener);
}
void fireOccupantJoined(JID roomJID, JID user, String nickname) {
for (MUCEventListener listener : listeners) {
listener.occupantJoined(roomJID, user, nickname);
}
}
void fireOccupantLeft(JID roomJID, JID user) {
for (MUCEventListener listener : listeners) {
listener.occupantLeft(roomJID, user);
}
}
void fireNicknameChanged(JID roomJID, JID user, String oldNickname, String newNickname) {
for (MUCEventListener listener : listeners) {
listener.nicknameChanged(roomJID, user, oldNickname, newNickname);
}
}
void fireMessageReceived(JID roomJID, JID user, String nickname, Message message) {
for (MUCEventListener listener : listeners) {
listener.messageReceived(roomJID, user, nickname, message);
}
}
void fireRoomDestroyed(JID roomJID) {
for (MUCEventListener listener : listeners) {
listener.roomDestroyed(roomJID);
}
}
/**
* Converts an array to a comma-delimitted String.
*
* @param array the array.
* @return a comma delimtted String of the array values.
*/
private static String fromArray(String [] array) {
StringBuilder buf = new StringBuilder();
for (int i=0; i<array.length; i++) {
buf.append(array[i]);
if (i != array.length-1) {
buf.append(",");
}
}
return buf.toString();
}
/****************** Statistics code ************************/
private void addTotalRoomStats() {
// Register a statistic.
Statistic statistic = new Statistic() {
public String getName() {
return LocaleUtils.getLocalizedString("muc.stats.active_group_chats.name");
}
public Type getStatType() {
return Type.count;
}
public String getDescription() {
return LocaleUtils.getLocalizedString("muc.stats.active_group_chats.desc");
}
public String getUnits() {
return LocaleUtils.getLocalizedString("muc.stats.active_group_chats.units");
}
public double sample() {
return getNumberChatRooms();
}
public boolean isPartialSample() {
return false;
}
};
StatisticsManager.getInstance().addStatistic(roomsStatKey, statistic);
}
private void addTotalOccupantsStats() {
// Register a statistic.
Statistic statistic = new Statistic() {
public String getName() {
return LocaleUtils.getLocalizedString("muc.stats.occupants.name");
}
public Type getStatType() {
return Type.count;
}
public String getDescription() {
return LocaleUtils.getLocalizedString("muc.stats.occupants.description");
}
public String getUnits() {
return LocaleUtils.getLocalizedString("muc.stats.occupants.label");
}
public double sample() {
return getNumberRoomOccupants();
}
public boolean isPartialSample() {
return false;
}
};
StatisticsManager.getInstance().addStatistic(occupantsStatKey, statistic);
}
private void addTotalConnectedUsers() {
// Register a statistic.
Statistic statistic = new Statistic() {
public String getName() {
return LocaleUtils.getLocalizedString("muc.stats.users.name");
}
public Type getStatType() {
return Type.count;
}
public String getDescription() {
return LocaleUtils.getLocalizedString("muc.stats.users.description");
}
public String getUnits() {
return LocaleUtils.getLocalizedString("muc.stats.users.label");
}
public double sample() {
return getNumberConnectedUsers(false);
}
public boolean isPartialSample() {
return false;
}
};
StatisticsManager.getInstance().addStatistic(usersStatKey, statistic);
}
private void addNumberIncomingMessages() {
// Register a statistic.
Statistic statistic = new Statistic() {
public String getName() {
return LocaleUtils.getLocalizedString("muc.stats.incoming.name");
}
public Type getStatType() {
return Type.rate;
}
public String getDescription() {
return LocaleUtils.getLocalizedString("muc.stats.incoming.description");
}
public String getUnits() {
return LocaleUtils.getLocalizedString("muc.stats.incoming.label");
}
public double sample() {
return inMessages.getAndSet(0);
}
public boolean isPartialSample() {
// Get this value from the other cluster nodes
return true;
}
};
StatisticsManager.getInstance().addMultiStatistic(incomingStatKey, trafficStatGroup, statistic);
}
private void addNumberOutgoingMessages() {
// Register a statistic.
Statistic statistic = new Statistic() {
public String getName() {
return LocaleUtils.getLocalizedString("muc.stats.outgoing.name");
}
public Type getStatType() {
return Type.rate;
}
public String getDescription() {
return LocaleUtils.getLocalizedString("muc.stats.outgoing.description");
}
public String getUnits() {
return LocaleUtils.getLocalizedString("muc.stats.outgoing.label");
}
public double sample() {
return outMessages.getAndSet(0);
}
public boolean isPartialSample() {
// Each cluster node knows the total across the cluster
return false;
}
};
StatisticsManager.getInstance().addMultiStatistic(outgoingStatKey, trafficStatGroup, statistic);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -