1 package org.jivesoftware.smack.debugger; 2 3 import org.jivesoftware.smack.ConnectionListener; 4 import org.jivesoftware.smack.PacketListener; 5 import org.jivesoftware.smack.XMPPConnection; 6 import org.jivesoftware.smack.packet.Packet; 7 import org.jivesoftware.smack.util.*; 8 9 import java.io.Reader ; 10 import java.io.Writer ; 11 import java.text.SimpleDateFormat ; 12 import java.util.Date ; 13 14 25 public class ConsoleDebugger implements SmackDebugger { 26 27 public static boolean printInterpreted = false; 28 private SimpleDateFormat dateFormatter = new SimpleDateFormat ("hh:mm:ss aaa"); 29 30 private XMPPConnection connection = null; 31 32 private PacketListener listener = null; 33 private ConnectionListener connListener = null; 34 35 private Writer writer; 36 private Reader reader; 37 private ReaderListener readerListener; 38 private WriterListener writerListener; 39 40 public ConsoleDebugger(XMPPConnection connection, Writer writer, Reader reader) { 41 this.connection = connection; 42 this.writer = writer; 43 this.reader = reader; 44 createDebug(); 45 } 46 47 50 private void createDebug() { 51 ObservableReader debugReader = new ObservableReader(reader); 53 readerListener = new ReaderListener() { 54 public void read(String str) { 55 System.out.println( 56 dateFormatter.format(new Date ()) + " RCV (" + connection.hashCode() + 57 "): " + 58 str); 59 } 60 }; 61 debugReader.addReaderListener(readerListener); 62 63 ObservableWriter debugWriter = new ObservableWriter(writer); 65 writerListener = new WriterListener() { 66 public void write(String str) { 67 System.out.println( 68 dateFormatter.format(new Date ()) + " SENT (" + connection.hashCode() + 69 "): " + 70 str); 71 } 72 }; 73 debugWriter.addWriterListener(writerListener); 74 75 reader = debugReader; 78 writer = debugWriter; 79 80 listener = new PacketListener() { 84 public void processPacket(Packet packet) { 85 if (printInterpreted) { 86 System.out.println( 87 dateFormatter.format(new Date ()) + " RCV PKT (" + 88 connection.hashCode() + 89 "): " + 90 packet.toXML()); 91 } 92 } 93 }; 94 95 connListener = new ConnectionListener() { 96 public void connectionClosed() { 97 System.out.println( 98 dateFormatter.format(new Date ()) + " Connection closed (" + 99 connection.hashCode() + 100 ")"); 101 } 102 103 public void connectionClosedOnError(Exception e) { 104 System.out.println( 105 dateFormatter.format(new Date ()) + 106 " Connection closed due to an exception (" + 107 connection.hashCode() + 108 ")"); 109 e.printStackTrace(); 110 } 111 }; 112 } 113 114 public Reader newConnectionReader(Reader newReader) { 115 ((ObservableReader)reader).removeReaderListener(readerListener); 116 ObservableReader debugReader = new ObservableReader(newReader); 117 debugReader.addReaderListener(readerListener); 118 reader = debugReader; 119 return reader; 120 } 121 122 public Writer newConnectionWriter(Writer newWriter) { 123 ((ObservableWriter)writer).removeWriterListener(writerListener); 124 ObservableWriter debugWriter = new ObservableWriter(newWriter); 125 debugWriter.addWriterListener(writerListener); 126 writer = debugWriter; 127 return writer; 128 } 129 130 public void userHasLogged(String user) { 131 boolean isAnonymous = "".equals(StringUtils.parseName(user)); 132 String title = 133 "User logged (" + connection.hashCode() + "): " 134 + (isAnonymous ? "" : StringUtils.parseBareAddress(user)) 135 + "@" 136 + connection.getServiceName() 137 + ":" 138 + connection.getPort(); 139 title += "/" + StringUtils.parseResource(user); 140 System.out.println(title); 141 connection.addConnectionListener(connListener); 144 } 145 146 public Reader getReader() { 147 return reader; 148 } 149 150 public Writer getWriter() { 151 return writer; 152 } 153 154 public PacketListener getReaderListener() { 155 return listener; 156 } 157 158 public PacketListener getWriterListener() { 159 return null; 160 } 161 } 162 | Popular Tags |