Coverage Summary for Class: StaticMessages (org.ethereum.net.message)
Class |
Class, %
|
Method, %
|
Line, %
|
StaticMessages |
0%
(0/1)
|
0%
(0/5)
|
0%
(0/26)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2017 RSK Labs Ltd.
4 * (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>)
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package org.ethereum.net.message;
21
22 import org.ethereum.config.SystemProperties;
23 import org.ethereum.net.client.Capability;
24 import org.ethereum.net.client.ConfigCapabilities;
25 import org.ethereum.net.p2p.*;
26
27 import java.util.List;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30
31 /**
32 * This class contains static values of messages on the network. These message
33 * will always be the same and therefore don't need to be created each time.
34 *
35 * @author Roman Mandeleil
36 * @since 13.04.14
37 */
38 public class StaticMessages {
39
40 private final SystemProperties config;
41 private final ConfigCapabilities configCapabilities;
42
43 public static final PingMessage PING_MESSAGE = new PingMessage();
44 public static final PongMessage PONG_MESSAGE = new PongMessage();
45 public static final GetPeersMessage GET_PEERS_MESSAGE = new GetPeersMessage();
46 public static final DisconnectMessage DISCONNECT_MESSAGE = new DisconnectMessage(ReasonCode.REQUESTED);
47
48 public StaticMessages(SystemProperties config, ConfigCapabilities configCapabilities) {
49 this.config = config;
50 this.configCapabilities = configCapabilities;
51 }
52
53 public HelloMessage createHelloMessage(String peerId) {
54 return createHelloMessage(peerId, config.getPeerPort());
55 }
56 public HelloMessage createHelloMessage(String peerId, int listenPort) {
57
58 String helloAnnouncement = buildHelloAnnouncement();
59 byte p2pVersion = (byte) config.defaultP2PVersion();
60 List<Capability> capabilities = configCapabilities.getConfigCapabilities();
61
62 return new HelloMessage(p2pVersion, helloAnnouncement,
63 capabilities, listenPort, peerId);
64 }
65
66 private String buildHelloAnnouncement() {
67 String version = config.projectVersion();
68 String numberVersion = version;
69 Pattern pattern = Pattern.compile("^\\d+(\\.\\d+)*");
70 Matcher matcher = pattern.matcher(numberVersion);
71 if (matcher.find()) {
72 numberVersion = numberVersion.substring(matcher.start(), matcher.end());
73 }
74 String system = System.getProperty("os.name");
75 if (system.contains(" ")) {
76 system = system.substring(0, system.indexOf(" "));
77 }
78 if (System.getProperty("java.vm.vendor").contains("Android")) {
79 system = "Android";
80 }
81 String phrase = config.helloPhrase();
82
83 return String.format("Ethereum(J)/v%s/%s/%s/Java/%s", numberVersion, system,
84 config.projectVersionModifier().equalsIgnoreCase("release") ? "Release" : "Dev", phrase);
85 }
86 }