Commit c8389232 authored by Derek DeMoro's avatar Derek DeMoro Committed by derek

Refactoring work.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@578 b35dd754-fafc-0310-a699-88a17e54d16e
parent 6122b0a5
......@@ -120,6 +120,10 @@ public class ServerSession implements Session {
return address;
}
public void setAddress(JID address){
this.address = address;
}
public void process(Packet packet) {
}
......
......@@ -51,6 +51,14 @@ public interface Session extends RoutableChannelHandler {
*/
public JID getAddress();
/**
* Sets the new address of this session. The address is used by services like the core
* server packet router to determine if a packet should be sent to the handler.
* Handlers that are working on behalf of the server should use the generic server
* hostname address (e.g. server.com).
*/
public void setAddress(JID address);
/**
* Returns the connection associated with this Session.
*
......
......@@ -11,7 +11,17 @@
package org.jivesoftware.messenger;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
......@@ -21,11 +31,16 @@ import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.container.Container;
import org.jivesoftware.messenger.container.TrackInfo;
import org.jivesoftware.messenger.spi.BasicStreamIDFactory;
import org.jivesoftware.messenger.spi.PacketTransporterImpl;
import org.jivesoftware.messenger.spi.SessionImpl;
import org.jivesoftware.messenger.user.UserManager;
import org.jivesoftware.messenger.user.UserNotFoundException;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.xmpp.packet.JID;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet;
import org.xmpp.packet.Presence;
/**
* Manages the sessions associated with an account. The information
......@@ -38,12 +53,11 @@ public class SessionManager implements ConnectionCloseListener {
private int sessionCount = 0;
final int NEVER_KICK = -1;
public XMPPServer server;
public PacketRouter router;
public PacketTransporter transporter;
public PacketTransporterImpl transporter;
private String serverName;
private XMPPAddress serverAddress;
private JID serverAddress;
public UserManager userManager;
private int conflictLimit;
private Random randomResource = new Random();
......@@ -126,14 +140,13 @@ public class SessionManager implements ConnectionCloseListener {
* have no associated user, and don't follow the normal routing rules for
* priority based fall over.
*/
private HashMap anonymousSessions = new HashMap();
private Map anonymousSessions = new HashMap();
/**
* Simple data structure to track sessions for a single user (tracked by resource
* and priority).
*/
private class SessionMap {
private HashMap resources = new HashMap();
private LinkedList priorityList = new LinkedList();
......@@ -179,7 +192,7 @@ public class SessionManager implements ConnectionCloseListener {
* @param sender The sender who's session just changed priority
* @param priority The new priority for the session
*/
public void changePriority(XMPPAddress sender, int priority) {
public void changePriority(JID sender, int priority) {
String resource = sender.getResource();
if (resources.containsKey(resource)) {
priorityList.remove(resource);
......@@ -248,12 +261,11 @@ public class SessionManager implements ConnectionCloseListener {
*
* @param packet
*/
private void broadcast(XMPPPacket packet) throws
UnauthorizedException, PacketException, XMLStreamException {
private void broadcast(Packet packet) throws UnauthorizedException, PacketException, XMLStreamException {
Iterator entries = resources.values().iterator();
while (entries.hasNext()) {
Session session = (Session)entries.next();
packet.setRecipient(session.getAddress());
packet.setTo(session.getAddress());
session.getConnection().deliver(packet);
}
}
......@@ -294,7 +306,7 @@ public class SessionManager implements ConnectionCloseListener {
public boolean addSession(Session session) {
boolean success = false;
sessionLock.writeLock().lock();
String username = session.getAddress().getName().toLowerCase();
String username = session.getAddress().getNode().toLowerCase();
SessionMap resources = null;
try {
resources = (SessionMap)sessions.get(username);
......@@ -317,8 +329,8 @@ public class SessionManager implements ConnectionCloseListener {
}
if (success) {
Session defaultSession = resources.getDefaultSession();
routingTable.addRoute(new XMPPAddress(defaultSession.getAddress().getNamePrep(),
defaultSession.getAddress().getHostPrep(), ""), defaultSession);
routingTable.addRoute(new JID(defaultSession.getAddress().getNode(), defaultSession.getAddress().getDomain(), ""),
defaultSession);
routingTable.addRoute(session.getAddress(), session);
}
return success;
......@@ -330,8 +342,8 @@ public class SessionManager implements ConnectionCloseListener {
* @param sender The sender who's session just changed priority
* @param priority The new priority for the session
*/
public void changePriority(XMPPAddress sender, int priority) {
String username = sender.getName().toLowerCase();
public void changePriority(JID sender, int priority) {
String username = sender.getNode().toLowerCase();
sessionLock.writeLock().lock();
try {
SessionMap resources = (SessionMap)sessions.get(username);
......@@ -343,8 +355,8 @@ public class SessionManager implements ConnectionCloseListener {
// Get the session with highest priority
Session defaultSession = resources.getDefaultSession();
// Update the route to the bareJID with the session with highest priority
routingTable.addRoute(new XMPPAddress(defaultSession.getAddress().getNamePrep(),
defaultSession.getAddress().getHostPrep(), ""), defaultSession);
routingTable.addRoute(new JID(defaultSession.getAddress().getNode(), defaultSession.getAddress().getDomain(), ""),
defaultSession);
}
finally {
sessionLock.writeLock().unlock();
......@@ -363,10 +375,10 @@ public class SessionManager implements ConnectionCloseListener {
* @param recipient The recipient ID to send to or null to select the default route
* @return The XMPPAddress best suited to use for delivery to the recipient
*/
public Session getBestRoute(XMPPAddress recipient) {
public Session getBestRoute(JID recipient) {
Session session = null;
String resource = recipient.getResource();
String username = recipient.getName();
String username = recipient.getNode();
if (username == null || "".equals(username)) {
if (resource != null) {
anonymousSessionLock.readLock().lock();
......@@ -402,10 +414,10 @@ public class SessionManager implements ConnectionCloseListener {
return session;
}
public boolean isActiveRoute(XMPPAddress route) {
public boolean isActiveRoute(JID route) {
boolean hasRoute = false;
String resource = route.getResource();
String username = route.getName();
String username = route.getNode();
if (username == null || "".equals(username)) {
if (resource != null) {
......@@ -449,14 +461,13 @@ public class SessionManager implements ConnectionCloseListener {
return hasRoute;
}
public Session getSession(XMPPAddress address)
throws UnauthorizedException, SessionNotFoundException {
public Session getSession(JID address) throws UnauthorizedException, SessionNotFoundException {
Session session = null;
String resource = address.getResource();
if (resource == null) {
throw new SessionNotFoundException();
}
String username = address.getName();
String username = address.getNode();
if (username == null || "".equals(username)) {
anonymousSessionLock.readLock().lock();
try {
......@@ -729,8 +740,7 @@ public class SessionManager implements ConnectionCloseListener {
*
* @param packet The packet to be broadcast
*/
public void broadcast(XMPPPacket packet) throws
UnauthorizedException, PacketException, XMLStreamException {
public void broadcast(Packet packet) throws UnauthorizedException, PacketException, XMLStreamException {
sessionLock.readLock().lock();
try {
Iterator values = sessions.values().iterator();
......@@ -760,8 +770,7 @@ public class SessionManager implements ConnectionCloseListener {
*
* @param packet The packet to be broadcast
*/
public void userBroadcast(String username, XMPPPacket packet) throws
UnauthorizedException, PacketException, XMLStreamException {
public void userBroadcast(String username, Packet packet) throws UnauthorizedException, PacketException, XMLStreamException {
sessionLock.readLock().lock();
try {
SessionMap sessionMap = (SessionMap)sessions.get(username);
......@@ -796,8 +805,8 @@ public class SessionManager implements ConnectionCloseListener {
}
}
else {
if (session.getAddress() != null && session.getAddress().getName() != null) {
String username = session.getAddress().getName().toLowerCase();
if (session.getAddress() != null && session.getAddress().getNode() != null) {
String username = session.getAddress().getNode().toLowerCase();
sessionLock.writeLock().lock();
try {
sessionMap = (SessionMap)sessions.get(username);
......@@ -818,25 +827,23 @@ public class SessionManager implements ConnectionCloseListener {
Presence presence = session.getPresence();
if (presence == null || presence.isAvailable()) {
Presence offline = packetFactory.getPresence();
offline.setOriginatingSession(session);
offline.setSender(session.getAddress());
offline.setRecipient(new XMPPAddress(null, serverName, null));
offline.setAvailable(false);
offline.setFrom(session.getAddress());
offline.setTo(new JID(null, serverName, null));
offline.setType(Presence.Type.unavailable);
router.route(offline);
}
if (session.getAddress() != null && routingTable != null && !session.getAddress().isEmpty()) {
if (session.getAddress() != null && routingTable != null && session.getAddress().toBareJID().trim().length() != 0) {
routingTable.removeRoute(session.getAddress());
if (sessionMap != null) {
if (sessionMap.isEmpty()) {
// Remove the route for the session's BARE address
routingTable.removeRoute(new XMPPAddress(session.getAddress().getNamePrep(),
session.getAddress().getHostPrep(), ""));
routingTable.removeRoute(new JID(session.getAddress().getNode(), session.getAddress().getDomain(), ""));
}
else {
// Update the route for the session's BARE address
Session defaultSession = sessionMap.getDefaultSession();
routingTable.addRoute(new XMPPAddress(defaultSession.getAddress().getNamePrep(),
defaultSession.getAddress().getHostPrep(), ""), defaultSession);
routingTable.addRoute(new JID(defaultSession.getAddress().getNode(), defaultSession.getAddress().getDomain(), ""),
defaultSession);
}
}
}
......@@ -844,7 +851,9 @@ public class SessionManager implements ConnectionCloseListener {
public void addAnonymousSession(Session session) {
try {
session.getAddress().setResource(Integer.toHexString(randomResource.nextInt()));
JID newAddress = new JID(session.getAddress().getNode(), session.getAddress().getDomain(),
Integer.toHexString(randomResource.nextInt()));
session.setAddress(newAddress);
anonymousSessionLock.writeLock().lock();
try {
anonymousSessions.put(session.getAddress().getResource(), session);
......@@ -893,7 +902,6 @@ public class SessionManager implements ConnectionCloseListener {
protected TrackInfo getTrackInfo() {
TrackInfo trackInfo = new TrackInfo();
trackInfo.getTrackerClasses().put(XMPPServer.class, "server");
trackInfo.getTrackerClasses().put(PacketTransporter.class, "transporter");
trackInfo.getTrackerClasses().put(PacketRouter.class, "router");
trackInfo.getTrackerClasses().put(UserManager.class, "userManager");
trackInfo.getTrackerClasses().put(PacketFactory.class, "packetFactory");
......@@ -904,7 +912,7 @@ public class SessionManager implements ConnectionCloseListener {
public void serviceAdded(Object service) {
if (service instanceof XMPPServer && server != null) {
serverName = server.getServerInfo().getName();
serverAddress = XMPPAddress.parseJID(serverName);
serverAddress = new JID(serverName);
}
}
......@@ -947,14 +955,14 @@ public class SessionManager implements ConnectionCloseListener {
}
}
public void sendServerMessage(XMPPAddress address, String subject, String body) throws SessionNotFoundException {
XMPPPacket packet = createServerMessage(subject, body);
public void sendServerMessage(JID address, String subject, String body) throws SessionNotFoundException {
Packet packet = createServerMessage(subject, body);
try {
if (address == null || address.getName() == null || address.getName().length() < 1) {
if (address == null || address.getNode() == null || address.getNode().length() < 1) {
broadcast(packet);
}
else if (address.getResource() == null || address.getResource().length() < 1) {
userBroadcast(address.getName(), packet);
userBroadcast(address.getNode(), packet);
}
else {
getSession(address).getConnection().deliver(packet);
......@@ -964,9 +972,9 @@ public class SessionManager implements ConnectionCloseListener {
}
}
private XMPPPacket createServerMessage(String subject, String body) {
Message message = packetFactory.getMessage();
message.setSender(serverAddress);
private Packet createServerMessage(String subject, String body) {
Message message = new Message();
message.setFrom(serverAddress);
if (subject != null) {
message.setSubject(subject);
}
......
......@@ -19,6 +19,9 @@ import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.user.User;
import org.jivesoftware.messenger.user.UserManager;
import org.jivesoftware.messenger.user.UserNotFoundException;
import org.xmpp.packet.JID;
import org.xmpp.packet.Presence;
import org.xmpp.packet.Packet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
......@@ -31,9 +34,9 @@ import java.util.List;
public class SessionImpl implements Session {
/**
* The XMPPAddress this session is authenticated as.
* The Address this session is authenticated as.
*/
private XMPPAddress jid;
private JID address;
/**
* The stream id for this session (random and unique).
......@@ -74,13 +77,12 @@ public class SessionImpl implements Session {
*
* @param connection The connection we are proxying
*/
public SessionImpl(String serverName, Connection connection, StreamID streamID)
throws UnauthorizedException {
public SessionImpl(String serverName, Connection connection, StreamID streamID) throws UnauthorizedException {
conn = connection;
this.streamID = streamID;
this.serverName = serverName;
this.jid = new XMPPAddress(null, null, null);
presence = new PresenceImpl();
this.address = new JID(null, null, null);
presence = new Presence();
this.sessionManager = SessionManager.getInstance();
......@@ -102,11 +104,11 @@ public class SessionImpl implements Session {
public void setAuthToken(AuthToken auth, UserManager userManager, String resource) throws UserNotFoundException {
User user = userManager.getUser(auth.getUsername());
jid = new XMPPAddress(user.getUsername(), serverName, resource);
address = new JID(user.getUsername(), serverName, resource);
authToken = auth;
List params = new ArrayList();
params.add(jid.toString());
params.add(address.toBareJID());
params.add(getConnection().toString());
// Log.info(LocaleUtils.getLocalizedString("admin.authenticated",params));
......@@ -115,13 +117,13 @@ public class SessionImpl implements Session {
}
public void setAnonymousAuth() {
jid = new XMPPAddress("", serverName, "");
address = new JID("", serverName, "");
// Registering with the session manager assigns the resource
sessionManager.addAnonymousSession(this);
setStatus(Session.STATUS_AUTHENTICATED);
List params = new ArrayList();
params.add(jid.toString());
params.add(address.toString());
params.add(getConnection().toString());
// Log.info(LocaleUtils.getLocalizedString("admin.authenticated",params));
}
......@@ -138,8 +140,12 @@ public class SessionImpl implements Session {
return conn;
}
public XMPPAddress getAddress() {
return jid;
public JID getAddress() {
return address;
}
public void setAddress(JID address){
this.address = address;
}
public StreamID getStreamID() {
......@@ -216,11 +222,11 @@ public class SessionImpl implements Session {
conflictCount++;
}
public void process(XMPPPacket packet) {
public void process(Packet packet) {
deliver(packet);
}
private void deliver(XMPPPacket packet) {
private void deliver(Packet packet) {
if (conn != null && !conn.isClosed()) {
try {
conn.deliver(packet);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment