@@ -9,19 +9,20 @@ import {GroupConfig, ClientNetworkConfig} from '../Config';
99
1010import ConnectionAuthenticator = require( './ConnectionAuthenticator' ) ;
1111import HazelcastClient = require( '../HazelcastClient' ) ;
12+ import { ConnectionListener } from '../ConnectionListener' ;
1213
1314class ClientConnectionManager {
1415
1516 private client : HazelcastClient ;
16-
17+ private listeners : ConnectionListener [ ] = [ ] ;
1718 private pendingConnections : { [ address : string ] : Q . Deferred < ClientConnection > } = { } ;
18- private establishedConnections : { [ address : string ] : ClientConnection } = { } ;
19+ establishedConnections : { [ address : string ] : ClientConnection } = { } ;
1920
2021 constructor ( client : HazelcastClient ) {
2122 this . client = client ;
2223 }
2324
24- public getOrConnect ( address : Address ) : Q . Promise < ClientConnection > {
25+ getOrConnect ( address : Address ) : Q . Promise < ClientConnection > {
2526 var addressIndex = address . toString ( ) ;
2627 var result : Q . Deferred < ClientConnection > = Q . defer < ClientConnection > ( ) ;
2728
@@ -43,21 +44,23 @@ class ClientConnectionManager {
4344 var clientConnection = new ClientConnection ( address ) ;
4445
4546 clientConnection . connect ( ) . then ( ( connection : ClientConnection ) => {
46-
4747 connection . registerResponseCallback ( ( data : Buffer ) => {
4848 this . client . getInvocationService ( ) . processResponse ( data ) ;
4949 } ) ;
5050
5151 var callback = ( authenticated : boolean ) => {
5252 if ( authenticated ) {
5353 result . resolve ( connection ) ;
54- this . establishedConnections [ addressIndex ] = connection ;
54+ this . establishedConnections [ connection . address . toString ( ) ] = connection ;
5555 } else {
5656 result . reject ( new Error ( 'Authentication failed' ) ) ;
5757 }
5858 } ;
59-
60- this . authenticate ( connection ) . then ( callback ) . finally ( ( ) => {
59+ this . authenticate ( connection ) . then ( callback ) . then ( ( ) => {
60+ this . onConnectionOpened ( connection ) ;
61+ } ) . catch ( ( e : any ) => {
62+ result . reject ( e ) ;
63+ } ) . finally ( ( ) => {
6164 delete this . pendingConnections [ addressIndex ] ;
6265 } ) ;
6366 } ) . catch ( ( e : any ) => {
@@ -67,6 +70,40 @@ class ClientConnectionManager {
6770 return result . promise ;
6871 }
6972
73+ destroyConnection ( address : Address ) : void {
74+ var addressStr = address . toString ( ) ;
75+ if ( this . pendingConnections . hasOwnProperty ( addressStr ) ) {
76+ this . pendingConnections [ addressStr ] . reject ( null ) ;
77+ }
78+ if ( this . establishedConnections . hasOwnProperty ( addressStr ) ) {
79+ var conn = this . establishedConnections [ addressStr ] ;
80+ conn . close ( ) ;
81+ this . onConnectionClosed ( conn ) ;
82+ delete this . establishedConnections [ addressStr ] ;
83+ }
84+ }
85+
86+ addListener ( listener : ConnectionListener ) {
87+ this . listeners . push ( listener ) ;
88+ }
89+
90+ private onConnectionClosed ( connection : ClientConnection ) {
91+ this . listeners . forEach ( ( listener ) => {
92+ if ( listener . hasOwnProperty ( 'onConnectionClosed' ) ) {
93+ setImmediate ( listener . onConnectionClosed . bind ( this ) , connection ) ;
94+ }
95+ } ) ;
96+ }
97+
98+ private onConnectionOpened ( connection : ClientConnection ) {
99+ console . log ( 'Authenticated to ' + connection . address ) ;
100+ this . listeners . forEach ( ( listener ) => {
101+ if ( listener . hasOwnProperty ( 'onConnectionOpened' ) ) {
102+ setImmediate ( listener . onConnectionOpened . bind ( this ) , connection ) ;
103+ }
104+ } ) ;
105+ }
106+
70107 private authenticate ( connection : ClientConnection ) : Q . Promise < boolean > {
71108 var name = this . client . getConfig ( ) . groupConfig . name ;
72109 var password = this . client . getConfig ( ) . groupConfig . password ;
0 commit comments