1 20 21 package org.jivesoftware.whack; 22 23 import org.dom4j.Element; 24 import org.dom4j.io.XPPPacketReader; 25 import org.xmlpull.v1.XmlPullParserException; 26 import org.xmpp.packet.*; 27 28 import java.io.EOFException ; 29 import java.net.SocketException ; 30 31 36 class SocketReadThread extends Thread { 37 38 private ExternalComponent component; 39 private boolean shutdown = false; 40 41 XPPPacketReader reader = null; 42 43 49 public SocketReadThread(ExternalComponent component, XPPPacketReader reader) { 50 super("Component socket reader"); 51 this.component = component; 52 this.reader = reader; 53 } 54 55 59 public void run() { 60 try { 61 readStream(); 62 } 63 catch (EOFException eof) { 64 } 66 catch (SocketException se) { 67 if (!shutdown) { 70 component.getManager().getLog().error(se); 71 component.connectionLost(); 72 } 73 } 74 catch (XmlPullParserException ie) { 75 component.getManager().getLog().error(ie); 76 } 77 catch (Exception e) { 78 component.getManager().getLog().warn(e); 79 } 80 } 81 82 85 private void readStream() throws Exception { 86 while (!shutdown) { 87 Element doc = reader.parseDocument().getRootElement(); 88 89 if (doc == null) { 90 return; 93 } 94 95 Packet packet; 96 String tag = doc.getName(); 97 if ("message".equals(tag)) { 98 packet = new Message(doc); 99 } 100 else if ("presence".equals(tag)) { 101 packet = new Presence(doc); 102 } 103 else if ("iq".equals(tag)) { 104 packet = getIQ(doc); 105 } 106 else { 107 throw new XmlPullParserException("Unknown packet type was read: " + tag); 108 } 109 component.processPacket(packet); 111 } 112 } 113 114 private IQ getIQ(Element doc) { 115 Element query = doc.element("query"); 116 if (query != null && "jabber:iq:roster".equals(query.getNamespaceURI())) { 117 return new Roster(doc); 118 } 119 else { 120 return new IQ(doc); 121 } 122 } 123 124 132 public void shutdown() { 133 shutdown = true; 134 } 135 } 136 | Popular Tags |