[Zerofile-svn] SF.net SVN: zerofile: [57] trunk/src
Status: Pre-Alpha
Brought to you by:
karl-bengtsson
|
From: <kar...@us...> - 2007-11-27 15:38:31
|
Revision: 57
http://zerofile.svn.sourceforge.net/zerofile/?rev=57&view=rev
Author: karl-bengtsson
Date: 2007-11-27 07:38:23 -0800 (Tue, 27 Nov 2007)
Log Message:
-----------
Implemented DOM XMPP parsing and basic message handling thingys. And various improvements. Huzzah!
Modified Paths:
--------------
trunk/src/XMPPLinkLocalChatSession.java
trunk/src/ZeroFileChatWindow.java
trunk/src/ZeroconfBrowsing.java
Added Paths:
-----------
trunk/src/XMPPDOMParser.java
Added: trunk/src/XMPPDOMParser.java
===================================================================
--- trunk/src/XMPPDOMParser.java (rev 0)
+++ trunk/src/XMPPDOMParser.java 2007-11-27 15:38:23 UTC (rev 57)
@@ -0,0 +1,38 @@
+import java.io.*;
+import javax.xml.parsers.*;
+import org.w3c.dom.*;
+import org.xml.sax.*;
+
+public class XMPPDOMParser {
+ public static String getRootTagName(String XMLexcerpt)
+ {
+ try
+ {
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ Document d = factory.newDocumentBuilder().parse(new InputSource(new StringReader(XMLexcerpt)));
+ Node n = d.getFirstChild();
+ return n.getNodeName();
+ }
+ catch (Exception e)
+ {
+ System.out.println(e);
+ }
+ return null;
+ }
+
+ public static String getMessageBodyFromMessageStanza(String stanza)
+ {
+ try
+ {
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ Document d = factory.newDocumentBuilder().parse(new InputSource(new StringReader(stanza)));
+ Node n = d.getElementsByTagName("body").item(0);
+ return n.getTextContent();
+ }
+ catch (Exception e)
+ {
+ System.out.println(e);
+ }
+ return null;
+ }
+}
Modified: trunk/src/XMPPLinkLocalChatSession.java
===================================================================
--- trunk/src/XMPPLinkLocalChatSession.java 2007-11-27 13:04:34 UTC (rev 56)
+++ trunk/src/XMPPLinkLocalChatSession.java 2007-11-27 15:38:23 UTC (rev 57)
@@ -7,11 +7,11 @@
public class XMPPLinkLocalChatSession {
private boolean _sentHandshake = false;
- private boolean _recievedHandshake =false;
+ private boolean _recievedHandshake = false;
+ private boolean _sentHangup = false;
private Socket _s;
private Reader _fromRemoteHost;
private PrintWriter _toRemoteHost;
- //private PrintWriter _toUser = new PrintWriter(System.out,true);
private ZeroFileChatWindow _chatWindow;
private Thread _remoteHostReadingThread = new Thread() {
@@ -22,13 +22,13 @@
while ((chars_read = _fromRemoteHost.read(buffer)) != -1)
{
RecievedStanza(String.valueOf(buffer).substring(0,chars_read));
- //_toUser.write(buffer,0,chars_read);
- //_toUser.flush();
- //_chatWindow.printText(String.valueOf(buffer));
}
}
- catch (IOException e) {System.out.println(e);}
- System.out.println("Connection closed!");
+ catch (IOException e)
+ {
+ this.interrupt();
+ }
+
}
};
@@ -66,7 +66,41 @@
private void RecievedStanza(String stanza)
{
- RecievedMessage(stanza);
+ if (stanza.contains("<?xml"))
+ // Stanza is a handshake
+ {
+ _recievedHandshake = true;
+ if (!_sentHandshake)
+ sendHandshake();
+ }
+ else if (stanza.contains("</stream:stream>"))
+ // Stanza is a hangup message
+ {
+ _chatWindow.printText("Opposite party has left the chat");
+ disconnect();
+ }
+ else
+ // Stanza is neither handshake nor hangup, rather a message with a payload of some sort
+ {
+ if (_sentHandshake && _recievedHandshake)
+ {
+ String stanzaType = null;
+ try
+ {
+ stanzaType = XMPPDOMParser.getRootTagName(stanza);
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ if (stanzaType.equals("message"))
+ {
+ RecievedMessage(XMPPDOMParser.getMessageBodyFromMessageStanza(stanza));
+ }
+ else
+ System.out.println(stanzaType + " - not yet handled stanza type.");
+ }
+ }
}
private void RecievedMessage(String mess)
@@ -84,7 +118,6 @@
_fromRemoteHost = new InputStreamReader(_s.getInputStream());
_remoteHostReadingThread.start();
_toRemoteHost = new PrintWriter(_s.getOutputStream());
- sendHandshake();
}
catch (Exception e)
{
@@ -102,9 +135,15 @@
{
try
{
- _toRemoteHost.print("</stream:stream>");
- _toRemoteHost.flush();
- _s.close();
+ if (!_sentHangup)
+ {
+ _toRemoteHost.print("</stream:stream>");
+ _toRemoteHost.flush();
+ _s.close();
+ _sentHandshake = false;
+ _recievedHandshake = false;
+ _sentHangup = true;
+ }
}
catch (Exception e)
{
Modified: trunk/src/ZeroFileChatWindow.java
===================================================================
--- trunk/src/ZeroFileChatWindow.java 2007-11-27 13:04:34 UTC (rev 56)
+++ trunk/src/ZeroFileChatWindow.java 2007-11-27 15:38:23 UTC (rev 57)
@@ -71,7 +71,7 @@
_chatWindowFrame.addWindowListener(new WindowListener(){
public void windowClosing(WindowEvent e)
{
-
+ _session.disconnect();
}
public void windowDeiconified(WindowEvent e)
{
@@ -87,7 +87,7 @@
}
public void windowClosed(WindowEvent e)
{
- _session.disconnect();
+
}
public void windowIconified(WindowEvent e)
{
Modified: trunk/src/ZeroconfBrowsing.java
===================================================================
--- trunk/src/ZeroconfBrowsing.java 2007-11-27 13:04:34 UTC (rev 56)
+++ trunk/src/ZeroconfBrowsing.java 2007-11-27 15:38:23 UTC (rev 57)
@@ -19,7 +19,6 @@
if (!ZeroFileMainWindow.containsHost(newHost) && !ZeroconfRegistration.getMyServiceName().equals(name))
{
ZeroFileMainWindow.addHost(newHost);
- System.out.println("hittat servicename: " + name);
}
}
public void serviceLost(DNSSDService browser, int flags, int ifIndex,
@@ -28,10 +27,8 @@
ZeroFileMainWindow.removeHost(new XMPPLinkLocalHost(name));
}
});
- System.out.println("Starting browsing...");
}
static void stopBrowsing(){
_s.stop();
- System.out.println("Stopped browsing");
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|