Skip to content

Commit

Permalink
Merge pull request #38 from tomamic/master
Browse files Browse the repository at this point in the history
chat code complete clean-up
  • Loading branch information
rik0 committed Mar 3, 2013
2 parents d5376b6 + 234068e commit 2e0f697
Show file tree
Hide file tree
Showing 29 changed files with 711 additions and 1,785 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.svn
.classpath
.project
.metadata
*.jar
.idea
$config
Expand Down
2 changes: 1 addition & 1 deletion blogracy-vuze/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<resources>
<resource>
<directory>src/main/plugins</directory>
<targetPath>../plugins/</targetPath>
<targetPath>plugins</targetPath>
</resource>
<!--resource>
<directory>src/main/java/net/blogracy/chat/resources</directory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/
package com.aelitis.azureus.plugins.chat;

// Blogracy: removed all references to swt and ui

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand Down
20 changes: 20 additions & 0 deletions blogracy-vuze/src/main/java/net/blogracy/Blogracy.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
*/
package net.blogracy;

import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
Expand All @@ -23,6 +26,9 @@
import org.gudy.azureus2.plugins.Plugin;
import org.gudy.azureus2.plugins.PluginException;
import org.gudy.azureus2.plugins.PluginInterface;
import org.gudy.azureus2.plugins.PluginManager;

import com.aelitis.azureus.plugins.chat.ChatPlugin;

/**
* @author mic
Expand Down Expand Up @@ -80,10 +86,24 @@ public static void main(String[] args) {
} catch (Exception e) {
e.printStackTrace();
}

try {
(new java.io.File("plugins/chat")).mkdirs();
Properties chatProp = new Properties();
chatProp.load(Blogracy.class.getClassLoader().getResourceAsStream("plugins/chat/plugin.properties"));
chatProp.store(new FileWriter("plugins/chat/plugin.properties"), "Chat plugin");
(new java.io.File("plugins/blogracy")).mkdirs();
Properties blogracyProp = new Properties();
blogracyProp.load(Blogracy.class.getClassLoader().getResourceAsStream("plugins/blogracy/plugin.properties"));
blogracyProp.store(new FileWriter("plugins/blogracy/plugin.properties"), "Blogracy plugin");
} catch (Exception e) {
e.printStackTrace();
}

List<String> argList = new ArrayList<String>();
argList.addAll(Arrays.asList(args));
argList.add("--ui=console");
org.gudy.azureus2.ui.common.Main.main(argList.toArray(args));
//PluginManager.startAzureus(PluginManager.UI_NONE, new java.util.Properties());
}
}
113 changes: 66 additions & 47 deletions blogracy-vuze/src/main/java/net/blogracy/services/ChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
import java.util.HashMap;


public class ChatService implements MessageListener {

public class ChatService implements javax.jms.MessageListener, com.aelitis.azureus.plugins.chat.messaging.MessageListener {
private PluginInterface vuze;

private Session session;
Expand All @@ -46,9 +45,11 @@ public class ChatService implements MessageListener {
private MessageConsumer consumer;

private String TOPIC_NAME = "CHAT.DEMO";
private String lastMessageSend = "";
private String lastMessageSent = "";
private String lastMessageReceived = "";

private String lastMsg = "";

private HashMap<String, Download> channels = new HashMap<String, Download>();

public ChatService(Connection connection, PluginInterface vuze) {
Expand All @@ -61,7 +62,7 @@ public ChatService(Connection connection, PluginInterface vuze) {
consumer = session.createConsumer(topic);
consumer.setMessageListener(this);
} catch (JMSException e) {
System.out.println("JMS error: creating the text listener");
Logger.error("JMS error: creating the text listener");
}
}

Expand All @@ -70,32 +71,51 @@ public void onMessage(Message message) {
if (message instanceof TextMessage) {
try {
String text = ((TextMessage) message).getText();
if (floodControl(text)) {
/*System.out.println("Flooding");*/
} else {
parseXML(text);
lastMessageSend = text;
//System.out.println("Outgoing Message: " + text);
}
parseXML(text);
} catch (JMSException e) {
System.out.println("JMS error: reading messages");
Logger.error("JMS error: reading messages");
}
}
else {
Logger.error("This is not a TextMessage");
}
}

@Override
public void downloadAdded(Download download) {}

@Override
public void downloadRemoved(Download download) {}

@Override
public void downloadActive(Download download) {}

@Override
public void downloadInactive(Download download) {}

@Override
public void messageReceived(Download download, byte[] sender, String nick, String text) {
for (String channel : channels.keySet()) {
if (channels.get(channel) == download) {
String type = "chat";
if (text.equals("has left the channel")) type = "leave";
msgFromVuze(channel, type, nick, text);
}
}
else System.out.println("This is not a TextMessage");
}

public void msgFromVuze(String channel, String type, String nick, String text){
String xml = createXML(channel, type, nick, text);
//System.out.println("Incoming Message: " + XMLmessage);
lastMessageReceived = xml;

TextMessage message;
try {
message = session.createTextMessage();
message.setText(xml);
producer.send(topic, message);
if (floodControl(channel, type, nick, text)) {
String xml = createXML(channel, type, nick, text);
Logger.info("Incoming Message: " + xml);
TextMessage message = session.createTextMessage();
message.setText(xml);
producer.send(topic, message);
}
} catch (JMSException e) {
System.out.println("JMS error: sending messages");
Logger.error("JMS error: sending messages");
}
}

Expand All @@ -109,17 +129,23 @@ private void parseXML(String xml){

NodeList nodes = doc.getElementsByTagName("message");
Element element = (Element) nodes.item(0);
String channel = element.getAttribute("channel");
String type = element.getAttribute("type");
String nick = element.getAttribute("from");
String channel = element.getAttribute("channel");
String text = element.getTextContent();
msgToVuze(channel, type, nick, text);

if (floodControl(channel, type, nick, text)) {
Logger.info("Outgoing Message: " + xml);
msgToVuze(channel, type, nick, text);
}

} catch (Exception e) {
e.printStackTrace();
}
}

private String createXML(String channel, String type, String nick, String text){
String xml = "";
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Expand All @@ -137,12 +163,11 @@ private String createXML(String channel, String type, String nick, String text){
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(source, result);
String str = stringWriter.getBuffer().toString();//.substring(38);
return str;
xml = stringWriter.getBuffer().toString().substring(38);
} catch (Exception e) {
e.printStackTrace();
return "nothing";
}
return xml;
}

private void msgToVuze(String channel, String type, String nick, String text) {
Expand All @@ -151,41 +176,35 @@ private void msgToVuze(String channel, String type, String nick, String text) {

if (type.equals("chat")) {
Download download = channels.get(channel);
plugin.sendMessage(download, text);
if (download != null) {
plugin.sendMessage(download, text);
}
} else if (type.equals("join")) {
Logger.info("Joining channel: " + channel);

plugin.setNick(nick);
Download download = plugin.addChannel(channel);
channels.put(channel, download);
plugin.addMessageListener(new com.aelitis.azureus.plugins.chat.messaging.MessageListener() {
public void downloadAdded(Download download) {}
public void downloadRemoved(Download download) {}
public void downloadActive(Download download) {}
public void downloadInactive(Download download) {}
public void messageReceived(Download download, byte[] sender, String nick, String text) {
String channel = new String(sender);
for (String key : channels.keySet()) {
if (channels.get(key) == download) channel = key;
}
String type = "chat";
if (text.equals("has left the channel")) type = "leave";
msgFromVuze(channel, type, nick, text);
}
}, download);
plugin.addMessageListener(this, download);
Logger.info("chat request: " + channel);

} else if (type.equals("leave")){
Download download = channels.get(channel);
plugin.sendMessage(download, "has left the channel");
if (download != null) {
plugin.sendMessage(download, "has left the channel");
}
if (nick.equals(plugin.getNick())) {
// plugin.closeBridge(channelName);
}
}
}

private boolean floodControl(String text){
return (text.equals(lastMessageReceived)
||text.equals(lastMessageSend));
private boolean floodControl(String channel, String type, String nick, String text) {
String msg = channel + ":" + type + ":" + nick + ":" + text;
if (! lastMsg.equals(msg)) {
lastMsg = msg;
return true;
}
return false;
}
}
2 changes: 1 addition & 1 deletion blogracy-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
</resource>
<resource>
<directory>src/main/webapp</directory>
<targetPath>../webapp</targetPath>
<targetPath>webapp</targetPath>
</resource>
</resources>

Expand Down
Binary file modified blogracy-web/src/main/config/blogracy.jks
Binary file not shown.
2 changes: 1 addition & 1 deletion blogracy-web/src/main/config/blogracyPaths.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
#

blogracy.paths.root = webapp
blogracy.paths.cache = webapp/cache
blogracy.paths.cache = cache

11 changes: 7 additions & 4 deletions blogracy-web/src/main/java/net/blogracy/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ public class WebServer
{
public static void main(String[] args) throws Exception
{
String webDir = WebServer.class.getClassLoader().getResource("webapp").toExternalForm();

Server server = new Server(8080);

WebAppContext context = new WebAppContext();
context.setDescriptor("webapp/WEB-INF/web.xml");
context.setResourceBase("webapp");
context.setResourceBase(webDir);
//context.setDescriptor(webDir + "/WEB-INF/web.xml");
context.setContextPath("/");
context.setParentLoaderPriority(true);

Expand All @@ -30,14 +32,15 @@ public static void main(String[] args) throws Exception
List<User> friends = Configurations.getUserConfig().getFriends();
for (User friend : friends) {
String hash = friend.getHash().toString();
ChatController.createChannel(hash);
ChatController.getSingleton().joinChannel(hash);
}
String id = Configurations.getUserConfig().getUser().getHash().toString();
ChatController.getSingleton().joinChannel(id);

int TOTAL_WAIT = 5 * 60 * 1000; // 5 minutes

while (true) {
FileSharing sharing = FileSharing.getSingleton();
String id = Configurations.getUserConfig().getUser().getHash().toString();
sharing.addFeedEntry(id, "" + new java.util.Date(), null);

// List<User> friends = Configurations.getUserConfig().getFriends();
Expand Down
Loading

0 comments on commit 2e0f697

Please sign in to comment.