Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
duniter4j
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
1
Issues
1
List
Boards
Labels
Service Desk
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
clients
java
duniter4j
Commits
2b595812
Commit
2b595812
authored
Jan 22, 2019
by
Benoit Lavenier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[fix] clean code on peers async refreshing
parent
3c9cebfe
Pipeline
#4432
passed with stage
in 45 seconds
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
45 additions
and
30 deletions
+45
-30
duniter4j-core-client/src/main/java/org/duniter/core/client/service/local/NetworkServiceImpl.java
...duniter/core/client/service/local/NetworkServiceImpl.java
+26
-27
duniter4j-core-shared/src/main/java/org/duniter/core/util/concurrent/CompletableFutures.java
.../org/duniter/core/util/concurrent/CompletableFutures.java
+2
-3
duniter4j-core-shared/src/main/java/org/duniter/core/util/http/InetAddressUtils.java
...ain/java/org/duniter/core/util/http/InetAddressUtils.java
+8
-0
duniter4j-core-shared/src/test/java/org/duniter/core/util/http/InetAddressUtilsTest.java
...java/org/duniter/core/util/http/InetAddressUtilsTest.java
+9
-0
No files found.
duniter4j-core-client/src/main/java/org/duniter/core/client/service/local/NetworkServiceImpl.java
View file @
2b595812
...
...
@@ -176,14 +176,9 @@ public class NetworkServiceImpl extends BaseRemoteServiceImpl implements Network
peer
=
mainPeer
;
}
// Exclude peer with only a local IPv4 address
else
if
(
InetAddressUtils
.
isLocalIPv4Address
(
peer
.
getHost
()))
{
return
null
;
}
// Exclude localhost address
else
if
(
"localhost"
.
equalsIgnoreCase
(
peer
.
getHost
()))
{
return
null
;
// Exclude peer with only a local IPv4 address (or localhost)
else
if
(
InetAddressUtils
.
isLocalAddress
(
peer
.
getHost
()))
{
return
CompletableFuture
.<
Peer
>
completedFuture
(
null
);
}
return
asyncRefreshPeer
(
peer
,
memberUids
,
pool
);
...
...
@@ -195,7 +190,8 @@ public class NetworkServiceImpl extends BaseRemoteServiceImpl implements Network
public
CompletableFuture
<
Peer
>
asyncRefreshPeer
(
final
Peer
peer
,
final
Map
<
String
,
String
>
memberUids
,
final
ExecutorService
pool
)
{
System
.
out
.
println
(
"Refreshing peer: "
+
peer
.
toString
());
if
(
log
.
isDebugEnabled
())
log
.
debug
(
String
.
format
(
"[%s] Refreshing peer status"
,
peer
.
toString
()));
return
CompletableFuture
.
supplyAsync
(()
->
fillNodeSummary
(
peer
),
pool
)
.
thenApplyAsync
(
this
::
fillCurrentBlock
)
.
exceptionally
(
throwable
->
{
...
...
@@ -615,32 +611,36 @@ public class NetworkServiceImpl extends BaseRemoteServiceImpl implements Network
}
protected
Peer
fillNodeSummary
(
final
Peer
peer
)
{
// Skip if no BMA, BMAS or ES_CORE_API
if
(!
Peers
.
hasBmaEndpoint
(
peer
)
&&
!
Peers
.
hasEsCoreEndpoint
(
peer
))
return
peer
;
JsonNode
summary
=
getNodeSummary
(
peer
);
peer
.
getStats
().
setVersion
(
getVersion
(
summary
));
peer
.
getStats
().
setSoftware
(
getSoftware
(
summary
));
return
peer
;
}
protected
Peer
fillCurrentBlock
(
final
Peer
peer
)
{
if
(
Peers
.
hasBmaEndpoint
(
peer
)
||
Peers
.
hasEsCoreEndpoint
(
peer
))
{
JsonNode
json
=
get
(
peer
,
BMA_URL_BLOCKCHAIN_CURRENT
)
;
// Skip if no BMA, BMAS or ES_CORE_API
if
(!
Peers
.
hasBmaEndpoint
(
peer
)
&&
!
Peers
.
hasEsCoreEndpoint
(
peer
))
return
peer
;
String
currency
=
json
.
has
(
"currency"
)
?
json
.
get
(
"currency"
).
asText
()
:
null
;
peer
.
setCurrency
(
currency
);
JsonNode
json
=
get
(
peer
,
BMA_URL_BLOCKCHAIN_CURRENT
);
Integer
number
=
json
.
has
(
"number"
)
?
json
.
get
(
"number"
).
asIn
t
()
:
null
;
peer
.
getStats
().
setBlockNumber
(
number
);
String
currency
=
json
.
has
(
"currency"
)
?
json
.
get
(
"currency"
).
asTex
t
()
:
null
;
peer
.
setCurrency
(
currency
);
String
hash
=
json
.
has
(
"hash"
)
?
json
.
get
(
"hash"
).
asTex
t
()
:
null
;
peer
.
getStats
().
setBlockHash
(
hash
);
Integer
number
=
json
.
has
(
"number"
)
?
json
.
get
(
"number"
).
asIn
t
()
:
null
;
peer
.
getStats
().
setBlockNumber
(
number
);
Long
medianTime
=
json
.
has
(
"medianTime"
)
?
json
.
get
(
"medianTime"
).
asLong
()
:
null
;
peer
.
getStats
().
setMedianTime
(
medianTime
);
String
hash
=
json
.
has
(
"hash"
)
?
json
.
get
(
"hash"
).
asText
()
:
null
;
peer
.
getStats
().
setBlockHash
(
hash
);
if
(
log
.
isTraceEnabled
())
{
log
.
trace
(
String
.
format
(
"[%s] current block [%s-%s]"
,
peer
.
toString
(),
number
,
hash
));
}
Long
medianTime
=
json
.
has
(
"medianTime"
)
?
json
.
get
(
"medianTime"
).
asLong
()
:
null
;
peer
.
getStats
().
setMedianTime
(
medianTime
);
if
(
log
.
isTraceEnabled
())
{
log
.
trace
(
String
.
format
(
"[%s] current block [%s-%s]"
,
peer
.
toString
(),
number
,
hash
));
}
return
peer
;
...
...
@@ -675,17 +675,16 @@ public class NetworkServiceImpl extends BaseRemoteServiceImpl implements Network
peers
.
forEach
(
peerFound
->
{
if
(
peerFound
.
getStats
().
getStatus
()
==
Peer
.
PeerStatus
.
DOWN
)
{
String
error
=
peerFound
.
getStats
().
getError
();
log
.
trace
(
String
.
format
(
"
peer [%s] [%s]
%s"
,
log
.
trace
(
String
.
format
(
"
[%s] status is %s
%s"
,
peerFound
.
toString
(),
peerFound
.
getStats
().
getStatus
()
.
name
(),
error
!=
null
?
error
:
""
));
Peer
.
PeerStatus
.
DOWN
.
name
(),
error
!=
null
?
(
":"
+
error
)
:
""
));
}
else
{
log
.
trace
(
String
.
format
(
"
peer [%s] [%s]
[v%s] block [%s]"
,
peerFound
.
toString
(),
log
.
trace
(
String
.
format
(
"
[%s] status %s:
[v%s] block [%s]"
,
peerFound
.
toString
(),
peerFound
.
getStats
().
getStatus
().
name
(),
peerFound
.
getStats
().
getVersion
(),
peerFound
.
getStats
().
getBlockNumber
()
));
}
});
}
...
...
duniter4j-core-shared/src/main/java/org/duniter/core/util/concurrent/CompletableFutures.java
View file @
2b595812
...
...
@@ -22,8 +22,6 @@ package org.duniter.core.util.concurrent;
* #L%
*/
import
org.apache.commons.collections4.CollectionUtils
;
import
java.util.List
;
import
java.util.Objects
;
import
java.util.concurrent.CompletableFuture
;
...
...
@@ -40,7 +38,6 @@ public class CompletableFutures {
}
public
static
<
T
>
CompletableFuture
<
List
<
T
>>
allOfToList
(
List
<
CompletableFuture
<
T
>>
futures
)
{
CollectionUtils
.
filter
(
futures
,
Objects:
:
nonNull
);
CompletableFuture
<
Void
>
allDoneFuture
=
CompletableFuture
.
allOf
(
futures
.
toArray
(
new
CompletableFuture
[
futures
.
size
()]));
return
allDoneFuture
.
thenApply
(
v
->
...
...
@@ -57,8 +54,10 @@ public class CompletableFutures {
return
allDoneFuture
.
thenApply
(
v
->
futures
.
stream
()
.
map
(
future
->
future
.
join
())
.
filter
(
Objects:
:
nonNull
)
// skip empty result
.
filter
(
filter
)
.
collect
(
Collectors
.
toList
())
);
}
}
duniter4j-core-shared/src/main/java/org/duniter/core/util/http/InetAddressUtils.java
View file @
2b595812
...
...
@@ -51,4 +51,12 @@ public class InetAddressUtils {
public
static
boolean
isIPv6Address
(
String
input
)
{
return
org
.
apache
.
http
.
conn
.
util
.
InetAddressUtils
.
isIPv6Address
(
input
);
}
public
static
boolean
isLocalAddress
(
String
input
)
{
return
isLocalIPv4Address
(
input
)
||
"localhost"
.
equalsIgnoreCase
(
input
);
}
public
static
boolean
isNotLocalAddress
(
String
input
)
{
return
!
isLocalAddress
(
input
);
}
}
duniter4j-core-shared/src/test/java/org/duniter/core/util/http/InetAddressUtilsTest.java
View file @
2b595812
...
...
@@ -44,6 +44,15 @@ public class InetAddressUtilsTest {
boolean
check
=
InetAddressUtils
.
isLocalIPv4Address
(
"192.168.1.11"
);
Assert
.
assertTrue
(
check
);
check
=
InetAddressUtils
.
isLocalIPv4Address
(
"127.0.0.1"
);
Assert
.
assertTrue
(
check
);
}
@Test
public
void
isLocalAddress
()
{
boolean
check
=
InetAddressUtils
.
isLocalAddress
(
"localhost"
);
Assert
.
assertTrue
(
check
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment