@@ -42,6 +42,8 @@ public abstract class WebSocketNode {
@Resource ( name = Application . RESNAME_SNCP_ADDR )
protected InetSocketAddress localSncpAddress ; //为SncpServer的服务address
protected WebSocketAddress wsaddress ;
protected String name ;
//如果不是分布式(没有SNCP) 值为null
@@ -51,11 +53,11 @@ public abstract class WebSocketNode {
@Resource ( name = " $_sendconvert " )
protected Convert sendConvert ;
//存放所有用户分布在节点上的队列信息,Set<Inet SocketAddress> 为 sncpnode 的集合, key: groupid
//存放所有用户分布在节点上的队列信息,Set<Web SocketAddress> 为 sncpnode 的集合, key: groupid
//集合包含 localSncpAddress
//如果不是分布式(没有SNCP), sncpNodeAddresses 将不会被用到
//如果不是分布式(没有SNCP), source 将不会被用到
@Resource ( name = " $ " )
protected CacheSource < Inet SocketAddress> sncpNodeAddresses ;
protected CacheSource < Web SocketAddress> source ;
//当前节点的本地WebSocketEngine
protected WebSocketEngine localEngine ;
@@ -69,14 +71,18 @@ public abstract class WebSocketNode {
public void init ( AnyValue conf ) {
this . tryAcquireSeconds = Integer . getInteger ( " WebSocketNode.tryAcquireSeconds " , 12 ) ;
if ( sncpNodeAddresses ! = null & & " memory " . equals ( sncpNodeAddresses . getType ( ) ) ) {
sncpNodeAddresses . initValueType ( Inet SocketAddress. class ) ;
if ( source ! = null & & " memory " . equals ( source . getType ( ) ) ) {
source . initValueType ( Web SocketAddress. class ) ;
}
if ( localEngine ! = null ) {
int wsthreads = localEngine . wsthreads ;
if ( wsthreads = = 0 ) wsthreads = Runtime . getRuntime ( ) . availableProcessors ( ) * 8 ;
if ( wsthreads > 0 ) this . semaphore = new Semaphore ( wsthreads ) ;
}
String mqtopic = this . messageAgent = = null ? null : this . messageAgent . generateWebSocketRespTopic ( this ) ;
if ( mqtopic ! = null | | this . localSncpAddress ! = null ) {
this . wsaddress = new WebSocketAddress ( mqtopic , localSncpAddress ) ;
}
}
public void destroy ( AnyValue conf ) {
@@ -97,45 +103,45 @@ public abstract class WebSocketNode {
if ( this . localEngine = = null ) return ;
//关掉所有本地本地WebSocket
this . localEngine . getLocalWebSockets ( ) . forEach ( g - > g . close ( ) ) ;
if ( sncpNodeAddresses ! = null & & localSncpA ddress ! = null ) {
sncpNodeAddresses . removeSetItem ( SOURCE_SNCP_ADDRS_KEY , Inet SocketAddress. class , localSncpA ddress) ;
if ( source ! = null & & wsa ddress ! = null ) {
source . removeSetItem ( SOURCE_SNCP_ADDRS_KEY , Web SocketAddress. class , this . wsa ddress) ;
}
}
protected abstract CompletableFuture < List < String > > getWebSocketAddresses ( @RpcTargetAddress InetSocketAddress targetAddress , Serializable userid ) ;
protected abstract CompletableFuture < List < String > > getWebSocketAddresses ( @RpcTargetTopic String topic , @ RpcTargetAddress InetSocketAddress targetAddress , Serializable userid ) ;
protected abstract CompletableFuture < Integer > sendMessage ( @RpcTargetAddress InetSocketAddress targetAddress , Object message , boolean last , Serializable . . . userids ) ;
protected abstract CompletableFuture < Integer > sendMessage ( @RpcTargetTopic String topic , @ RpcTargetAddress InetSocketAddress targetAddress , Object message , boolean last , Serializable . . . userids ) ;
protected abstract CompletableFuture < Integer > broadcastMessage ( @RpcTargetAddress InetSocketAddress targetAddress , WebSocketRange wsrange , Object message , boolean last ) ;
protected abstract CompletableFuture < Integer > broadcastMessage ( @RpcTargetTopic String topic , @ RpcTargetAddress InetSocketAddress targetAddress , WebSocketRange wsrange , Object message , boolean last ) ;
protected abstract CompletableFuture < Integer > sendAction ( @RpcTargetAddress InetSocketAddress targetAddress , WebSocketAction action , Serializable . . . userids ) ;
protected abstract CompletableFuture < Integer > sendAction ( @RpcTargetTopic String topic , @ RpcTargetAddress InetSocketAddress targetAddress , WebSocketAction action , Serializable . . . userids ) ;
protected abstract CompletableFuture < Integer > broadcastAction ( @RpcTargetAddress InetSocketAddress targetAddress , WebSocketAction action ) ;
protected abstract CompletableFuture < Integer > broadcastAction ( @RpcTargetTopic String topic , @ RpcTargetAddress InetSocketAddress targetAddress , WebSocketAction action ) ;
protected abstract CompletableFuture < Void > connect ( Serializable userid , Inet SocketAddress sncpA ddr) ;
protected abstract CompletableFuture < Void > connect ( Serializable userid , Web SocketAddress wsa ddr) ;
protected abstract CompletableFuture < Void > disconnect ( Serializable userid , Inet SocketAddress sncpA ddr) ;
protected abstract CompletableFuture < Void > disconnect ( Serializable userid , Web SocketAddress wsa ddr) ;
protected abstract CompletableFuture < Void > changeUserid ( Serializable fromuserid , Serializable touserid , Inet SocketAddress sncpA ddr) ;
protected abstract CompletableFuture < Void > changeUserid ( Serializable fromuserid , Serializable touserid , Web SocketAddress wsa ddr) ;
protected abstract CompletableFuture < Boolean > existsWebSocket ( Serializable userid , @RpcTargetAddress InetSocketAddress targetAddress ) ;
protected abstract CompletableFuture < Boolean > existsWebSocket ( Serializable userid , @RpcTargetTopic String topic , @RpcTargetAddress InetSocketAddress targetAddress ) ;
protected abstract CompletableFuture < Integer > forceCloseWebSocket ( Serializable userid , @RpcTargetAddress InetSocketAddress targetAddress ) ;
protected abstract CompletableFuture < Integer > forceCloseWebSocket ( Serializable userid , @RpcTargetTopic String topic , @RpcTargetAddress InetSocketAddress targetAddress ) ;
//--------------------------------------------------------------------------------
final CompletableFuture < Void > connect ( final Serializable userid ) {
if ( logger . isLoggable ( Level . FINEST ) ) logger . finest ( localSncpA ddress + " receive websocket connect event ( " + userid + " on " + ( this . localEngine = = null ? null : this . localEngine . getEngineid ( ) ) + " ). " ) ;
return connect ( userid , localSncpA ddress) ;
if ( logger . isLoggable ( Level . FINEST ) ) logger . finest ( this . wsa ddress + " receive websocket connect event ( " + userid + " on " + ( this . localEngine = = null ? null : this . localEngine . getEngineid ( ) ) + " ). " ) ;
return connect ( userid , this . wsa ddress) ;
}
final CompletableFuture < Void > disconnect ( final Serializable userid ) {
if ( logger . isLoggable ( Level . FINEST ) ) logger . finest ( localSncpA ddress + " receive websocket disconnect event ( " + userid + " on " + ( this . localEngine = = null ? null : this . localEngine . getEngineid ( ) ) + " ). " ) ;
return disconnect ( userid , localSncpA ddress) ;
if ( logger . isLoggable ( Level . FINEST ) ) logger . finest ( this . wsa ddress + " receive websocket disconnect event ( " + userid + " on " + ( this . localEngine = = null ? null : this . localEngine . getEngineid ( ) ) + " ). " ) ;
return disconnect ( userid , this . wsa ddress) ;
}
final CompletableFuture < Void > changeUserid ( Serializable olduserid , final Serializable newuserid ) {
if ( logger . isLoggable ( Level . FINEST ) ) logger . finest ( localSncpA ddress + " receive websocket changeUserid event (from " + olduserid + " to " + newuserid + " on " + ( this . localEngine = = null ? null : this . localEngine . getEngineid ( ) ) + " ). " ) ;
return changeUserid ( olduserid , newuserid , localSncpA ddress) ;
if ( logger . isLoggable ( Level . FINEST ) ) logger . finest ( this . wsa ddress + " receive websocket changeUserid event (from " + olduserid + " to " + newuserid + " on " + ( this . localEngine = = null ? null : this . localEngine . getEngineid ( ) ) + " ). " ) ;
return changeUserid ( olduserid , newuserid , this . wsa ddress) ;
}
public final String getName ( ) {
@@ -147,15 +153,16 @@ public abstract class WebSocketNode {
* 获取目标地址 <br>
* 该方法仅供内部调用
*
* @param topic RpcTargetTopic
* @param targetAddress InetSocketAddress
* @param userid Serializable
*
* @return 客户端地址列表
*/
protected CompletableFuture < List < String > > remoteWebSocketAddresses ( @RpcTargetAddress InetSocketAddress targetAddress , Serializable userid ) {
protected CompletableFuture < List < String > > remoteWebSocketAddresses ( @RpcTargetTopic String topic , @ RpcTargetAddress InetSocketAddress targetAddress , Serializable userid ) {
if ( remoteNode = = null ) return CompletableFuture . completedFuture ( null ) ;
try {
return remoteNode . getWebSocketAddresses ( targetAddress , userid ) ;
return remoteNode . getWebSocketAddresses ( topic , targetAddress, userid ) ;
} catch ( Exception e ) {
logger . log ( Level . WARNING , " remote " + targetAddress + " websocket getOnlineRemoteAddresses error " , e ) ;
return CompletableFuture . completedFuture ( null ) ;
@@ -164,21 +171,21 @@ public abstract class WebSocketNode {
/**
* 获取用户在线的SNCP节点地址列表, 不是分布式则返回元素数量为1, 且元素值为null的列表<br>
* Inet SocketAddress 为 SNCP节点地址
* Web SocketAddress 为 SNCP节点地址
*
* @param userid Serializable
*
* @return 地址列表
*/
public CompletableFuture < Collection < Inet SocketAddress> > getRpcNodeAddresses ( final Serializable userid ) {
if ( this . sncpNodeAddresses ! = null ) {
public CompletableFuture < Collection < Web SocketAddress> > getRpcNodeAddresses ( final Serializable userid ) {
if ( this . source ! = null ) {
tryAcquireSemaphore ( ) ;
CompletableFuture < Collection < Inet SocketAddress> > result = this . sncpNodeAddresses . getCollectionAsync ( SOURCE_SNCP_USERID_PREFIX + userid , Inet SocketAddress. class ) ;
CompletableFuture < Collection < Web SocketAddress> > result = this . source . getCollectionAsync ( SOURCE_SNCP_USERID_PREFIX + userid , Web SocketAddress. class ) ;
if ( semaphore ! = null ) result . whenComplete ( ( r , e ) - > releaseSemaphore ( ) ) ;
return result ;
}
List < Inet SocketAddress> rs = new ArrayList < > ( ) ;
rs . add ( this . localSncpA ddress) ;
List < Web SocketAddress> rs = new ArrayList < > ( ) ;
rs . add ( this . wsa ddress) ;
return CompletableFuture . completedFuture ( rs ) ;
}
@@ -191,14 +198,14 @@ public abstract class WebSocketNode {
*
* @return 地址集合
*/
public CompletableFuture < Map < Inet SocketAddress, List < String > > > getRpcNodeWebSocketAddresses ( final Serializable userid ) {
CompletableFuture < Collection < Inet SocketAddress> > sncpFuture = getRpcNodeAddresses ( userid ) ;
return sncpFuture . thenCompose ( ( Collection < Inet SocketAddress> addrs ) - > {
public CompletableFuture < Map < Web SocketAddress, List < String > > > getRpcNodeWebSocketAddresses ( final Serializable userid ) {
CompletableFuture < Collection < Web SocketAddress> > sncpFuture = getRpcNodeAddresses ( userid ) ;
return sncpFuture . thenCompose ( ( Collection < Web SocketAddress> addrs ) - > {
if ( logger . isLoggable ( Level . FINEST ) ) logger . finest ( " websocket found userid: " + userid + " on " + addrs ) ;
if ( addrs = = null | | addrs . isEmpty ( ) ) return CompletableFuture . completedFuture ( new HashMap < > ( ) ) ;
CompletableFuture < Map < Inet SocketAddress, List < String > > > future = null ;
for ( final Inet SocketAddress nodeAddress : addrs ) {
CompletableFuture < Map < Inet SocketAddress, List < String > > > mapFuture = getWebSocketAddresses ( nodeAddress , userid )
CompletableFuture < Map < Web SocketAddress, List < String > > > future = null ;
for ( final Web SocketAddress nodeAddress : addrs ) {
CompletableFuture < Map < Web SocketAddress, List < String > > > mapFuture = getWebSocketAddresses ( nodeAddress . getTopic ( ) , nodeAddress . getAddr ( ) , userid )
. thenCompose ( ( List < String > list ) - > CompletableFuture . completedFuture ( Utility . ofMap ( nodeAddress , list ) ) ) ;
future = future = = null ? mapFuture : future . thenCombine ( mapFuture , ( a , b ) - > Utility . merge ( a , b ) ) ;
}
@@ -213,11 +220,11 @@ public abstract class WebSocketNode {
* @return boolean
*/
public CompletableFuture < Integer > getUserSize ( ) {
if ( this . localEngine ! = null & & this . sncpNodeAddresses = = null ) {
if ( this . localEngine ! = null & & this . source = = null ) {
return CompletableFuture . completedFuture ( this . localEngine . getLocalUserSize ( ) ) ;
}
tryAcquireSemaphore ( ) ;
CompletableFuture < Integer > rs = this . sncpNodeAddresses . queryKeysStartsWithAsync ( SOURCE_SNCP_USERID_PREFIX ) . thenApply ( v - > v . size ( ) ) ;
CompletableFuture < Integer > rs = this . source . queryKeysStartsWithAsync ( SOURCE_SNCP_USERID_PREFIX ) . thenApply ( v - > v . size ( ) ) ;
if ( semaphore ! = null ) rs . whenComplete ( ( r , e ) - > releaseSemaphore ( ) ) ;
return rs ;
}
@@ -229,11 +236,11 @@ public abstract class WebSocketNode {
* @return boolean
*/
public CompletableFuture < Set < String > > getUserSet ( ) {
if ( this . localEngine ! = null & & this . sncpNodeAddresses = = null ) {
if ( this . localEngine ! = null & & this . source = = null ) {
return CompletableFuture . completedFuture ( new LinkedHashSet < > ( this . localEngine . getLocalUserSet ( ) . stream ( ) . map ( x - > String . valueOf ( x ) ) . collect ( Collectors . toList ( ) ) ) ) ;
}
tryAcquireSemaphore ( ) ;
CompletableFuture < Set < String > > rs = this . sncpNodeAddresses . queryKeysStartsWithAsync ( SOURCE_SNCP_USERID_PREFIX ) . thenApply ( v - > new LinkedHashSet < > ( v . stream ( ) . map ( x - > x . substring ( SOURCE_SNCP_USERID_PREFIX . length ( ) ) ) . collect ( Collectors . toList ( ) ) ) ) ;
CompletableFuture < Set < String > > rs = this . source . queryKeysStartsWithAsync ( SOURCE_SNCP_USERID_PREFIX ) . thenApply ( v - > new LinkedHashSet < > ( v . stream ( ) . map ( x - > x . substring ( SOURCE_SNCP_USERID_PREFIX . length ( ) ) ) . collect ( Collectors . toList ( ) ) ) ) ;
if ( semaphore ! = null ) rs . whenComplete ( ( r , e ) - > releaseSemaphore ( ) ) ;
return rs ;
}
@@ -250,23 +257,23 @@ public abstract class WebSocketNode {
if ( userid instanceof WebSocketUserAddress ) return existsWebSocket ( ( WebSocketUserAddress ) userid ) ;
CompletableFuture < Boolean > localFuture = null ;
if ( this . localEngine ! = null ) localFuture = CompletableFuture . completedFuture ( localEngine . existsLocalWebSocket ( userid ) ) ;
if ( this . sncpNodeAddresses = = null | | this . remoteNode = = null ) {
if ( this . source = = null | | this . remoteNode = = null ) {
if ( logger . isLoggable ( Level . FINEST ) ) logger . finest ( " websocket remote node is null " ) ;
//没有CacheSource就不会有分布式节点
return localFuture ;
}
//远程节点关闭
tryAcquireSemaphore ( ) ;
CompletableFuture < Collection < Inet SocketAddress> > addrsFuture = sncpNodeAddresses . getCollectionAsync ( SOURCE_SNCP_USERID_PREFIX + userid , Inet SocketAddress. class ) ;
CompletableFuture < Collection < Web SocketAddress> > addrsFuture = source . getCollectionAsync ( SOURCE_SNCP_USERID_PREFIX + userid , Web SocketAddress. class ) ;
if ( semaphore ! = null ) addrsFuture . whenComplete ( ( r , e ) - > releaseSemaphore ( ) ) ;
CompletableFuture < Boolean > remoteFuture = addrsFuture . thenCompose ( ( Collection < Inet SocketAddress> addrs ) - > {
CompletableFuture < Boolean > remoteFuture = addrsFuture . thenCompose ( ( Collection < Web SocketAddress> addrs ) - > {
//if (logger.isLoggable(Level.FINEST)) logger.finest("websocket found userid:" + userid + " on " + addrs);
if ( addrs = = null | | addrs . isEmpty ( ) ) return CompletableFuture . completedFuture ( false ) ;
CompletableFuture < Boolean > future = null ;
for ( Inet SocketAddress addr : addrs ) {
if ( addr = = null | | addr . equals ( localSncpA ddress) ) continue ;
future = future = = null ? remoteNode . existsWebSocket ( userid , addr )
: future . thenCombine ( remoteNode . existsWebSocket ( userid , addr ) , ( a , b ) - > a | b ) ;
for ( Web SocketAddress addr : addrs ) {
if ( addr = = null | | addr . equals ( wsa ddress) ) continue ;
future = future = = null ? remoteNode . existsWebSocket ( userid , addr . getTopic ( ) , addr . getAddr ( ) )
: future . thenCombine ( remoteNode . existsWebSocket ( userid , addr . getTopic ( ) , addr . getAddr ( ) ), ( a , b ) - > a | b ) ;
}
return future = = null ? CompletableFuture . completedFuture ( false ) : future ;
} ) ;
@@ -284,23 +291,23 @@ public abstract class WebSocketNode {
public CompletableFuture < Boolean > existsWebSocket ( final WebSocketUserAddress userAddress ) {
CompletableFuture < Boolean > localFuture = null ;
if ( this . localEngine ! = null ) localFuture = CompletableFuture . completedFuture ( localEngine . existsLocalWebSocket ( userAddress . userid ( ) ) ) ;
if ( this . sncpNodeAddresses = = null | | this . remoteNode = = null ) {
if ( this . source = = null | | this . remoteNode = = null ) {
if ( logger . isLoggable ( Level . FINEST ) ) logger . finest ( " websocket remote node is null " ) ;
//没有CacheSource就不会有分布式节点
return localFuture ;
}
Collection < Inet SocketAddress> addrs = userAddress . sncpA ddresses( ) ;
Collection < Web SocketAddress> addrs = userAddress . a ddresses( ) ;
if ( addrs ! = null ) addrs = new ArrayList < > ( addrs ) ; //不能修改参数内部值
if ( userAddress . sncpA ddress( ) ! = null ) {
if ( userAddress . a ddress( ) ! = null ) {
if ( addrs = = null ) addrs = new ArrayList < > ( ) ;
addrs . add ( userAddress . sncpA ddress( ) ) ;
addrs . add ( userAddress . a ddress( ) ) ;
}
if ( addrs = = null | | addrs . isEmpty ( ) ) return CompletableFuture . completedFuture ( false ) ;
CompletableFuture < Boolean > future = null ;
for ( Inet SocketAddress addr : addrs ) {
if ( addr = = null | | addr . equals ( localSncpA ddress) ) continue ;
future = future = = null ? remoteNode . existsWebSocket ( userAddress . userid ( ) , addr )
: future . thenCombine ( remoteNode . existsWebSocket ( userAddress . userid ( ) , addr ) , ( a , b ) - > a | b ) ;
for ( Web SocketAddress addr : addrs ) {
if ( addr = = null | | addr . equals ( wsa ddress) ) continue ;
future = future = = null ? remoteNode . existsWebSocket ( userAddress . userid ( ) , addr . getTopic ( ) , addr . getAddr ( ) )
: future . thenCombine ( remoteNode . existsWebSocket ( userAddress . userid ( ) , addr . getTopic ( ) , addr . getAddr ( ) ), ( a , b ) - > a | b ) ;
}
return future = = null ? CompletableFuture . completedFuture ( false ) : future ;
}
@@ -332,35 +339,35 @@ public abstract class WebSocketNode {
private CompletableFuture < Integer > forceCloseWebSocket ( final Serializable userid , final WebSocketUserAddress userAddress ) {
CompletableFuture < Integer > localFuture = null ;
if ( this . localEngine ! = null ) localFuture = CompletableFuture . completedFuture ( localEngine . forceCloseLocalWebSocket ( userAddress = = null ? userid : userAddress . userid ( ) ) ) ;
if ( this . sncpNodeAddresses = = null | | this . remoteNode = = null ) {
if ( this . source = = null | | this . remoteNode = = null ) {
if ( logger . isLoggable ( Level . FINEST ) ) logger . finest ( " websocket remote node is null " ) ;
//没有CacheSource就不会有分布式节点
return localFuture ;
}
//远程节点关闭
CompletableFuture < Collection < Inet SocketAddress> > addrsFuture ;
CompletableFuture < Collection < Web SocketAddress> > addrsFuture ;
if ( userAddress = = null ) {
tryAcquireSemaphore ( ) ;
addrsFuture = sncpNodeAddresses . getCollectionAsync ( SOURCE_SNCP_USERID_PREFIX + userid , Inet SocketAddress. class ) ;
addrsFuture = source . getCollectionAsync ( SOURCE_SNCP_USERID_PREFIX + userid , Web SocketAddress. class ) ;
if ( semaphore ! = null ) addrsFuture . whenComplete ( ( r , e ) - > releaseSemaphore ( ) ) ;
} else {
Collection < Inet SocketAddress> addrs = userAddress . sncpA ddresses( ) ;
Collection < Web SocketAddress> addrs = userAddress . a ddresses( ) ;
if ( addrs ! = null ) addrs = new ArrayList < > ( addrs ) ; //不能修改参数内部值
if ( userAddress . sncpA ddress( ) ! = null ) {
if ( userAddress . a ddress( ) ! = null ) {
if ( addrs = = null ) addrs = new ArrayList < > ( ) ;
addrs . add ( userAddress . sncpA ddress( ) ) ;
addrs . add ( userAddress . a ddress( ) ) ;
}
if ( addrs = = null | | addrs . isEmpty ( ) ) return CompletableFuture . completedFuture ( 0 ) ;
addrsFuture = CompletableFuture . completedFuture ( addrs ) ;
}
CompletableFuture < Integer > remoteFuture = addrsFuture . thenCompose ( ( Collection < Inet SocketAddress> addrs ) - > {
CompletableFuture < Integer > remoteFuture = addrsFuture . thenCompose ( ( Collection < Web SocketAddress> addrs ) - > {
if ( logger . isLoggable ( Level . FINEST ) ) logger . finest ( " websocket found userid: " + userid + " on " + addrs ) ;
if ( addrs = = null | | addrs . isEmpty ( ) ) return CompletableFuture . completedFuture ( 0 ) ;
CompletableFuture < Integer > future = null ;
for ( Inet SocketAddress addr : addrs ) {
if ( addr = = null | | addr . equals ( localSncpA ddress) ) continue ;
future = future = = null ? remoteNode . forceCloseWebSocket ( userid , addr )
: future . thenCombine ( remoteNode . forceCloseWebSocket ( userid , addr ) , ( a , b ) - > a + b ) ;
for ( Web SocketAddress addr : addrs ) {
if ( addr = = null | | addr . equals ( wsa ddress) ) continue ;
future = future = = null ? remoteNode . forceCloseWebSocket ( userid , addr . getTopic ( ) , addr . getAddr ( ) )
: future . thenCombine ( remoteNode . forceCloseWebSocket ( userid , addr . getTopic ( ) , addr . getAddr ( ) ), ( a , b ) - > a + b ) ;
}
return future = = null ? CompletableFuture . completedFuture ( 0 ) : future ;
} ) ;
@@ -511,7 +518,7 @@ public abstract class WebSocketNode {
}
if ( message0 instanceof CompletableFuture ) return ( ( CompletableFuture ) message0 ) . thenApply ( msg - > sendMessage ( convert , msg , last , userids ) ) ;
final Object message = ( convert = = null | | message0 instanceof WebSocketPacket ) ? message0 : ( ( convert instanceof TextConvert ) ? new WebSocketPacket ( ( ( TextConvert ) convert ) . convertTo ( message0 ) , last ) : new WebSocketPacket ( ( ( BinaryConvert ) convert ) . convertTo ( message0 ) , last ) ) ;
if ( this . localEngine ! = null & & this . sncpNodeAddresses = = null ) { //本地模式且没有分布式
if ( this . localEngine ! = null & & this . source = = null ) { //本地模式且没有分布式
return this . localEngine . sendLocalMessage ( message , last , userids ) ;
}
final Object remoteMessage = formatRemoteMessage ( message ) ;
@@ -526,16 +533,16 @@ public abstract class WebSocketNode {
keyuser . put ( keys [ i ] , userids [ i ] ) ;
}
tryAcquireSemaphore ( ) ;
CompletableFuture < Map < String , Collection < Inet SocketAddress> > > addrsFuture = sncpNodeAddresses . getCollectionMapAsync ( true , Inet SocketAddress. class , keys ) ;
CompletableFuture < Map < String , Collection < Web SocketAddress> > > addrsFuture = source . getCollectionMapAsync ( true , Web SocketAddress. class , keys ) ;
if ( semaphore ! = null ) addrsFuture . whenComplete ( ( r , e ) - > releaseSemaphore ( ) ) ;
rsfuture = addrsFuture . thenCompose ( ( Map < String , Collection < Inet SocketAddress> > addrs ) - > {
rsfuture = addrsFuture . thenCompose ( ( Map < String , Collection < Web SocketAddress> > addrs ) - > {
if ( addrs = = null | | addrs . isEmpty ( ) ) {
if ( logger . isLoggable ( Level . FINER ) ) logger . finer ( " websocket not found userids: " + JsonConvert . root ( ) . convertTo ( userids ) + " on any node " ) ;
return CompletableFuture . completedFuture ( RETCODE_GROUP_EMPTY ) ;
}
Map < Inet SocketAddress, List < Serializable > > addrUsers = new HashMap < > ( ) ;
Map < Web SocketAddress, List < Serializable > > addrUsers = new HashMap < > ( ) ;
addrs . forEach ( ( key , as ) - > {
for ( Inet SocketAddress a : as ) {
for ( Web SocketAddress a : as ) {
addrUsers . computeIfAbsent ( a , k - > new ArrayList < > ( ) ) . add ( keyuser . get ( key ) ) ;
}
} ) ;
@@ -543,7 +550,7 @@ public abstract class WebSocketNode {
logger . finest ( " websocket(localaddr= " + localSncpAddress + " , userids= " + JsonConvert . root ( ) . convertTo ( userids ) + " ) found message-addr-userids: " + addrUsers ) ;
}
CompletableFuture < Integer > future = null ;
for ( Map . Entry < Inet SocketAddress, List < Serializable > > en : addrUsers . entrySet ( ) ) {
for ( Map . Entry < Web SocketAddress, List < Serializable > > en : addrUsers . entrySet ( ) ) {
Serializable [ ] oneaddrUserids = en . getValue ( ) . toArray ( new Serializable [ en . getValue ( ) . size ( ) ] ) ;
future = future = = null ? sendOneAddrMessage ( en . getKey ( ) , remoteMessage , last , oneaddrUserids )
: future . thenCombine ( sendOneAddrMessage ( en . getKey ( ) , remoteMessage , last , oneaddrUserids ) , ( a , b ) - > a | b ) ;
@@ -570,17 +577,17 @@ public abstract class WebSocketNode {
if ( useraddrs = = null | | useraddrs . length < 1 ) return CompletableFuture . completedFuture ( RETCODE_GROUP_EMPTY ) ;
if ( message0 instanceof CompletableFuture ) return ( ( CompletableFuture ) message0 ) . thenApply ( msg - > sendMessage ( convert , msg , last , useraddrs ) ) ;
final Object message = ( convert = = null | | message0 instanceof WebSocketPacket ) ? message0 : ( ( convert instanceof TextConvert ) ? new WebSocketPacket ( ( ( TextConvert ) convert ) . convertTo ( message0 ) , last ) : new WebSocketPacket ( ( ( BinaryConvert ) convert ) . convertTo ( message0 ) , last ) ) ;
if ( this . localEngine ! = null & & this . sncpNodeAddresses = = null ) { //本地模式且没有分布式
if ( this . localEngine ! = null & & this . source = = null ) { //本地模式且没有分布式
return this . localEngine . sendLocalMessage ( message , last , userAddressToUserids ( useraddrs ) ) ;
}
final Object remoteMessage = formatRemoteMessage ( message ) ;
final Map < Inet SocketAddress, List < Serializable > > addrUsers = userAddressToAddrMap ( useraddrs ) ;
final Map < Web SocketAddress, List < Serializable > > addrUsers = userAddressToAddrMap ( useraddrs ) ;
if ( logger . isLoggable ( Level . FINEST ) ) {
logger . finest ( " websocket(localaddr= " + localSncpAddress + " , useraddrs= " + JsonConvert . root ( ) . convertTo ( useraddrs ) + " ) found message-addr-userids: " + addrUsers ) ;
}
CompletableFuture < Integer > future = null ;
for ( Map . Entry < Inet SocketAddress, List < Serializable > > en : addrUsers . entrySet ( ) ) {
for ( Map . Entry < Web SocketAddress, List < Serializable > > en : addrUsers . entrySet ( ) ) {
Serializable [ ] oneaddrUserids = en . getValue ( ) . toArray ( new Serializable [ en . getValue ( ) . size ( ) ] ) ;
future = future = = null ? sendOneAddrMessage ( en . getKey ( ) , remoteMessage , last , oneaddrUserids )
: future . thenCombine ( sendOneAddrMessage ( en . getKey ( ) , remoteMessage , last , oneaddrUserids ) , ( a , b ) - > a | b ) ;
@@ -596,7 +603,7 @@ public abstract class WebSocketNode {
}
CompletableFuture < Integer > localFuture = null ;
if ( this . localEngine ! = null ) localFuture = localEngine . sendLocalMessage ( message , last , userid ) ;
if ( this . sncpNodeAddresses = = null | | this . remoteNode = = null ) {
if ( this . source = = null | | this . remoteNode = = null ) {
if ( logger . isLoggable ( Level . FINEST ) ) logger . finest ( " websocket remote node is null " ) ;
//没有CacheSource就不会有分布式节点
return localFuture = = null ? CompletableFuture . completedFuture ( RETCODE_GROUP_EMPTY ) : localFuture ;
@@ -604,40 +611,40 @@ public abstract class WebSocketNode {
//远程节点发送消息
final Object remoteMessage = formatRemoteMessage ( message ) ;
tryAcquireSemaphore ( ) ;
CompletableFuture < Collection < Inet SocketAddress> > addrsFuture = sncpNodeAddresses . getCollectionAsync ( SOURCE_SNCP_USERID_PREFIX + userid , Inet SocketAddress. class ) ;
CompletableFuture < Collection < Web SocketAddress> > addrsFuture = source . getCollectionAsync ( SOURCE_SNCP_USERID_PREFIX + userid , Web SocketAddress. class ) ;
if ( semaphore ! = null ) addrsFuture . whenComplete ( ( r , e ) - > releaseSemaphore ( ) ) ;
CompletableFuture < Integer > remoteFuture = addrsFuture . thenCompose ( ( Collection < Inet SocketAddress> addrs ) - > {
CompletableFuture < Integer > remoteFuture = addrsFuture . thenCompose ( ( Collection < Web SocketAddress> addrs ) - > {
if ( addrs = = null | | addrs . isEmpty ( ) ) {
if ( logger . isLoggable ( Level . FINER ) ) logger . finer ( " websocket not found userid: " + userid + " on any node " ) ;
return CompletableFuture . completedFuture ( RETCODE_GROUP_EMPTY ) ;
}
if ( logger . isLoggable ( Level . FINEST ) ) logger . finest ( " websocket(localaddr= " + localSncpA ddress + " ) found userid: " + userid + " on " + addrs ) ;
if ( logger . isLoggable ( Level . FINEST ) ) logger . finest ( " websocket(localaddr= " + wsa ddress + " ) found userid: " + userid + " on " + addrs ) ;
CompletableFuture < Integer > future = null ;
for ( Inet SocketAddress addr : addrs ) {
if ( addr = = null | | addr . equals ( localSncpA ddress) ) continue ;
future = future = = null ? remoteNode . sendMessage ( addr , remoteMessage , last , userid )
: future . thenCombine ( remoteNode . sendMessage ( addr , remoteMessage , last , userid ) , ( a , b ) - > a | b ) ;
for ( Web SocketAddress addr : addrs ) {
if ( addr = = null | | addr . equals ( wsa ddress) ) continue ;
future = future = = null ? remoteNode . sendMessage ( addr . getTopic ( ) , addr . getAddr ( ) , remoteMessage , last , userid )
: future . thenCombine ( remoteNode . sendMessage ( addr . getTopic ( ) , addr . getAddr ( ) , remoteMessage , last , userid ) , ( a , b ) - > a | b ) ;
}
return future = = null ? CompletableFuture . completedFuture ( RETCODE_GROUP_EMPTY ) : future ;
} ) ;
return localFuture = = null ? remoteFuture : localFuture . thenCombine ( remoteFuture , ( a , b ) - > a | b ) ;
}
protected CompletableFuture < Integer > sendOneAddrMessage ( final Inet SocketAddress sncpA ddr, final Object message , final boolean last , final Serializable . . . userids ) {
if ( message instanceof CompletableFuture ) return ( ( CompletableFuture ) message ) . thenApply ( msg - > sendOneAddrMessage ( sncpA ddr, msg , last , userids ) ) ;
protected CompletableFuture < Integer > sendOneAddrMessage ( final Web SocketAddress a ddr, final Object message , final boolean last , final Serializable . . . userids ) {
if ( message instanceof CompletableFuture ) return ( ( CompletableFuture ) message ) . thenApply ( msg - > sendOneAddrMessage ( a ddr, msg , last , userids ) ) ;
if ( logger . isLoggable ( Level . FINEST ) & & this . localEngine = = null ) { //只打印远程模式的
logger . finest ( " websocket want send message {userids: " + JsonConvert . root ( ) . convertTo ( userids ) + " , sncpaddr: " + sncpA ddr + " , content: " + ( message instanceof WebSocketPacket ? ( ( WebSocketPacket ) message ) . toSimpleString ( ) : ( message instanceof CharSequence ? message : JsonConvert . root ( ) . convertTo ( message ) ) ) + " } from locale node to " + ( ( this . localEngine ! = null ) ? " locale " : " remote " ) + " engine " ) ;
logger . finest ( " websocket want send message {userids: " + JsonConvert . root ( ) . convertTo ( userids ) + " , sncpaddr: " + a ddr + " , content: " + ( message instanceof WebSocketPacket ? ( ( WebSocketPacket ) message ) . toSimpleString ( ) : ( message instanceof CharSequence ? message : JsonConvert . root ( ) . convertTo ( message ) ) ) + " } from locale node to " + ( ( this . localEngine ! = null ) ? " locale " : " remote " ) + " engine " ) ;
}
if ( Objects . equals ( sncpA ddr, this . localSncpA ddress) ) {
if ( Objects . equals ( a ddr, this . wsa ddress) ) {
return this . localEngine = = null ? CompletableFuture . completedFuture ( RETCODE_GROUP_EMPTY ) : localEngine . sendLocalMessage ( message , last , userids ) ;
}
if ( this . sncpNodeAddresses = = null | | this . remoteNode = = null ) {
if ( this . source = = null | | this . remoteNode = = null ) {
if ( logger . isLoggable ( Level . FINEST ) ) logger . finest ( " websocket remote node is null " ) ;
//没有CacheSource就不会有分布式节点
return CompletableFuture . completedFuture ( RETCODE_GROUP_EMPTY ) ;
}
final Object remoteMessage = formatRemoteMessage ( message ) ;
return remoteNode . sendMessage ( sncpAddr , remoteMessage , last , userids ) ;
return remoteNode . sendMessage ( addr . getTopic ( ) , addr . getAddr ( ) , remoteMessage , last , userids ) ;
}
protected Serializable [ ] userAddressToUserids ( WebSocketUserAddress . . . useraddrs ) {
@@ -649,14 +656,14 @@ public abstract class WebSocketNode {
return set . toArray ( new Serializable [ set . size ( ) ] ) ;
}
protected Map < Inet SocketAddress, List < Serializable > > userAddressToAddrMap ( WebSocketUserAddress . . . useraddrs ) {
final Map < Inet SocketAddress, List < Serializable > > addrUsers = new HashMap < > ( ) ;
protected Map < Web SocketAddress, List < Serializable > > userAddressToAddrMap ( WebSocketUserAddress . . . useraddrs ) {
final Map < Web SocketAddress, List < Serializable > > addrUsers = new HashMap < > ( ) ;
for ( WebSocketUserAddress userAddress : useraddrs ) {
if ( userAddress . sncpA ddress( ) ! = null ) {
addrUsers . computeIfAbsent ( userAddress . sncpA ddress( ) , k - > new ArrayList < > ( ) ) . add ( userAddress . userid ( ) ) ;
if ( userAddress . a ddress( ) ! = null ) {
addrUsers . computeIfAbsent ( userAddress . a ddress( ) , k - > new ArrayList < > ( ) ) . add ( userAddress . userid ( ) ) ;
}
if ( userAddress . sncpA ddresses( ) ! = null ) {
for ( Inet SocketAddress addr : userAddress . sncpA ddresses( ) ) {
if ( userAddress . a ddresses( ) ! = null ) {
for ( Web SocketAddress addr : userAddress . a ddresses( ) ) {
if ( addr ! = null ) {
addrUsers . computeIfAbsent ( addr , k - > new ArrayList < > ( ) ) . add ( userAddress . userid ( ) ) ;
}
@@ -773,22 +780,22 @@ public abstract class WebSocketNode {
public CompletableFuture < Integer > broadcastMessage ( final WebSocketRange wsrange , final Convert convert , final Object message0 , final boolean last ) {
if ( message0 instanceof CompletableFuture ) return ( ( CompletableFuture ) message0 ) . thenApply ( msg - > broadcastMessage ( wsrange , convert , msg , last ) ) ;
final Object message = ( convert = = null | | message0 instanceof WebSocketPacket ) ? message0 : ( ( convert instanceof TextConvert ) ? new WebSocketPacket ( ( ( TextConvert ) convert ) . convertTo ( message0 ) , last ) : new WebSocketPacket ( ( ( BinaryConvert ) convert ) . convertTo ( message0 ) , last ) ) ;
if ( this . localEngine ! = null & & this . sncpNodeAddresses = = null ) { //本地模式且没有分布式
if ( this . localEngine ! = null & & this . source = = null ) { //本地模式且没有分布式
return this . localEngine . broadcastLocalMessage ( wsrange , message , last ) ;
}
final Object remoteMessage = formatRemoteMessage ( message ) ;
CompletableFuture < Integer > localFuture = this . localEngine = = null ? null : this . localEngine . broadcastLocalMessage ( wsrange , message , last ) ;
tryAcquireSemaphore ( ) ;
CompletableFuture < Collection < Inet SocketAddress> > addrsFuture = sncpNodeAddresses . getCollectionAsync ( SOURCE_SNCP_ADDRS_KEY , Inet SocketAddress. class ) ;
CompletableFuture < Collection < Web SocketAddress> > addrsFuture = source . getCollectionAsync ( SOURCE_SNCP_ADDRS_KEY , Web SocketAddress. class ) ;
if ( semaphore ! = null ) addrsFuture . whenComplete ( ( r , e ) - > releaseSemaphore ( ) ) ;
CompletableFuture < Integer > remoteFuture = addrsFuture . thenCompose ( ( Collection < Inet SocketAddress> addrs ) - > {
CompletableFuture < Integer > remoteFuture = addrsFuture . thenCompose ( ( Collection < Web SocketAddress> addrs ) - > {
if ( logger . isLoggable ( Level . FINEST ) ) logger . finest ( " websocket broadcast message ( " + remoteMessage + " ) on " + addrs ) ;
if ( addrs = = null | | addrs . isEmpty ( ) ) return CompletableFuture . completedFuture ( 0 ) ;
CompletableFuture < Integer > future = null ;
for ( Inet SocketAddress addr : addrs ) {
if ( addr = = null | | addr . equals ( localSncpA ddress) ) continue ;
future = future = = null ? remoteNode . broadcastMessage ( addr , wsrange , remoteMessage , last )
: future . thenCombine ( remoteNode . broadcastMessage ( addr , wsrange , remoteMessage , last ) , ( a , b ) - > a | b ) ;
for ( Web SocketAddress addr : addrs ) {
if ( addr = = null | | addr . equals ( wsa ddress) ) continue ;
future = future = = null ? remoteNode . broadcastMessage ( addr . getTopic ( ) , addr . getAddr ( ) , wsrange , remoteMessage , last )
: future . thenCombine ( remoteNode . broadcastMessage ( addr . getTopic ( ) , addr . getAddr ( ) , wsrange , remoteMessage , last ) , ( a , b ) - > a | b ) ;
}
return future = = null ? CompletableFuture . completedFuture ( 0 ) : future ;
} ) ;
@@ -804,21 +811,21 @@ public abstract class WebSocketNode {
*/
@Local
public CompletableFuture < Integer > broadcastAction ( final WebSocketAction action ) {
if ( this . localEngine ! = null & & this . sncpNodeAddresses = = null ) { //本地模式且没有分布式
if ( this . localEngine ! = null & & this . source = = null ) { //本地模式且没有分布式
return this . localEngine . broadcastLocalAction ( action ) ;
}
CompletableFuture < Integer > localFuture = this . localEngine = = null ? null : this . localEngine . broadcastLocalAction ( action ) ;
tryAcquireSemaphore ( ) ;
CompletableFuture < Collection < Inet SocketAddress> > addrsFuture = sncpNodeAddresses . getCollectionAsync ( SOURCE_SNCP_ADDRS_KEY , Inet SocketAddress. class ) ;
CompletableFuture < Collection < Web SocketAddress> > addrsFuture = source . getCollectionAsync ( SOURCE_SNCP_ADDRS_KEY , Web SocketAddress. class ) ;
if ( semaphore ! = null ) addrsFuture . whenComplete ( ( r , e ) - > releaseSemaphore ( ) ) ;
CompletableFuture < Integer > remoteFuture = addrsFuture . thenCompose ( ( Collection < Inet SocketAddress> addrs ) - > {
CompletableFuture < Integer > remoteFuture = addrsFuture . thenCompose ( ( Collection < Web SocketAddress> addrs ) - > {
if ( logger . isLoggable ( Level . FINEST ) ) logger . finest ( " websocket broadcast action ( " + action + " ) on " + addrs ) ;
if ( addrs = = null | | addrs . isEmpty ( ) ) return CompletableFuture . completedFuture ( 0 ) ;
CompletableFuture < Integer > future = null ;
for ( Inet SocketAddress addr : addrs ) {
if ( addr = = null | | addr . equals ( localSncpA ddress) ) continue ;
future = future = = null ? remoteNode . broadcastAction ( addr , action )
: future . thenCombine ( remoteNode . broadcastAction ( addr , action ) , ( a , b ) - > a | b ) ;
for ( Web SocketAddress addr : addrs ) {
if ( addr = = null | | addr . equals ( wsa ddress) ) continue ;
future = future = = null ? remoteNode . broadcastAction ( addr . getTopic ( ) , addr . getAddr ( ) , action )
: future . thenCombine ( remoteNode . broadcastAction ( addr . getTopic ( ) , addr . getAddr ( ) , action ) , ( a , b ) - > a | b ) ;
}
return future = = null ? CompletableFuture . completedFuture ( 0 ) : future ;
} ) ;
@@ -844,7 +851,7 @@ public abstract class WebSocketNode {
}
return sendAction ( action , useraddrs ) ;
}
if ( this . localEngine ! = null & & this . sncpNodeAddresses = = null ) { //本地模式且没有分布式
if ( this . localEngine ! = null & & this . source = = null ) { //本地模式且没有分布式
return this . localEngine . sendLocalAction ( action , userids ) ;
}
CompletableFuture < Integer > rsfuture ;
@@ -858,16 +865,16 @@ public abstract class WebSocketNode {
keyuser . put ( keys [ i ] , userids [ i ] ) ;
}
tryAcquireSemaphore ( ) ;
CompletableFuture < Map < String , Collection < Inet SocketAddress> > > addrsFuture = sncpNodeAddresses . getCollectionMapAsync ( true , Inet SocketAddress. class , keys ) ;
CompletableFuture < Map < String , Collection < Web SocketAddress> > > addrsFuture = source . getCollectionMapAsync ( true , Web SocketAddress. class , keys ) ;
if ( semaphore ! = null ) addrsFuture . whenComplete ( ( r , e ) - > releaseSemaphore ( ) ) ;
rsfuture = addrsFuture . thenCompose ( ( Map < String , Collection < Inet SocketAddress> > addrs ) - > {
rsfuture = addrsFuture . thenCompose ( ( Map < String , Collection < Web SocketAddress> > addrs ) - > {
if ( addrs = = null | | addrs . isEmpty ( ) ) {
if ( logger . isLoggable ( Level . FINER ) ) logger . finer ( " websocket not found userids: " + JsonConvert . root ( ) . convertTo ( userids ) + " on any node " ) ;
return CompletableFuture . completedFuture ( RETCODE_GROUP_EMPTY ) ;
}
Map < Inet SocketAddress, List < Serializable > > addrUsers = new HashMap < > ( ) ;
Map < Web SocketAddress, List < Serializable > > addrUsers = new HashMap < > ( ) ;
addrs . forEach ( ( key , as ) - > {
for ( Inet SocketAddress a : as ) {
for ( Web SocketAddress a : as ) {
addrUsers . computeIfAbsent ( a , k - > new ArrayList < > ( ) ) . add ( keyuser . get ( key ) ) ;
}
} ) ;
@@ -875,7 +882,7 @@ public abstract class WebSocketNode {
logger . finest ( " websocket(localaddr= " + localSncpAddress + " , userids= " + JsonConvert . root ( ) . convertTo ( userids ) + " ) found action-userid-addrs: " + addrUsers ) ;
}
CompletableFuture < Integer > future = null ;
for ( Map . Entry < Inet SocketAddress, List < Serializable > > en : addrUsers . entrySet ( ) ) {
for ( Map . Entry < Web SocketAddress, List < Serializable > > en : addrUsers . entrySet ( ) ) {
Serializable [ ] oneaddrUserids = en . getValue ( ) . toArray ( new Serializable [ en . getValue ( ) . size ( ) ] ) ;
future = future = = null ? sendOneAddrAction ( en . getKey ( ) , action , oneaddrUserids )
: future . thenCombine ( sendOneAddrAction ( en . getKey ( ) , action , oneaddrUserids ) , ( a , b ) - > a | b ) ;
@@ -898,16 +905,16 @@ public abstract class WebSocketNode {
@Local
public CompletableFuture < Integer > sendAction ( final WebSocketAction action , final WebSocketUserAddress . . . useraddrs ) {
if ( useraddrs = = null | | useraddrs . length < 1 ) return CompletableFuture . completedFuture ( RETCODE_GROUP_EMPTY ) ;
if ( this . localEngine ! = null & & this . sncpNodeAddresses = = null ) { //本地模式且没有分布式
if ( this . localEngine ! = null & & this . source = = null ) { //本地模式且没有分布式
return this . localEngine . sendLocalAction ( action , userAddressToUserids ( useraddrs ) ) ;
}
final Map < Inet SocketAddress, List < Serializable > > addrUsers = userAddressToAddrMap ( useraddrs ) ;
final Map < Web SocketAddress, List < Serializable > > addrUsers = userAddressToAddrMap ( useraddrs ) ;
if ( logger . isLoggable ( Level . FINEST ) ) {
logger . finest ( " websocket(localaddr= " + localSncpAddress + " , useraddrs= " + JsonConvert . root ( ) . convertTo ( useraddrs ) + " ) found action-userid-addrs: " + addrUsers ) ;
}
CompletableFuture < Integer > future = null ;
for ( Map . Entry < Inet SocketAddress, List < Serializable > > en : addrUsers . entrySet ( ) ) {
for ( Map . Entry < Web SocketAddress, List < Serializable > > en : addrUsers . entrySet ( ) ) {
Serializable [ ] oneaddrUserids = en . getValue ( ) . toArray ( new Serializable [ en . getValue ( ) . size ( ) ] ) ;
future = future = = null ? sendOneAddrAction ( en . getKey ( ) , action , oneaddrUserids )
: future . thenCombine ( sendOneAddrAction ( en . getKey ( ) , action , oneaddrUserids ) , ( a , b ) - > a | b ) ;
@@ -921,45 +928,45 @@ public abstract class WebSocketNode {
}
CompletableFuture < Integer > localFuture = null ;
if ( this . localEngine ! = null ) localFuture = localEngine . sendLocalAction ( action , userid ) ;
if ( this . sncpNodeAddresses = = null | | this . remoteNode = = null ) {
if ( this . source = = null | | this . remoteNode = = null ) {
if ( logger . isLoggable ( Level . FINEST ) ) logger . finest ( " websocket remote node is null " ) ;
//没有CacheSource就不会有分布式节点
return localFuture = = null ? CompletableFuture . completedFuture ( RETCODE_GROUP_EMPTY ) : localFuture ;
}
//远程节点发送操作
tryAcquireSemaphore ( ) ;
CompletableFuture < Collection < Inet SocketAddress> > addrsFuture = sncpNodeAddresses . getCollectionAsync ( SOURCE_SNCP_USERID_PREFIX + userid , Inet SocketAddress. class ) ;
CompletableFuture < Collection < Web SocketAddress> > addrsFuture = source . getCollectionAsync ( SOURCE_SNCP_USERID_PREFIX + userid , Web SocketAddress. class ) ;
if ( semaphore ! = null ) addrsFuture . whenComplete ( ( r , e ) - > releaseSemaphore ( ) ) ;
CompletableFuture < Integer > remoteFuture = addrsFuture . thenCompose ( ( Collection < Inet SocketAddress> addrs ) - > {
CompletableFuture < Integer > remoteFuture = addrsFuture . thenCompose ( ( Collection < Web SocketAddress> addrs ) - > {
if ( addrs = = null | | addrs . isEmpty ( ) ) {
if ( logger . isLoggable ( Level . FINER ) ) logger . finer ( " websocket not found userid: " + userid + " on any node " ) ;
return CompletableFuture . completedFuture ( RETCODE_GROUP_EMPTY ) ;
}
if ( logger . isLoggable ( Level . FINEST ) ) logger . finest ( " websocket(localaddr= " + localSncpAddress + " ) found userid: " + userid + " on " + addrs ) ;
CompletableFuture < Integer > future = null ;
for ( Inet SocketAddress addr : addrs ) {
if ( addr = = null | | addr . equals ( localSncpA ddress) ) continue ;
future = future = = null ? remoteNode . sendAction ( addr , action , userid )
: future . thenCombine ( remoteNode . sendAction ( addr , action , userid ) , ( a , b ) - > a | b ) ;
for ( Web SocketAddress addr : addrs ) {
if ( addr = = null | | addr . equals ( wsa ddress) ) continue ;
future = future = = null ? remoteNode . sendAction ( addr . getTopic ( ) , addr . getAddr ( ) , action , userid )
: future . thenCombine ( remoteNode . sendAction ( addr . getTopic ( ) , addr . getAddr ( ) , action , userid ) , ( a , b ) - > a | b ) ;
}
return future = = null ? CompletableFuture . completedFuture ( RETCODE_GROUP_EMPTY ) : future ;
} ) ;
return localFuture = = null ? remoteFuture : localFuture . thenCombine ( remoteFuture , ( a , b ) - > a | b ) ;
}
protected CompletableFuture < Integer > sendOneAddrAction ( final Inet SocketAddress sncpA ddr, final WebSocketAction action , final Serializable . . . userids ) {
protected CompletableFuture < Integer > sendOneAddrAction ( final Web SocketAddress a ddr, final WebSocketAction action , final Serializable . . . userids ) {
if ( logger . isLoggable ( Level . FINEST ) & & this . localEngine = = null ) { //只打印远程模式的
logger . finest ( " websocket want send action {userids: " + JsonConvert . root ( ) . convertTo ( userids ) + " , sncpaddr: " + sncpA ddr + " , action: " + action + " from locale node to " + ( ( this . localEngine ! = null ) ? " locale " : " remote " ) + " engine " ) ;
logger . finest ( " websocket want send action {userids: " + JsonConvert . root ( ) . convertTo ( userids ) + " , sncpaddr: " + a ddr + " , action: " + action + " from locale node to " + ( ( this . localEngine ! = null ) ? " locale " : " remote " ) + " engine " ) ;
}
if ( Objects . equals ( sncpA ddr, this . localSncpA ddress) ) {
if ( Objects . equals ( a ddr, this . wsa ddress) ) {
return this . localEngine = = null ? CompletableFuture . completedFuture ( RETCODE_GROUP_EMPTY ) : localEngine . sendLocalAction ( action , userids ) ;
}
if ( this . sncpNodeAddresses = = null | | this . remoteNode = = null ) {
if ( this . source = = null | | this . remoteNode = = null ) {
if ( logger . isLoggable ( Level . FINEST ) ) logger . finest ( " websocket remote node is null " ) ;
//没有CacheSource就不会有分布式节点
return CompletableFuture . completedFuture ( RETCODE_GROUP_EMPTY ) ;
}
return remoteNode . sendAction ( sncpAddr , action , userids ) ;
return remoteNode . sendAction ( addr . getTopic ( ) , addr . getAddr ( ) , action , userids ) ;
}
protected Object formatRemoteMessage ( Object message ) {