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