Skip to content

Commit 6db0c88

Browse files
committed
Merge branch 'surevine-issues/213'
2 parents 6e95e6d + b33db93 commit 6db0c88

14 files changed

Lines changed: 448 additions & 125 deletions

File tree

postgres/upgrade-7.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
BEGIN TRANSACTION;
2+
3+
CREATE TABLE "online_users" ("user" TEXT NOT NULL,
4+
"updated" TIMESTAMP);
5+
6+
INSERT INTO schema_version (version, "when", description)
7+
VALUES (7, 'now', 'Added presence table');
8+
9+
COMMIT;

src/main/java/org/buddycloud/channelserver/ChannelsEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private void setupManagers() throws ComponentException {
113113
nodeStoreFactory);
114114
federatedQueueManager = new FederatedQueueManager(this,
115115
configuration);
116-
onlineUsers = new OnlineResourceManager(configuration);
116+
onlineUsers = new OnlineResourceManager(configuration, channelManagerFactory.create());
117117
}
118118

119119
@Override

src/main/java/org/buddycloud/channelserver/Configuration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ public class Configuration extends Properties {
3636

3737
public static final String DISCOVERY_USE_DNS = "discovery.dns.enabled";
3838

39+
public static final String PERSIST_PRESENCE_DATA = "users.presence.persist";
40+
3941
public static final String NOTIFICATIONS_SENDTO = "notifications.sendTo";
4042
public static final String NOTIFICATIONS_CONNECTED = "notifications.connected";
4143

4244
private static final String CONFIGURATION_FILE = "configuration.properties";
4345

44-
public static final String PURGE_REMOTE_ON_START = "sync.purge-on-start";
4546

4647
private static Configuration instance = null;
4748

src/main/java/org/buddycloud/channelserver/channel/ChannelManagerImpl.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,4 +448,19 @@ public int getCountUserFeedItems(JID user, Date since,
448448
return nodeStore.getCountUserFeedItems(user, since, parentOnly);
449449
}
450450

451+
@Override
452+
public void jidOnline(JID jid) throws NodeStoreException {
453+
nodeStore.jidOnline(jid);
454+
}
455+
456+
@Override
457+
public void jidOffline(JID jid) throws NodeStoreException {
458+
nodeStore.jidOffline(jid);
459+
}
460+
461+
@Override
462+
public ArrayList<JID> onlineJids(JID jid) throws NodeStoreException {
463+
return nodeStore.onlineJids(jid);
464+
}
465+
451466
}

src/main/java/org/buddycloud/channelserver/db/NodeStore.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,31 @@ boolean userHasRatedPost(String node, JID user, GlobalItemID id)
595595
*/
596596
ResultSet<NodeThread> getNodeThreads(String node, String afterId, int limit)
597597
throws NodeStoreException;
598+
599+
/**
600+
* A JID has come online
601+
*
602+
* @param jid
603+
* @throws NodeStoreException
604+
*/
605+
void jidOnline(JID jid) throws NodeStoreException;
606+
607+
/**
608+
* A JID has gone offline
609+
*
610+
* @param jid
611+
* @throws NodeStoreException
612+
*/
613+
void jidOffline(JID jid) throws NodeStoreException;
614+
615+
/**
616+
* Get online resources
617+
* @return
618+
* @throws NodeStoreException
619+
*
620+
* @params jid
621+
*/
622+
ArrayList<JID> onlineJids(JID jid) throws NodeStoreException;
598623

599624
/**
600625
* Count node threads

src/main/java/org/buddycloud/channelserver/db/jdbc/JDBCNodeStore.java

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1668,6 +1668,61 @@ public int countNodeThreads(String node) throws NodeStoreException {
16681668
close(selectStatement);
16691669
}
16701670
}
1671+
1672+
@Override
1673+
public void jidOnline(JID jid) throws NodeStoreException {
1674+
PreparedStatement addStatement = null;
1675+
try {
1676+
jidOffline(jid);
1677+
addStatement = conn.prepareStatement(dialect.addOnlineJid());
1678+
addStatement.setString(1, jid.toFullJID());
1679+
addStatement.executeUpdate();
1680+
addStatement.close();
1681+
} catch (SQLException e) {
1682+
throw new NodeStoreException(e);
1683+
} finally {
1684+
close(addStatement);
1685+
}
1686+
}
1687+
1688+
@Override
1689+
public void jidOffline(JID jid) throws NodeStoreException {
1690+
PreparedStatement stmt = null;
1691+
try {
1692+
stmt = conn.prepareStatement(dialect.deleteOnlineJid());
1693+
stmt.setString(1, jid.toFullJID());
1694+
stmt.executeUpdate();
1695+
} catch (SQLException e) {
1696+
throw new NodeStoreException(e);
1697+
} finally {
1698+
close(stmt); // Will implicitly close the resultset if required
1699+
}
1700+
}
1701+
1702+
@Override
1703+
public ArrayList<JID> onlineJids(JID jid) throws NodeStoreException {
1704+
PreparedStatement stmt = null;
1705+
1706+
try {
1707+
stmt = conn.prepareStatement(dialect.selectOnlineResources());
1708+
1709+
stmt.setString(1, jid.toBareJID() + "%");
1710+
1711+
java.sql.ResultSet rs = stmt.executeQuery();
1712+
1713+
ArrayList<JID> result = new ArrayList<JID>();
1714+
1715+
while (rs.next()) {
1716+
result.add(new JID(rs.getString(1)));
1717+
}
1718+
1719+
return result;
1720+
} catch (SQLException e) {
1721+
throw new NodeStoreException(e);
1722+
} finally {
1723+
close(stmt); // Will implicitly close the resultset if required
1724+
}
1725+
}
16711726

16721727
@Override
16731728
public Transaction beginTransaction() throws NodeStoreException {
@@ -1799,6 +1854,12 @@ private String getLocalId(String nodeItemId) {
17991854
public interface NodeStoreSQLDialect {
18001855
String insertNode();
18011856

1857+
String addOnlineJid();
1858+
1859+
String deleteOnlineJid();
1860+
1861+
String selectOnlineResources();
1862+
18021863
String selectNodeMemberships();
18031864

18041865
String selectUserMemberships();
@@ -1934,7 +1995,7 @@ public interface NodeStoreSQLDialect {
19341995
String selectUserFeedItems();
19351996

19361997
String selectCountUserFeedItems();
1937-
1998+
19381999
}
19392000

19402001
}

src/main/java/org/buddycloud/channelserver/db/jdbc/dialect/Sql92NodeStoreDialect.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,15 @@ public class Sql92NodeStoreDialect implements NodeStoreSQLDialect {
380380
"WHERE " +
381381
"(\"subscriptions\".\"node\" = ?) " +
382382
"ORDER BY \"updated\" DESC; ";
383+
384+
private static final String DELETE_ONLINE_JID = "DELETE FROM \"online_users\" WHERE \"user\" = ?;";
385+
386+
private static final String INSERT_ONLINE_JID = "INSERT INTO \"online_users\" (\"user\", \"updated\") VALUES (?, NOW());";
387+
388+
private static final String SELECT_ONLINE_RESOURCES = "SELECT \"user\", \"updated\" "
389+
+ "FROM \"online_users\" "
390+
+ "WHERE \"user\" LIKE ? "
391+
+ "ORDER BY \"updated\" DESC;";
383392

384393
private static final String SELECT_USER_FEED_ITEMS = ""
385394
+ "SELECT \"node\", \"id\", \"updated\", \"xml\", \"in_reply_to\" "
@@ -733,6 +742,20 @@ public String countNodeThreads() {
733742
}
734743

735744
@Override
745+
public String deleteOnlineJid() {
746+
return DELETE_ONLINE_JID;
747+
}
748+
749+
@Override
750+
public String selectOnlineResources() {
751+
return SELECT_ONLINE_RESOURCES;
752+
}
753+
754+
@Override
755+
public String addOnlineJid() {
756+
return INSERT_ONLINE_JID;
757+
}
758+
736759
public String selectUserFeedItems() {
737760
return SELECT_USER_FEED_ITEMS;
738761
}

src/main/java/org/buddycloud/channelserver/queue/OutQueueConsumer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.buddycloud.channelserver.ChannelsEngine;
88
import org.buddycloud.channelserver.Configuration;
99
import org.buddycloud.channelserver.channel.LocalDomainChecker;
10+
import org.buddycloud.channelserver.db.exception.NodeStoreException;
1011
import org.buddycloud.channelserver.utils.users.OnlineResourceManager;
1112
import org.dom4j.Attribute;
1213
import org.xmpp.component.ComponentException;
@@ -37,7 +38,7 @@ public OutQueueConsumer(ChannelsEngine component,
3738
}
3839

3940
@Override
40-
protected void consume(Packet p) {
41+
protected void consume(Packet p) throws NodeStoreException {
4142

4243
try {
4344
if (true == isRemoteServer(p.getTo())) {

src/main/java/org/buddycloud/channelserver/queue/QueueConsumer.java

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,39 @@
55
import java.util.concurrent.Executors;
66

77
import org.apache.log4j.Logger;
8+
import org.buddycloud.channelserver.db.exception.NodeStoreException;
89
import org.xmpp.packet.Packet;
910

1011
public abstract class QueueConsumer {
1112

12-
private static final Logger LOGGER = Logger.getLogger(QueueConsumer.class);
13-
14-
private ExecutorService executorService = Executors.newFixedThreadPool(5);
15-
private final BlockingQueue<Packet> queue;
16-
17-
public QueueConsumer(BlockingQueue<Packet> queue) {
18-
this.queue = queue;
19-
}
20-
21-
public void start() {
22-
executorService.submit(new Runnable() {
23-
@Override
24-
public void run() {
25-
while (true) {
26-
try {
27-
Packet packet = queue.take();
28-
consume(packet);
29-
} catch (InterruptedException e) {
30-
LOGGER.error(e);
31-
}
32-
}
33-
}
34-
});
35-
}
36-
37-
protected abstract void consume(Packet p);
38-
13+
private static final Logger LOGGER = Logger.getLogger(QueueConsumer.class);
14+
15+
private ExecutorService executorService = Executors.newFixedThreadPool(5);
16+
private final BlockingQueue<Packet> queue;
17+
18+
public QueueConsumer(BlockingQueue<Packet> queue) {
19+
this.queue = queue;
20+
}
21+
22+
public void start() {
23+
executorService.submit(new Runnable() {
24+
@Override
25+
public void run() {
26+
while (true) {
27+
try {
28+
Packet packet = queue.take();
29+
consume(packet);
30+
} catch (InterruptedException e) {
31+
LOGGER.error(e);
32+
} catch (NodeStoreException e) {
33+
// TODO Auto-generated catch block
34+
LOGGER.error(e);
35+
}
36+
}
37+
}
38+
});
39+
}
40+
41+
protected abstract void consume(Packet p) throws NodeStoreException;
42+
3943
}

0 commit comments

Comments
 (0)