3030 * @author coezbek
3131 * @author srossbach
3232 */
33- public class BinaryChannelConnection implements IByteStreamConnection {
33+ public class BinaryChannelConnection implements IPacketConnection {
3434
3535 private static final Logger LOG = Logger .getLogger (BinaryChannelConnection .class );
3636
@@ -49,21 +49,23 @@ private static class Opcode {
4949 /** Max size of data chunks */
5050 private static final int CHUNKSIZE = 32 * 1024 - 1 ;
5151
52- private IByteStreamConnectionListener listener ;
5352 private ReceiverThread receiveThread ;
5453
5554 private final JID remoteAddress ;
5655 private final JID localAddress ;
5756
5857 private final String connectionID ;
5958
59+ private final IConnectionClosedCallback callback ;
60+
6061 private IDPool idPool = new IDPool ();
6162
6263 private boolean connected ;
6364 private boolean initialized ;
6465
6566 private Map <Integer , ByteArrayOutputStream > pendingFragmentedPackets =
6667 new HashMap <Integer , ByteArrayOutputStream >();
68+
6769 private Map <Integer , BinaryXMPPExtension > pendingXMPPExtensions =
6870 new HashMap <Integer , BinaryXMPPExtension >();
6971
@@ -111,24 +113,16 @@ public void run() {
111113
112114 private IBinaryXMPPExtensionReceiver receiver ;
113115
114- public BinaryChannelConnection (
115- JID localAddress ,
116- JID remoteAddress ,
117- String connectionID ,
118- ByteStream stream ,
119- StreamMode mode ,
120- IByteStreamConnectionListener listener )
121- throws IOException {
122- this .listener = listener ;
123- this .localAddress = localAddress ;
124- this .remoteAddress = remoteAddress ;
125- this .connectionID = connectionID ;
126- this .stream = stream ;
127- this .stream .setReadTimeout (0 ); // keep connection alive
128- this .mode = mode ;
116+ public BinaryChannelConnection (ByteStream stream , IConnectionClosedCallback callback ) {
117+ this .callback = callback ;
118+ // FIXME
119+ this .localAddress = (JID ) stream .getLocalAddress ();
120+ // FIXME
121+ this .remoteAddress = (JID ) stream .getRemoteAddress ();
129122
130- outputStream = new DataOutputStream (new BufferedOutputStream (stream .getOutputStream ()));
131- inputStream = new DataInputStream (new BufferedInputStream (stream .getInputStream ()));
123+ this .connectionID = stream .getId ();
124+ this .mode = stream .getMode ();
125+ this .stream = stream ;
132126 }
133127
134128 @ Override
@@ -138,14 +132,17 @@ public void setBinaryXMPPExtensionReceiver(IBinaryXMPPExtensionReceiver receiver
138132 this .receiver = receiver ;
139133 }
140134
141- @ Override
142- public synchronized void initialize () {
135+ public synchronized void initialize () throws IOException {
143136 if (initialized ) return ;
144137
145138 /*
146139 * it is ok to start the receiver a bit later because the data will be
147140 * already buffered by SMACK or the OS
148141 */
142+ stream .setReadTimeout (0 ); // keep connection alive
143+ outputStream = new DataOutputStream (new BufferedOutputStream (stream .getOutputStream ()));
144+ inputStream = new DataInputStream (new BufferedInputStream (stream .getInputStream ()));
145+
149146 receiveThread = new ReceiverThread ();
150147 receiveThread .setName ("BinaryChannel-" + remoteAddress .getName ());
151148 receiveThread .start ();
@@ -154,13 +151,13 @@ public synchronized void initialize() {
154151 }
155152
156153 @ Override
157- public String getConnectionID () {
158- return connectionID ;
154+ public Object getLocalAddress () {
155+ return stream . getRemoteAddress () ;
159156 }
160157
161158 @ Override
162- public synchronized boolean isConnected () {
163- return connected ;
159+ public String getId () {
160+ return connectionID ;
164161 }
165162
166163 @ Override
@@ -192,10 +189,9 @@ public void close() {
192189 }
193190 }
194191
195- listener .connectionClosed (connectionID , this );
192+ if ( callback != null ) callback .connectionClosed (this );
196193 }
197194
198- @ Override
199195 public StreamMode getMode () {
200196 return mode ;
201197 }
@@ -436,6 +432,10 @@ private BinaryXMPPExtension readNextXMPPExtension() throws IOException {
436432 throw new InterruptedIOException ("interrupted while reading stream data" );
437433 }
438434
435+ private synchronized boolean isConnected () {
436+ return connected ;
437+ }
438+
439439 private synchronized void sendData (int fragmentId , byte [] data , int offset , int length )
440440 throws IOException {
441441
0 commit comments