Browse files

end of gsoc commit

  • Loading branch information...
1 parent c5d3230 commit 4d863b1b66acd5d9767641060acb4157e316eb8f @nomel7 nomel7 committed Aug 20, 2012
Showing with 386 additions and 202 deletions.
  1. +14 −4 pom.xml
  2. +1 −1 src/main/java/tahrir/TrConstants.java
  3. +17 −4 src/main/java/tahrir/TrNode.java
  4. +26 −17 src/main/java/tahrir/io/net/microblogging/ContactBook.java
  5. +24 −0 src/main/java/tahrir/io/net/microblogging/IncomingMicroblogHandler.java
  6. +12 −16 ...in/java/tahrir/io/net/microblogging/{MicrobloggingManger.java → MicroblogBroadcastScheduler.java}
  7. +3 −2 ...java/tahrir/io/net/microblogging/{MicoblogBroadcastSession.java → MicroblogBroadcastSession.java}
  8. +14 −10 src/main/java/tahrir/io/net/microblogging/MicroblogBroadcastSessionImpl.java
  9. +0 −102 src/main/java/tahrir/io/net/microblogging/MicroblogContainer.java
  10. +58 −0 src/main/java/tahrir/io/net/microblogging/containers/MicroblogsForBroadcast.java
  11. +55 −0 src/main/java/tahrir/io/net/microblogging/containers/MicroblogsForViewing.java
  12. +2 −1 src/main/java/tahrir/io/net/microblogging/filters/ContactsFilter.java
  13. +2 −2 src/main/java/tahrir/io/net/microblogging/filters/MicroblogFilter.java
  14. +2 −2 src/main/java/tahrir/io/net/microblogging/filters/Unfiltered.java
  15. +2 −2 src/main/java/tahrir/io/net/microblogging/filters/UserFilter.java
  16. +6 −6 src/main/java/tahrir/io/net/microblogging/{Microblog.java → microblogs/MicroblogForBroadcast.java}
  17. +69 −0 src/main/java/tahrir/io/net/microblogging/microblogs/ParsedMicroblog.java
  18. +16 −0 src/main/java/tahrir/tools/TrUtils.java
  19. +0 −9 src/main/java/tahrir/ui/MicroblogMessageParser.java
  20. +6 −3 src/main/java/tahrir/ui/MicroblogPost.java
  21. +10 −8 src/main/java/tahrir/ui/MicroblogViewingPage.java
  22. +22 −0 src/main/java/tahrir/ui/TabCreateButton.java
  23. +3 −5 src/main/java/tahrir/ui/TrMainWindow.java
  24. +13 −0 src/main/java/tahrir/ui/TransparentButton.java
  25. +4 −3 src/test/java/tahrir/io/net/microblogging/MicroblogBroadcastTest.java
  26. +5 −5 src/test/java/tahrir/ui/GUITest.java
View
18 pom.xml
@@ -174,9 +174,19 @@
<version>3.0</version>
</dependency>
<dependency>
- <groupId>com.seaglasslookandfeel</groupId>
- <artifactId>seaglasslookandfeel</artifactId>
- <version>0.2</version>
- </dependency>
+ <groupId>com.seaglasslookandfeel</groupId>
+ <artifactId>seaglasslookandfeel</artifactId>
+ <version>0.2</version>
+ </dependency>
+ <dependency>
+ <groupId>xom</groupId>
+ <artifactId>xom</artifactId>
+ <version>1.2.5</version>
+ </dependency>
+ <dependency>
+ <groupId>commons-codec</groupId>
+ <artifactId>commons-codec</artifactId>
+ <version>1.6</version>
+ </dependency>
</dependencies>
</project>
View
2 src/main/java/tahrir/TrConstants.java
@@ -35,7 +35,7 @@
public static final int MAINTENANCE_FREQUENCY_MIN = 1;
- public static final int MAX_MICROBLOGS_FOR_VIEWING = 200;
+ public static final int MAX_MICROBLOGS_FOR_VIEWING = 300;
public static final int GUI_WIDTH_PX = 600;
View
21 src/main/java/tahrir/TrNode.java
@@ -38,7 +38,7 @@
public final TrPeerManager peerManager;
- public MicrobloggingManger mbManager;
+ public MicrobloggingClasses mbClasses;
public TrSessionManager sessionMgr;
@@ -81,9 +81,9 @@ public void run(final RemoteNodeAddress remoteNodeAddress, final Modified modifi
logger.info("Set up peer manager");
peerManager = new TrPeerManager(config.peers, this);
- registerSessions();
+ mbClasses = new MicrobloggingClasses(this);
- mbManager = new MicrobloggingManger(this);
+ registerSessions();
}
/**
@@ -92,7 +92,7 @@ public void run(final RemoteNodeAddress remoteNodeAddress, final Modified modifi
private void registerSessions() {
sessionMgr.registerSessionClass(TopologyMaintenanceSession.class, TopologyMaintenanceSessionImpl.class);
sessionMgr.registerSessionClass(AssimilateSession.class, AssimilateSessionImpl.class);
- sessionMgr.registerSessionClass(MicoblogBroadcastSession.class, MicroblogBroadcastSessionImpl.class);
+ sessionMgr.registerSessionClass(MicroblogBroadcastSession.class, MicroblogBroadcastSessionImpl.class);
}
public ArrayList<File> getPublicNodeIdFiles() {
@@ -120,6 +120,19 @@ public void modifyPublicNodeId(final ModifyBlock<RemoteNodeAddress> mb) {
Persistence.loadAndModify(RemoteNodeAddress.class, pubNodeIdFile, mb);
}
+ public static class MicrobloggingClasses {
+ public MicroblogBroadcastScheduler mbScheduler;
+ public ContactBook contactBook;
+ public DuplicateNameAppender duplicateNameAppender;
+
+
+ public MicrobloggingClasses(final TrNode node) {
+ mbScheduler = new MicroblogBroadcastScheduler(node);
+ contactBook = new ContactBook(node, new File(node.rootDirectory, node.config.contacts));
+ duplicateNameAppender = new DuplicateNameAppender(new File(node.rootDirectory, node.config.publicKeyChars));
+ }
+ }
+
public static class PrivateNodeId {
public static Tuple2<PrivateNodeId, RemoteNodeAddress> generate() {
final Tuple2<RSAPublicKey, RSAPrivateKey> kp = TrCrypto.createRsaKeyPair();
View
43 src/main/java/tahrir/io/net/microblogging/ContactBook.java
@@ -6,46 +6,55 @@
import org.slf4j.*;
+import tahrir.TrNode;
import tahrir.tools.TrUtils;
import com.google.gson.JsonParseException;
public class ContactBook {
- public static Logger logger = LoggerFactory.getLogger(ContactBook.class);
+ private static Logger logger = LoggerFactory.getLogger(ContactBook.class);
- public ContactsContainer contactsContainer = new ContactsContainer();
+ private ContactsContainer contactsContainer = new ContactsContainer();
private final File contactsFile;
- private final MicrobloggingManger microbloggingManager;
+ private final TrNode node;
- public ContactBook(final MicrobloggingManger microbloggingManager, final File contactsFile) {
+ public ContactBook(final TrNode node, final File contactsFile) {
this.contactsFile = contactsFile;
- this.microbloggingManager = microbloggingManager;
+ this.node = node;
tryLoadContactsFromFile();
}
- /**
- * Adds a contact to the contact book
- * @return If the preferred nickname was given
- */
public boolean addContact(final String preferedNick, final RSAPublicKey publicKey) {
boolean gotPreferedNick;
if (!contactsContainer.hasNickName(preferedNick)) {
- contactsContainer.addContact(publicKey, new ContactInformation(preferedNick));
+ contactsContainer.add(publicKey, new ContactInformation(preferedNick));
gotPreferedNick = true;
} else {
// need to append something to end to make it unique
- final String toAppend = microbloggingManager.duplicateNameAppender.getIntsToAppend(publicKey);
+ final String toAppend = node.mbClasses.duplicateNameAppender.getIntsToAppend(publicKey);
// TODO: very unlikely but does not check if the appended nick name with appended already exists
- contactsContainer.addContact(publicKey, new ContactInformation(preferedNick, toAppend));
+ contactsContainer.add(publicKey, new ContactInformation(preferedNick, toAppend));
gotPreferedNick = false;
}
addContactsToFile();
return gotPreferedNick;
}
+ public ContactInformation getContact(final RSAPublicKey publicKey) {
+ return contactsContainer.getContact(publicKey);
+ }
+
+ public boolean hasContact(final RSAPublicKey publicKey) {
+ return contactsContainer.hasContact(publicKey);
+ }
+
+ public List<ContactInformation> getContacts() {
+ return contactsContainer.getContacts();
+ }
+
private void addContactsToFile() {
logger.info("Adding contact to file");
try {
@@ -116,24 +125,24 @@ public String getFullNick() {
public static class ContactsContainer {
private final LinkedHashMap<RSAPublicKey, ContactInformation> contacts;
- public ContactsContainer() {
+ private ContactsContainer() {
contacts = new LinkedHashMap<RSAPublicKey, ContactInformation>();
}
- public synchronized void addContact(final RSAPublicKey publicKey, final ContactInformation contactInfo) {
+ private synchronized void add(final RSAPublicKey publicKey, final ContactInformation contactInfo) {
contacts.put(publicKey, contactInfo);
}
- public synchronized ContactInformation getContact(final RSAPublicKey publicKey) {
+ private synchronized ContactInformation getContact(final RSAPublicKey publicKey) {
return contacts.get(publicKey);
}
- public synchronized Collection<ContactInformation> getContacts() {
+ private synchronized List<ContactInformation> getContacts() {
final ArrayList<ContactInformation> contactsSnapShot = new ArrayList<ContactInformation>(contacts.values());
return contactsSnapShot;
}
- public synchronized boolean hasNickName(final String nickName) {
+ private synchronized boolean hasNickName(final String nickName) {
for (final ContactInformation contact : contacts.values()) {
if (contact.getFullNick().equals(nickName))
return true;
View
24 src/main/java/tahrir/io/net/microblogging/IncomingMicroblogHandler.java
@@ -0,0 +1,24 @@
+package tahrir.io.net.microblogging;
+
+import tahrir.io.net.microblogging.containers.*;
+import tahrir.io.net.microblogging.microblogs.MicroblogForBroadcast;
+
+/**
+ * Handles things to do with newly incoming microblogs.
+ *
+ * @author Kieran Donegan <kdonegan.92@gmail.com>
+ *
+ */
+public class IncomingMicroblogHandler {
+ MicroblogsForViewing mbsForViewing;
+ MicroblogsForBroadcast mbsForBroadcast;
+
+ public IncomingMicroblogHandler(final MicroblogsForViewing mbsForViewing, final MicroblogsForBroadcast mbsForBroadcast) {
+ this.mbsForBroadcast = mbsForBroadcast;
+ this.mbsForViewing = mbsForViewing;
+ }
+
+ public void handleInsertion(final MicroblogForBroadcast mbForBroadcast) {
+ microblogsForViewing.add(mb);
+ }
+}
View
28 ...et/microblogging/MicrobloggingManger.java → ...blogging/MicroblogBroadcastScheduler.java
@@ -1,38 +1,34 @@
package tahrir.io.net.microblogging;
-import java.io.File;
import java.util.*;
import java.util.concurrent.TimeUnit;
import tahrir.TrNode;
import tahrir.io.net.*;
import tahrir.io.net.TrPeerManager.TrPeerInfo;
+import tahrir.io.net.microblogging.containers.MicroblogsForBroadcast;
+import tahrir.io.net.microblogging.microblogs.MicroblogForBroadcast;
import tahrir.tools.TrUtils;
/**
- * Handles general microblogging tasks such as scheduling broadcast
+ * Schedules a single microblog for broadcast.
*
* @author Kieran Donegan <kdonegan.92@gmail.com>
*/
-public class MicrobloggingManger {
- private MicroblogContainer mbContainer;
-
- public ContactBook contactBook;
- public DuplicateNameAppender duplicateNameAppender;
-
+public class MicroblogBroadcastScheduler {
+ private MicroblogsForBroadcast mbsForBroadcast;
private TrNode node;
- private Microblog currentlyBroadcasting;
+ private MicroblogForBroadcast currentlyBroadcasting;
private Iterator<PhysicalNetworkLocation> peerIter;
- public MicrobloggingManger(final TrNode node) {
+ public MicroblogBroadcastScheduler(final TrNode node) {
// some nodes i.e seed nodes won't be running microblogging broadcast
if (node.config.peers.runBroadcast) {
this.node = node;
scheduleForLater();
- contactBook = new ContactBook(this, new File(node.rootDirectory, node.config.contacts));
- duplicateNameAppender = new DuplicateNameAppender(new File(node.rootDirectory, node.config.publicKeyChars));
- mbContainer = new MicroblogContainer(contactBook);
+
+ mbsForBroadcast = new MicroblogsForBroadcast();
}
}
@@ -53,7 +49,7 @@ protected void setupForNextMicroblog() {
if (snapShot.size() >= node.peerManager.config.minPeers) {
peerIter = snapShot.iterator();
- currentlyBroadcasting = mbContainer.getMicroblogForBroadcast();
+ currentlyBroadcasting = mbsForBroadcast.getMicroblogForBroadcast();
startBroadcastToPeer();
} else {
@@ -71,8 +67,8 @@ protected void startBroadcastToPeer() {
}
}
- public MicroblogContainer getMicroblogContainer() {
- return mbContainer;
+ public MicroblogsForBroadcast getMicroblogContainer() {
+ return mbsForBroadcast;
}
private void scheduleForLater() {
View
5 ...croblogging/MicoblogBroadcastSession.java → ...roblogging/MicroblogBroadcastSession.java
@@ -2,14 +2,15 @@
import tahrir.TrConstants;
import tahrir.io.net.TrSession;
+import tahrir.io.net.microblogging.microblogs.MicroblogForBroadcast;
import tahrir.io.net.sessions.*;
-public interface MicoblogBroadcastSession extends TrSession {
+public interface MicroblogBroadcastSession extends TrSession {
@Priority(TrConstants.MICROBLOG_BROADCAST_PRIORITY)
public void areYouInterested(final int mbHash);
@Priority(TrConstants.MICROBLOG_BROADCAST_PRIORITY)
- public void insertMicroblog(final Microblog mb);
+ public void insertMicroblog(final MicroblogForBroadcast mb);
@Priority(TrConstants.MICROBLOG_BROADCAST_PRIORITY)
public void interestIs(boolean interest);
View
24 src/main/java/tahrir/io/net/microblogging/MicroblogBroadcastSessionImpl.java
@@ -4,37 +4,40 @@
import tahrir.TrNode;
import tahrir.io.net.*;
+import tahrir.io.net.microblogging.microblogs.MicroblogForBroadcast;
/**
* Responsible for creating sessions for sending a single microblog between this node
* and another if probabilistic conditions met.
*
* @author Kieran Donegan <kdonegan.92@gmail.com>
*/
-public class MicroblogBroadcastSessionImpl extends TrSessionImpl implements MicoblogBroadcastSession {
+public class MicroblogBroadcastSessionImpl extends TrSessionImpl implements MicroblogBroadcastSession {
private static final Logger logger = LoggerFactory.getLogger(MicroblogBroadcastSessionImpl.class.getName());
- private Microblog beingSent;
- private MicoblogBroadcastSession receiverSess;
+ private MicroblogForBroadcast beingSent;
- private MicoblogBroadcastSession initiatorSess;
+ private MicroblogBroadcastSession receiverSess;
+ private MicroblogBroadcastSession initiatorSess;
private boolean nextBroadcastStarted;
+ private IncomingMicroblogHandler incomingMbHandler;
+
public MicroblogBroadcastSessionImpl(final Integer sessionId, final TrNode node, final TrSessionManager sessionMgr) {
super(sessionId, node, sessionMgr);
}
- public void startSingleBroadcast(final Microblog mbToBroadcast, final PhysicalNetworkLocation peerPhysicalLoc) {
+ public void startSingleBroadcast(final MicroblogForBroadcast mbToBroadcast, final PhysicalNetworkLocation peerPhysicalLoc) {
nextBroadcastStarted = false;
beingSent = mbToBroadcast;
- receiverSess = remoteSession(MicoblogBroadcastSession.class, connection(peerPhysicalLoc));
+ receiverSess = remoteSession(MicroblogBroadcastSession.class, connection(peerPhysicalLoc));
receiverSess.registerFailureListener(new OnFailureRun());
receiverSess.areYouInterested(beingSent.hashCode());
}
public void areYouInterested(final int mbHash) {
- initiatorSess = remoteSession(MicoblogBroadcastSession.class, connection(sender()));
+ initiatorSess = remoteSession(MicroblogBroadcastSession.class, connection(sender()));
initiatorSess.interestIs(!node.mbManager.getMicroblogContainer().isLikelyToContain(mbHash));
}
@@ -47,9 +50,10 @@ public void interestIs(final boolean interest) {
}
}
- public void insertMicroblog(final Microblog mb) {
- node.mbManager.getMicroblogContainer().insert(mb);
- // TODO: this is a workaround until we have a registerSuccessListener
+ public void insertMicroblog(final MicroblogForBroadcast mb) {
+ incomingMbHandler.handleInsertion(mb);
+ //node.mbManager.getMicroblogContainer().insert(mb);
+ // TODO: this is a workaround until we have a registerSuccessListener()
initiatorSess.sessionFinished();
}
View
102 src/main/java/tahrir/io/net/microblogging/MicroblogContainer.java
@@ -1,102 +0,0 @@
-package tahrir.io.net.microblogging;
-
-import java.util.*;
-
-import tahrir.TrConstants;
-
-import com.google.common.collect.Sets;
-import com.google.common.eventbus.EventBus;
-
-public class MicroblogContainer {
- private final PriorityQueue<Microblog> microblogBroadcastQueue = new PriorityQueue<Microblog>(11, new MicroblogPriorityComparator());
- private final Set<Integer> seen = Sets.newLinkedHashSet();
-
- private final MicroblogsForViewing microblogsForViewing = new MicroblogsForViewing();
-
- private final ContactBook contactBook;
-
- public final EventBus eventBus = new EventBus();
-
- public MicroblogContainer(final ContactBook contactBook) {
- this.contactBook = contactBook;
- }
-
- public Microblog getMicroblogForBroadcast() {
- return microblogBroadcastQueue.poll();
- }
-
- public void changeBroadcastPriority(final Microblog mb, final int priority) {
- microblogBroadcastQueue.remove(mb);
- mb.priority = priority;
- microblogBroadcastQueue.add(mb);
- }
-
- public boolean isLikelyToContain(final int microblogHash) {
- return seen.contains(microblogHash);
- }
-
- public boolean contains(final Microblog mb) {
- return microblogBroadcastQueue.contains(mb);
- }
-
- public void insert(final Microblog mb) {
- // this check probably isn't necessary but just to be sure...
- if (!microblogBroadcastQueue.contains(mb)) {
- microblogBroadcastQueue.add(mb);
- seen.add(mb.hashCode());
- }
- if (microblogsForViewing.add(mb)) {
- eventBus.post(mb);
- }
- }
-
- public Iterator<Microblog> getMicroblogsViewingIter() {
- return microblogsForViewing.iterator();
- }
-
-
- private class MicroblogsForViewing implements Iterable<Microblog> {
- private final SortedSet<Microblog> microblogsViewingSet = Collections.synchronizedSortedSet(new TreeSet<Microblog>(new MicroblogTimeComparator()));
-
- private boolean add(final Microblog mb) {
- // TODO: may want to increase priority if added
- if (microblogsViewingSet.size() < TrConstants.MAX_MICROBLOGS_FOR_VIEWING) {
- microblogsViewingSet.add(mb);
- return true;
- }
- if (shouldAddByReplacement(mb)) {
- microblogsViewingSet.remove(microblogsViewingSet.last());
- microblogsViewingSet.add(mb);
- return true;
- }
- return false;
- }
-
- // TODO: probably needs to check more things
- private boolean shouldAddByReplacement(final Microblog mb) {
- if (contactBook.contactsContainer.getContact(mb.publicKey) != null)
- return true;
- else
- return false;
- }
-
- @Override
- public Iterator<Microblog> iterator() {
- return microblogsViewingSet.iterator();
- }
- }
-
- private class MicroblogPriorityComparator implements Comparator<Microblog> {
- @Override
- public int compare(final Microblog mb1, final Microblog mb2) {
- return Double.compare(mb1.priority, mb2.priority);
- }
- }
-
- private class MicroblogTimeComparator implements Comparator<Microblog> {
- @Override
- public int compare(final Microblog mb1, final Microblog mb2) {
- return Double.compare(mb2.timeCreated, mb1.timeCreated);
- }
- }
-}
View
58 src/main/java/tahrir/io/net/microblogging/containers/MicroblogsForBroadcast.java
@@ -0,0 +1,58 @@
+package tahrir.io.net.microblogging.containers;
+
+import java.util.*;
+
+import tahrir.io.net.microblogging.microblogs.MicroblogForBroadcast;
+
+import com.google.common.collect.Sets;
+
+public class MicroblogsForBroadcast {
+ private final PriorityQueue<MicroblogForBroadcast> microblogBroadcastQueue;
+ private final Set<Integer> seen;
+
+ public MicroblogsForBroadcast() {
+ microblogBroadcastQueue = new PriorityQueue<MicroblogForBroadcast>(11, new MicroblogPriorityComparator());
+ seen = Sets.newLinkedHashSet();
+ }
+
+ public MicroblogForBroadcast getMicroblogForBroadcast() {
+ return microblogBroadcastQueue.poll();
+ }
+
+ public void changeBroadcastPriority(final MicroblogForBroadcast mb, final int priority) {
+ microblogBroadcastQueue.remove(mb);
+ mb.priority = priority;
+ microblogBroadcastQueue.add(mb);
+ }
+
+ public boolean isLikelyToContain(final int microblogHash) {
+ return seen.contains(microblogHash);
+ }
+
+ public boolean contains(final MicroblogForBroadcast mb) {
+ return microblogBroadcastQueue.contains(mb);
+ }
+
+ public boolean insert(final MicroblogForBroadcast mb) {
+ final boolean inserted = false;
+
+ seen.add(mb.hashCode());
+ // this check probably isn't necessary but just to be sure...
+ if (!microblogBroadcastQueue.contains(mb)) {
+ // TODO: it doesn't check the size of the queue, it may get too big
+ microblogBroadcastQueue.add(mb);
+ }
+ return inserted;
+ }
+
+ public boolean remove(final MicroblogForBroadcast mb) {
+ return microblogBroadcastQueue.remove(mb);
+ }
+
+ private class MicroblogPriorityComparator implements Comparator<MicroblogForBroadcast> {
+ @Override
+ public int compare(final MicroblogForBroadcast mb1, final MicroblogForBroadcast mb2) {
+ return Double.compare(mb1.priority, mb2.priority);
+ }
+ }
+}
View
55 src/main/java/tahrir/io/net/microblogging/containers/MicroblogsForViewing.java
@@ -0,0 +1,55 @@
+package tahrir.io.net.microblogging.containers;
+
+import java.util.*;
+
+import tahrir.TrConstants;
+import tahrir.io.net.microblogging.ContactBook;
+import tahrir.io.net.microblogging.microblogs.MicroblogForBroadcast;
+
+public class MicroblogsForViewing implements Iterable<MicroblogForBroadcast> {
+ private final SortedSet<MicroblogForBroadcast> microblogsViewingSet = Collections.synchronizedSortedSet(new TreeSet<MicroblogForBroadcast>(new MicroblogTimeComparator()));
+ private final ContactBook contactBook;
+ private final MicroblogTimeComparator timeComparator;
+
+ public MicroblogsForViewing(final ContactBook contactBook) {
+ this.contactBook = contactBook;
+ timeComparator = new MicroblogTimeComparator();
+ }
+
+ public boolean insert(final MicroblogForBroadcast mb) {
+ boolean inserted = false;
+ // TODO: may want to increase priority if added
+ if (!isFull() || shouldAddByReplacement(mb)) {
+ microblogsViewingSet.add(mb);
+ inserted = true;
+ if (isFull()) {
+ microblogsViewingSet.remove(microblogsViewingSet.last());
+ }
+ }
+ return inserted;
+ }
+
+ @Override
+ public Iterator<MicroblogForBroadcast> iterator() {
+ return microblogsViewingSet.iterator();
+ }
+
+ private boolean shouldAddByReplacement(final MicroblogForBroadcast mb) {
+ return contactBook.hasContact(mb.publicKey) || isNewerThanLast(mb);
+ }
+
+ private boolean isNewerThanLast(final MicroblogForBroadcast mb) {
+ return timeComparator.compare(microblogsViewingSet.last(), mb) > 0;
+ }
+
+ private boolean isFull() {
+ return microblogsViewingSet.size() > TrConstants.MAX_MICROBLOGS_FOR_VIEWING;
+ }
+
+ private class MicroblogTimeComparator implements Comparator<MicroblogForBroadcast> {
+ @Override
+ public int compare(final MicroblogForBroadcast mb1, final MicroblogForBroadcast mb2) {
+ return Double.compare(mb2.timeCreated, mb1.timeCreated);
+ }
+ }
+}
View
3 src/main/java/tahrir/io/net/microblogging/filters/ContactsFilter.java
@@ -1,6 +1,7 @@
package tahrir.io.net.microblogging.filters;
import tahrir.io.net.microblogging.*;
+import tahrir.io.net.microblogging.microblogs.MicroblogForBroadcast;
public class ContactsFilter implements MicroblogFilter {
private final ContactBook contactBook;
@@ -10,7 +11,7 @@ public ContactsFilter(final ContactBook contactBook) {
}
@Override
- public boolean passesFilter(final Microblog mb) {
+ public boolean passesFilter(final MicroblogForBroadcast mb) {
if (contactBook.contactsContainer.hasContact(mb.publicKey))
return true;
else
View
4 src/main/java/tahrir/io/net/microblogging/filters/MicroblogFilter.java
@@ -1,7 +1,7 @@
package tahrir.io.net.microblogging.filters;
-import tahrir.io.net.microblogging.Microblog;
+import tahrir.io.net.microblogging.microblogs.MicroblogForBroadcast;
public interface MicroblogFilter {
- public boolean passesFilter(Microblog mb);
+ public boolean passesFilter(MicroblogForBroadcast mb);
}
View
4 src/main/java/tahrir/io/net/microblogging/filters/Unfiltered.java
@@ -1,10 +1,10 @@
package tahrir.io.net.microblogging.filters;
-import tahrir.io.net.microblogging.Microblog;
+import tahrir.io.net.microblogging.microblogs.MicroblogForBroadcast;
public class Unfiltered implements MicroblogFilter {
@Override
- public boolean passesFilter(final Microblog mb) {
+ public boolean passesFilter(final MicroblogForBroadcast mb) {
return true;
}
}
View
4 src/main/java/tahrir/io/net/microblogging/filters/UserFilter.java
@@ -2,7 +2,7 @@
import java.security.interfaces.RSAPublicKey;
-import tahrir.io.net.microblogging.Microblog;
+import tahrir.io.net.microblogging.microblogs.MicroblogForBroadcast;
public class UserFilter implements MicroblogFilter {
private final RSAPublicKey userPubKey;
@@ -12,7 +12,7 @@ public UserFilter(final RSAPublicKey userPubKey) {
}
@Override
- public boolean passesFilter(final Microblog mb) {
+ public boolean passesFilter(final MicroblogForBroadcast mb) {
if (mb.publicKey.equals(userPubKey))
return true;
else
View
12 ...ahrir/io/net/microblogging/Microblog.java → ...ing/microblogs/MicroblogForBroadcast.java
@@ -1,11 +1,11 @@
-package tahrir.io.net.microblogging;
+package tahrir.io.net.microblogging.microblogs;
import java.security.interfaces.RSAPublicKey;
import tahrir.*;
import tahrir.io.crypto.*;
-public class Microblog {
+public class MicroblogForBroadcast {
public int priority;
public TrSignature signature;
public String languageCode;
@@ -15,15 +15,15 @@
public long timeCreated;
// for serialization
- public Microblog() {
+ public MicroblogForBroadcast() {
}
- public Microblog(final TrNode creatingNode, final String message) {
+ public MicroblogForBroadcast(final TrNode creatingNode, final String message) {
this(creatingNode, message, TrConstants.BROADCAST_INIT_PRIORITY);
}
- public Microblog(final TrNode creatingNode, final String message, final int priority) {
+ public MicroblogForBroadcast(final TrNode creatingNode, final String message, final int priority) {
this.priority = priority;
timeCreated = System.currentTimeMillis();
this.message = message;
@@ -53,7 +53,7 @@ public boolean equals(final Object obj) {
return false;
if (getClass() != obj.getClass())
return false;
- final Microblog other = (Microblog) obj;
+ final MicroblogForBroadcast other = (MicroblogForBroadcast) obj;
if (signature == null) {
if (other.signature != null)
return false;
View
69 src/main/java/tahrir/io/net/microblogging/microblogs/ParsedMicroblog.java
@@ -0,0 +1,69 @@
+package tahrir.io.net.microblogging.microblogs;
+
+import java.security.interfaces.RSAPublicKey;
+import java.util.Map;
+
+import nu.xom.*;
+
+import org.slf4j.*;
+
+import tahrir.tools.TrUtils;
+
+import com.google.common.collect.MapMaker;
+
+/**
+ * Represents a microblog whose message (a String in XML format) has been parsed.
+ *
+ * @author Kieran Donegan <kdonegan.92@gmail.com>
+ */
+
+public class ParsedMicroblog {
+ private static final Logger logger = LoggerFactory.getLogger(ParsedMicroblog.class);
+
+ // Integers represent where they appeared in document
+ private final Map<RSAPublicKey, Integer> mentions = new MapMaker().makeMap();
+ private final Map<String, Integer> text = new MapMaker().makeMap();
+
+ private final MicroblogForBroadcast parsedFrom;
+
+ public ParsedMicroblog(final MicroblogForBroadcast parsedFrom) throws Exception {
+ this.parsedFrom = parsedFrom;
+ parseMessage();
+ }
+
+ public boolean hasMention(final RSAPublicKey userKey) {
+ return mentions.containsKey(userKey);
+ }
+
+ private void parseMessage() throws Exception {
+ final Builder parser = new Builder();
+ final Document doc = parser.build(parsedFrom.message, null);
+ final Element root = doc.getRootElement();
+ processFromRoot(root);
+ }
+
+ // TODO: this is messy... maybe use command pattern?
+ private void processFromRoot(final Element root) throws Exception {
+ final Elements elements = root.getChildElements();
+ for (int i = 0; i < elements.size(); i++) {
+ final Element element = elements.get(i);
+ if (element.getQualifiedName().equals("txt")) {
+ handleTxtElement(element, i);
+ } else if (element.getQualifiedName().equals("mention")) {
+ handleMention(element, i);
+ } else {
+ logger.error("Unrecoginsed element");
+ throw new Exception();
+ }
+ }
+ }
+
+ private void handleTxtElement(final Element element, final int position) {
+ text.put(element.getValue(), position);
+ }
+
+ private void handleMention(final Element element, final int position) {
+ final RSAPublicKey pubKey = TrUtils.getPublicKey(element.getValue());
+ mentions.put(pubKey, position);
+ }
+}
View
16 src/main/java/tahrir/tools/TrUtils.java
@@ -4,12 +4,17 @@
import java.lang.reflect.Type;
import java.security.*;
import java.security.interfaces.RSAPublicKey;
+import java.security.spec.X509EncodedKeySpec;
import java.util.Random;
import java.util.concurrent.*;
import net.sf.doodleproject.numerics4j.random.RandomRNG;
+
+import org.apache.commons.codec.binary.Base64;
+
import tahrir.*;
+import com.google.common.eventbus.EventBus;
import com.google.gson.*;
public class TrUtils {
@@ -27,6 +32,8 @@ public void run() {
public static final Gson gson;
+ public static final EventBus eventBus = new EventBus();
+
static {
final GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(RSAPublicKey.class, new PublicKeyInstanceCreator());
@@ -72,6 +79,15 @@ public static void readAllBytes(final byte[] buffer, final DataInputStream dis)
}
}
+ public static RSAPublicKey getPublicKey(final String pubKeyString) {
+ final byte[] bytes = Base64.decodeBase64(pubKeyString);
+ try {
+ return (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(bytes));
+ } catch (final Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
/*
* These methods are only for testing purposes.
*/
View
9 src/main/java/tahrir/ui/MicroblogMessageParser.java
@@ -1,9 +0,0 @@
-package tahrir.ui;
-
-import javax.swing.text.StyledDocument;
-
-public class MicroblogMessageParser {
- public static void parseMicroblogToDoc(final String microblogMessage, final StyledDocument doc) {
-
- }
-}
View
9 src/main/java/tahrir/ui/MicroblogPost.java
@@ -9,17 +9,20 @@
import net.miginfocom.swing.MigLayout;
import tahrir.TrConstants;
-import tahrir.io.net.microblogging.Microblog;
+import tahrir.io.net.microblogging.filters.UserFilter;
+import tahrir.io.net.microblogging.microblogs.MicroblogForBroadcast;
public class MicroblogPost {
private final JPanel contentPanel;
- public MicroblogPost(final Microblog mb, final TrMainWindow mainWindow) {
+ public MicroblogPost(final MicroblogForBroadcast mb, final TrMainWindow mainWindow) {
contentPanel = new JPanel(new MigLayout());
contentPanel.setBackground(Color.WHITE);
contentPanel.setBorder(BorderFactory.createRaisedSoftBevelBorder());
- final TabCreateButton authorNick = new TabCreateButton(mb.authorNick, mb.authorNick, mainWindow, mb.publicKey);
+ // create a button which bring up tab with author's microblogs when clicked
+ final MicroblogViewingPage authorViewingPage = new MicroblogViewingPage(null, new UserFilter(mb.publicKey), mainWindow);
+ final TabCreateButton authorNick = new TabCreateButton(mainWindow, mb.authorNick, authorViewingPage.getContentPane());
authorNick.setFont(new Font("bold", Font.BOLD, authorNick.getFont().getSize() + 2));
contentPanel.add(authorNick, "align left");
View
18 src/main/java/tahrir/ui/MicroblogViewingPage.java
@@ -8,15 +8,17 @@
import net.miginfocom.swing.MigLayout;
import tahrir.TrNode;
import tahrir.io.net.microblogging.*;
+import tahrir.io.net.microblogging.containers.MicroblogsForBroadcast;
import tahrir.io.net.microblogging.filters.MicroblogFilter;
+import tahrir.io.net.microblogging.microblogs.MicroblogForBroadcast;
import com.google.common.collect.Lists;
import com.google.common.eventbus.Subscribe;
public class MicroblogViewingPage {
private final MicroblogFilter filter;
- private final MicroblogContainer microblogContainer;
+ private final MicroblogsForBroadcast microblogContainer;
private final JPanel content;
private final JScrollPane scroller;
@@ -47,7 +49,7 @@ public MicroblogViewingPage(final TrNode node, final MicroblogFilter filter, fin
}
@Subscribe
- public void listenForNewMicroblog(final Microblog mb) {
+ public void listenForNewMicroblog(final MicroblogForBroadcast mb) {
if (filter.passesFilter(mb)) {
if (!loadNewPostsButton.presentInGUI) {
content.removeAll();
@@ -62,12 +64,12 @@ public JComponent getContentPane() {
return scroller;
}
- private LinkedList<Microblog> getFilteredMicroblogs() {
- final LinkedList<Microblog> microblogs = Lists.newLinkedList();
+ private LinkedList<MicroblogForBroadcast> getFilteredMicroblogs() {
+ final LinkedList<MicroblogForBroadcast> microblogs = Lists.newLinkedList();
- final Iterator<Microblog> iter = microblogContainer.getMicroblogsViewingIter();
+ final Iterator<MicroblogForBroadcast> iter = microblogContainer.getMicroblogsViewingIter();
while (iter.hasNext()) {
- final Microblog mb = iter.next();
+ final MicroblogForBroadcast mb = iter.next();
if (filter.passesFilter(mb)) {
microblogs.add(mb);
}
@@ -77,10 +79,10 @@ public JComponent getContentPane() {
}
private void addMicroblogs() {
- final LinkedList<Microblog> microblogs = getFilteredMicroblogs();
+ final LinkedList<MicroblogForBroadcast> microblogs = getFilteredMicroblogs();
if (microblogs.size() > 0) {
- for (final Microblog mb : microblogs) {
+ for (final MicroblogForBroadcast mb : microblogs) {
final MicroblogPost microblogPost = new MicroblogPost(mb, mainWindow);
content.add(microblogPost.getContentPanel(), "wrap, span");
}
View
22 src/main/java/tahrir/ui/TabCreateButton.java
@@ -0,0 +1,22 @@
+package tahrir.ui;
+
+import java.awt.Component;
+import java.awt.event.*;
+
+public class TabCreateButton extends TransparentButton implements ActionListener {
+ private final TrMainWindow mainWindow;
+ private final String tabName;
+ private final Component tabContents;
+
+ public TabCreateButton(final TrMainWindow mainWindow, final String tabName, final Component tabContents) {
+ super(null , null);
+ this.mainWindow = mainWindow;
+ this.tabContents = tabContents;
+ this.tabName = tabName;
+ }
+
+ @Override
+ public void actionPerformed(final ActionEvent arg0) {
+ mainWindow.createClosableTab(tabName, tabContents);
+ }
+}
View
8 src/main/java/tahrir/ui/TrMainWindow.java
@@ -2,7 +2,6 @@
import java.awt.*;
import java.awt.event.*;
-import java.security.interfaces.RSAPublicKey;
import javax.swing.*;
@@ -26,7 +25,6 @@ public TrMainWindow(final TrNode node) {
this.node = node;
contentPanel = new JPanel(new MigLayout());
-
tabbedPane = new JTabbedPane();
tabbedPane.setPreferredSize(new Dimension(TrConstants.GUI_WIDTH_PX, TrConstants.GUI_HEIGHT_PX - 120));
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
@@ -51,8 +49,8 @@ public TrMainWindow(final TrNode node) {
frame.setVisible(true);
}
- public void createClosableTab(final String tabName, final RSAPublicKey userToFilter) {
- tabbedPane.insertTab(null, null, new MicroblogViewingPage(node, new UserFilter(userToFilter), this).getContentPane(), null, tabbedPane.getTabCount());
+ public void createClosableTab(final String tabName, final Component tabContents) {
+ tabbedPane.insertTab(tabName, null, tabContents, null, tabbedPane.getTabCount());
final int newTabIndex = tabbedPane.getTabCount() - 1;
tabbedPane.setSelectedIndex(newTabIndex);
tabbedPane.setTabComponentAt(newTabIndex, new ClosableTabComponent(tabName));
@@ -101,7 +99,7 @@ public ClosableTabComponent(final String tabName) {
ClosableTabComponent parent;
public CloseTabButton(final ClosableTabComponent parent) {
- super(new ImageIcon(TrConstants.MAIN_WINDOW_ARTWORK_PATH + "close-tab.png"), "close this tab");
+ super(new ImageIcon(TrConstants.MAIN_WINDOW_ARTWORK_PATH + "close-tab.png"), "Close this tab");
this.parent = parent;
//setRolloverEnabled(true);
//setRolloverIcon(new ImageIcon(TrConstants.MAIN_WINDOW_ARTWORK_PATH + "close-tab-hover.png"));
View
13 src/main/java/tahrir/ui/TransparentButton.java
@@ -0,0 +1,13 @@
+package tahrir.ui;
+
+import javax.swing.*;
+
+public class TransparentButton extends JButton {
+ public TransparentButton(final ImageIcon icon, final String toolTip) {
+ super(icon);
+ setToolTipText(toolTip);
+ setToolTipText("Close this tab");
+ setContentAreaFilled(false);
+ setFocusable(false);
+ }
+}
View
7 src/test/java/tahrir/io/net/microblogging/MicroblogBroadcastTest.java
@@ -5,6 +5,7 @@
import tahrir.TrNode;
import tahrir.io.net.TrPeerManager.TrPeerInfo;
+import tahrir.io.net.microblogging.microblogs.MicroblogForBroadcast;
import tahrir.tools.TrUtils;
public class MicroblogBroadcastTest {
@@ -25,7 +26,7 @@ public void setUpNodes() throws Exception {
@Test
public void simpleTest() throws Exception {
- final Microblog testMb = new Microblog(sendingNode, "Hello world");
+ final MicroblogForBroadcast testMb = new MicroblogForBroadcast(sendingNode, "Hello world");
sendingNode.mbManager.getMicroblogContainer().insert(testMb);
// don't want initial wait
@@ -43,8 +44,8 @@ public void simpleTest() throws Exception {
@Test
public void priorityTest() throws Exception {
- final Microblog testMb0 = new Microblog(sendingNode, "You SHOULD have this microblog!", 0);
- final Microblog testMb1 = new Microblog(sendingNode, "You should NOT have this microblog!", Integer.MAX_VALUE);
+ final MicroblogForBroadcast testMb0 = new MicroblogForBroadcast(sendingNode, "You SHOULD have this microblog!", 0);
+ final MicroblogForBroadcast testMb1 = new MicroblogForBroadcast(sendingNode, "You should NOT have this microblog!", Integer.MAX_VALUE);
sendingNode.mbManager.getMicroblogContainer().insert(testMb0);
sendingNode.mbManager.getMicroblogContainer().insert(testMb1);
View
10 src/test/java/tahrir/ui/GUITest.java
@@ -4,7 +4,7 @@
import tahrir.TrNode;
import tahrir.io.crypto.TrCrypto;
-import tahrir.io.net.microblogging.Microblog;
+import tahrir.io.net.microblogging.microblogs.MicroblogForBroadcast;
import tahrir.tools.TrUtils;
public class GUITest {
@@ -23,10 +23,10 @@ public static void main(final String[] args) {
}
public static void addDummyMicroblogs(final TrNode node) {
- final Microblog testMb0 = new Microblog(node, "Ut non tellus at massa tincidunt malesuada sodales eu nunc. Proin in lectus sit amet mi tempor dictum. Nunc ullamcorper ornare metus eu ultrices. Maecenas vel erat id justo bibendum.");
- final Microblog testMb1 = new Microblog(node, "Curabitur in elit in tortor viverra fringilla. In sed tincidunt.");
- final Microblog testMb2 = new Microblog(node, "Suspendisse congue eleifend nunc sagittis euismod. Curabitur auctor nibh mauris, ut faucibus est. Class aptent taciti sociosqu ad litora torquent.");
- final Microblog testMb3 = new Microblog(node, "Maecenas sed neque nisi. Suspendisse at velit urna, sed fermentum velit. Cum sociis natoque penatibus.");
+ final MicroblogForBroadcast testMb0 = new MicroblogForBroadcast(node, "Ut non tellus at massa tincidunt malesuada sodales eu nunc. Proin in lectus sit amet mi tempor dictum. Nunc ullamcorper ornare metus eu ultrices. Maecenas vel erat id justo bibendum.");
+ final MicroblogForBroadcast testMb1 = new MicroblogForBroadcast(node, "Curabitur in elit in tortor viverra fringilla. In sed tincidunt.");
+ final MicroblogForBroadcast testMb2 = new MicroblogForBroadcast(node, "Suspendisse congue eleifend nunc sagittis euismod. Curabitur auctor nibh mauris, ut faucibus est. Class aptent taciti sociosqu ad litora torquent.");
+ final MicroblogForBroadcast testMb3 = new MicroblogForBroadcast(node, "Maecenas sed neque nisi. Suspendisse at velit urna, sed fermentum velit. Cum sociis natoque penatibus.");
testMb0.authorNick = "chuck-norris";
testMb1.authorNick = "sanity";

0 comments on commit 4d863b1

Please sign in to comment.