Some basic entities and project structure

This commit is contained in:
Christian Basler 2015-03-20 14:18:29 +01:00
commit caf66ea9a5
24 changed files with 1614 additions and 0 deletions

158
.gitignore vendored Normal file
View File

@ -0,0 +1,158 @@
# Created by https://www.gitignore.io
### Gradle ###
.gradle
build/
# Ignore Gradle GUI config
gradle-app.setting
# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
!gradle-wrapper.jar
### Intellij ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm
*.iml
## Directory-based project format:
.idea/
# if you remove the above rule, at least ignore the following:
# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries
# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml
# Gradle:
# .idea/gradle.xml
# .idea/libraries
# Mongo Explorer plugin:
# .idea/mongoSettings.xml
## File-based project format:
*.ipr
*.iws
## Plugin-specific files:
# IntelliJ
/out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
### Eclipse ###
*.pydevproject
.metadata
.gradle
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
# Eclipse Core
.project
# External tool builders
.externalToolBuilders/
# Locally stored "Eclipse launch configurations"
*.launch
# CDT-specific
.cproject
# JDT-specific (Eclipse Java Development Tools)
.classpath
# PDT-specific
.buildpath
# sbteclipse plugin
.target
# TeXlipse plugin
.texlipse
### Linux ###
*~
# KDE directory preferences
.directory
# Linux trash folder which might appear on any partition or disk
.Trash-*
### OSX ###
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### Windows ###
# Windows image file caches
Thumbs.db
ehthumbs.db
# Folder config file
Desktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msm
*.msp
# Windows shortcuts
*.lnk

201
LICENSE Normal file
View File

@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright {yyyy} {name of copyright owner}
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

12
build.gradle Normal file
View File

@ -0,0 +1,12 @@
apply plugin: 'java'
sourceCompatibility = 1.7
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.11'
}

12
domain/build.gradle Normal file
View File

@ -0,0 +1,12 @@
apply plugin: 'java'
sourceCompatibility = 1.7
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}

View File

@ -0,0 +1,65 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.entity;
import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
import ch.dissem.bitmessage.utils.Encode;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* Created by chris on 13.03.15.
*/
public class Addr implements MessagePayload {
private final List<NetworkAddress> addresses;
private Addr(Builder builder) {
addresses = builder.addresses;
}
@Override
public String getCommand() {
return "addr";
}
@Override
public void write(OutputStream stream) throws IOException {
Encode.varInt(addresses.size(), stream);
for (NetworkAddress address : addresses) {
address.write(stream);
}
}
public static final class Builder {
private List<NetworkAddress> addresses = new ArrayList<NetworkAddress>();
public Builder() {
}
public Builder addAddress(final NetworkAddress address) {
this.addresses.add(address);
return this;
}
public Addr build() {
return new Addr(this);
}
}
}

View File

@ -0,0 +1,24 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.entity;
/**
* Created by chris on 10.03.15.
*/
public interface MessagePayload extends Streamable {
String getCommand();
}

View File

@ -0,0 +1,90 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.entity;
import ch.dissem.bitmessage.utils.Encode;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
/**
* Created by chris on 10.03.15.
*/
public class NetworkMessage implements Streamable {
/**
* Magic value indicating message origin network, and used to seek to next message when stream state is unknown
*/
private final static int MAGIC = 0xE9BEB4D9;
private MessagePayload payload;
public NetworkMessage(MessagePayload payload) {
this.payload = payload;
}
/**
* First 4 bytes of sha512(payload)
*/
private byte[] getChecksum(byte[] bytes) throws NoSuchProviderException, NoSuchAlgorithmException {
MessageDigest mda = MessageDigest.getInstance("SHA-512");
byte[] d = mda.digest(bytes);
return new byte[]{d[0], d[1], d[2], d[3]};
}
/**
* The actual data, a message or an object. Not to be confused with objectPayload.
*/
public MessagePayload getPayload() {
return payload;
}
@Override
public void write(OutputStream stream) throws IOException {
// magic
Encode.int32(MAGIC, stream);
// ASCII string identifying the packet content, NULL padded (non-NULL padding results in packet rejected)
stream.write(payload.getCommand().getBytes("ASCII"));
for (int i = payload.getCommand().length(); i < 12; i++) {
stream.write('\0');
}
ByteArrayOutputStream payloadStream = new ByteArrayOutputStream();
payload.write(payloadStream);
byte[] payloadBytes = payloadStream.toByteArray();
// Length of payload in number of bytes. Because of other restrictions, there is no reason why this length would
// ever be larger than 1600003 bytes. Some clients include a sanity-check to avoid processing messages which are
// larger than this.
Encode.int32(payloadBytes.length, stream);
// checksum
try {
stream.write(getChecksum(payloadBytes));
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
}
// message payload
stream.write(payloadBytes);
}
}

View File

@ -0,0 +1,23 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.entity;
/**
* Created by chris on 16.03.15.
*/
public interface ObjectPayload {
}

View File

@ -0,0 +1,27 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.entity;
import java.io.IOException;
import java.io.OutputStream;
/**
* Created by chris on 10.03.15.
*/
public interface Streamable {
void write(OutputStream stream) throws IOException;
}

View File

@ -0,0 +1,35 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.entity;
import java.io.IOException;
import java.io.OutputStream;
/**
* Created by chris on 10.03.15.
*/
public class VerAck implements MessagePayload {
@Override
public String getCommand() {
return "verack";
}
@Override
public void write(OutputStream stream) throws IOException {
// NO OP
}
}

View File

@ -0,0 +1,191 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.entity;
import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
import ch.dissem.bitmessage.utils.Encode;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
/**
* Created by chris on 10.03.15.
*/
public class Version implements MessagePayload {
/**
* Identifies protocol version being used by the node. Should equal 3. Nodes should disconnect if the remote node's
* version is lower but continue with the connection if it is higher.
*/
private final int version;
/**
* bitfield of features to be enabled for this connection
*/
private final long services;
/**
* standard UNIX timestamp in seconds
*/
private final long timestamp;
/**
* The network address of the node receiving this message (not including the time or stream number)
*/
private final NetworkAddress addrRecv;
/**
* The network address of the node emitting this message (not including the time or stream number and the ip itself
* is ignored by the receiver)
*/
private final NetworkAddress addrFrom;
/**
* Random nonce used to detect connections to self.
*/
private final long nonce;
/**
* User Agent (0x00 if string is 0 bytes long). Sending nodes must not include a user_agent longer than 5000 bytes.
*/
private final String userAgent;
/**
* The stream numbers that the emitting node is interested in. Sending nodes must not include more than 160000
* stream numbers.
*/
private final long[] streamNumbers;
public int getVersion() {
return version;
}
public long getServices() {
return services;
}
public long getTimestamp() {
return timestamp;
}
public NetworkAddress getAddrRecv() {
return addrRecv;
}
public NetworkAddress getAddrFrom() {
return addrFrom;
}
public long getNonce() {
return nonce;
}
public String getUserAgent() {
return userAgent;
}
public long[] getStreamNumbers() {
return streamNumbers;
}
private Version(Builder builder) {
version = builder.version;
services = builder.services;
timestamp = builder.timestamp;
addrRecv = builder.addrRecv;
addrFrom = builder.addrFrom;
nonce = builder.nonce;
userAgent = builder.userAgent;
streamNumbers = builder.streamNumbers;
}
@Override
public String getCommand() {
return "ver";
}
@Override
public void write(OutputStream stream) throws IOException {
Encode.int32(version, stream);
Encode.int64(services, stream);
Encode.int64(timestamp, stream);
addrRecv.write(stream);
addrFrom.write(stream);
Encode.int64(nonce, stream);
Encode.varString(userAgent, stream);
Encode.varIntList(streamNumbers, stream);
}
public static final class Builder {
private int version = 3;
private long services = 1; // This is a normal network node
private long timestamp = System.currentTimeMillis() / 1000;
private NetworkAddress addrRecv;
private NetworkAddress addrFrom;
private long nonce = new Random().nextInt();
private String userAgent = "/Jabit:0.0.1/";
private long[] streamNumbers = {1};
public Builder() {
}
public Builder version(int version) {
this.version = version;
return this;
}
public Builder services(long services) {
this.services = services;
return this;
}
public Builder timestamp(long timestamp) {
this.timestamp = timestamp;
return this;
}
public Builder addrRecv(NetworkAddress addrRecv) {
this.addrRecv = addrRecv;
return this;
}
public Builder addrFrom(NetworkAddress addrFrom) {
this.addrFrom = addrFrom;
return this;
}
public Builder nonce(long nonce) {
this.nonce = nonce;
return this;
}
public Builder userAgent(String userAgent) {
this.userAgent = userAgent;
return this;
}
public Builder streamNumbers(long... streamNumbers) {
this.streamNumbers = streamNumbers;
return this;
}
public Version build() {
return new Version(this);
}
}
}

View File

@ -0,0 +1,41 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.entity.valueobject;
import ch.dissem.bitmessage.entity.Streamable;
import java.io.IOException;
import java.io.OutputStream;
/**
* Created by chris on 13.03.15.
*/
public class InventoryVector implements Streamable {
/**
* Hash of the object
*/
private final byte[] hash;
public InventoryVector(byte[] hash) {
this.hash = hash;
}
@Override
public void write(OutputStream stream) throws IOException {
stream.write(hash);
}
}

View File

@ -0,0 +1,155 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.entity.valueobject;
import ch.dissem.bitmessage.entity.Streamable;
import ch.dissem.bitmessage.utils.Encode;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Objects;
/**
* Created by chris on 10.03.15.
*/
public class NetworkAddress implements Streamable {
private long time;
/**
* Stream number for this node
*/
private int stream;
/**
* same service(s) listed in version
*/
private long services;
/**
* IPv6 address. IPv4 addresses are written into the message as a 16 byte IPv4-mapped IPv6 address
* (12 bytes 00 00 00 00 00 00 00 00 00 00 FF FF, followed by the 4 bytes of the IPv4 address).
*/
private byte[] ipv6;
private int port;
public int getPort() {
return port;
}
public InetAddress toInetAddress() {
try {
return InetAddress.getByAddress(ipv6);
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
}
private NetworkAddress(Builder builder) {
time = builder.time;
stream = builder.stream;
services = builder.services;
ipv6 = builder.ipv6;
port = builder.port;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
NetworkAddress that = (NetworkAddress) o;
return port == that.port && Arrays.equals(ipv6, that.ipv6);
}
@Override
public int hashCode() {
int result = ipv6 != null ? Arrays.hashCode(ipv6) : 0;
result = 31 * result + port;
return result;
}
@Override
public void write(OutputStream stream) throws IOException {
Encode.int64(time, stream);
Encode.int32(this.stream, stream);
Encode.int64(services, stream);
stream.write(ipv6);
Encode.int16(port, stream);
}
public static final class Builder {
private long time;
private int stream;
private long services = 1;
private byte[] ipv6;
private int port;
public Builder() {
}
public Builder time(final long time) {
this.time = time;
return this;
}
public Builder stream(final int stream) {
this.stream = stream;
return this;
}
public Builder services(final long services) {
this.services = services;
return this;
}
public Builder ipv6(int p00, int p01, int p02, int p03,
int p04, int p05, int p06, int p07,
int p08, int p09, int p10, int p11,
int p12, int p13, int p14, int p15) {
this.ipv6 = new byte[]{
(byte) p00, (byte) p01, (byte) p02, (byte) p03,
(byte) p04, (byte) p05, (byte) p06, (byte) p07,
(byte) p08, (byte) p09, (byte) p10, (byte) p11,
(byte) p12, (byte) p13, (byte) p14, (byte) p15
};
return this;
}
public Builder ipv4(int p00, int p01, int p02, int p03) {
this.ipv6 = new byte[]{
(byte) 0, (byte) 0, (byte) 0x00, (byte) 0x00,
(byte) 0, (byte) 0, (byte) 0x00, (byte) 0x00,
(byte) 0, (byte) 0, (byte) 0xff, (byte) 0xff,
(byte) p00, (byte) p01, (byte) p02, (byte) p03
};
return this;
}
public Builder port(final int port) {
this.port = port;
return this;
}
public NetworkAddress build() {
return new NetworkAddress(this);
}
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.ports;
import ch.dissem.bitmessage.entity.ObjectPayload;
import ch.dissem.bitmessage.entity.valueobject.InventoryVector;
import java.util.List;
/**
* Created by chris on 16.03.15.
*/
public interface Inventory {
public List<InventoryVector> getInventory();
public List<InventoryVector> getMissing(List<InventoryVector> offer);
public ObjectPayload getObject(InventoryVector vector);
public void storeObject(InventoryVector vector, ObjectPayload object);
public void cleanup();
}

View File

@ -0,0 +1,33 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.ports;
import ch.dissem.bitmessage.entity.NetworkMessage;
import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
/**
* Created by chris on 16.03.15.
*/
public interface NetworkMessageReceiver {
public void registerListener(int port);
public void registerListener(NetworkAddress node, MessageListener listener);
public static interface MessageListener {
public void receive(NetworkMessage message);
}
}

View File

@ -0,0 +1,27 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.ports;
import ch.dissem.bitmessage.entity.NetworkMessage;
import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
/**
* Created by chris on 16.03.15.
*/
public interface NetworkMessageSender {
public void send(NetworkAddress node, NetworkMessage message);
}

View File

@ -0,0 +1,77 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.utils;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
/**
* Created by chris on 13.03.15.
*/
public class Encode {
public static void varIntList(long[] values, OutputStream stream) throws IOException {
varInt(values.length, stream);
for (long value : values) {
varInt(value, stream);
}
}
public static void varInt(long value, OutputStream stream) throws IOException {
if (value < 0) {
// This is due to the fact that Java doesn't really support unsigned values.
// Please be aware that this might be an error due to a smaller negative value being cast to long.
// Normally, negative values shouldn't occur within the protocol, and I large enough longs
// to being recognized as negatives aren't realistic.
stream.write(0xff);
int64(value, stream);
} else if (value < 0xfd) {
int8(value, stream);
} else if (value <= 0xffffL) {
stream.write(0xfd);
int16(value, stream);
} else if (value <= 0xffffffffL) {
stream.write(0xfe);
int32(value, stream);
} else {
stream.write(0xff);
int64(value, stream);
}
}
public static void int8(long value, OutputStream stream) throws IOException {
stream.write((int) value);
}
public static void int16(long value, OutputStream stream) throws IOException {
stream.write(ByteBuffer.allocate(4).putInt((int) value).array(), 2, 2);
}
public static void int32(long value, OutputStream stream) throws IOException {
stream.write(ByteBuffer.allocate(4).putInt((int) value).array());
}
public static void int64(long value, OutputStream stream) throws IOException {
stream.write(ByteBuffer.allocate(8).putLong(value).array());
}
public static void varString(String value, OutputStream stream) throws IOException {
byte[] bytes = value.getBytes("utf-8");
varInt(bytes.length, stream);
stream.write(bytes);
}
}

View File

@ -0,0 +1,127 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.utils;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
/**
* Created by chris on 13.03.15.
*/
public class EncodeTest {
@Test
public void testUint8() throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Encode.int8(0, stream);
checkBytes(stream, 0);
stream = new ByteArrayOutputStream();
Encode.int8(255, stream);
checkBytes(stream, 255);
}
@Test
public void testUint16() throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Encode.int16(0, stream);
checkBytes(stream, 0, 0);
stream = new ByteArrayOutputStream();
Encode.int16(513, stream);
checkBytes(stream, 2, 1);
}
@Test
public void testUint32() throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Encode.int32(0, stream);
checkBytes(stream, 0, 0, 0, 0);
stream = new ByteArrayOutputStream();
Encode.int32(67305985, stream);
checkBytes(stream, 4, 3, 2, 1);
stream = new ByteArrayOutputStream();
Encode.int32(3355443201l, stream);
checkBytes(stream, 200, 0, 0, 1);
}
@Test
public void testUint64() throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Encode.int64(0, stream);
checkBytes(stream, 0, 0, 0, 0, 0, 0, 0, 0);
stream = new ByteArrayOutputStream();
Encode.int64(578437695752307201L, stream);
checkBytes(stream, 8, 7, 6, 5, 4, 3, 2, 1);
stream = new ByteArrayOutputStream();
// 200 * 72057594037927936L + 1
Encode.int64(0xc800000000000001L, stream);
checkBytes(stream, 200, 0, 0, 0, 0, 0, 0, 1);
}
@Test
public void testVarInt() throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Encode.varInt(0, stream);
checkBytes(stream, 0);
stream = new ByteArrayOutputStream();
Encode.varInt(252, stream);
checkBytes(stream, 252);
stream = new ByteArrayOutputStream();
Encode.varInt(253, stream);
checkBytes(stream, 253, 0, 253);
stream = new ByteArrayOutputStream();
Encode.varInt(65535, stream);
checkBytes(stream, 253, 255, 255);
stream = new ByteArrayOutputStream();
Encode.varInt(65536, stream);
checkBytes(stream, 254, 0, 1, 0, 0);
stream = new ByteArrayOutputStream();
Encode.varInt(4294967295L, stream);
checkBytes(stream, 254, 255, 255, 255, 255);
stream = new ByteArrayOutputStream();
Encode.varInt(4294967296L, stream);
checkBytes(stream, 255, 0, 0, 0, 1, 0, 0, 0, 0);
stream = new ByteArrayOutputStream();
Encode.varInt(-1L, stream);
checkBytes(stream, 255, 255, 255, 255, 255, 255, 255, 255, 255);
}
public void checkBytes(ByteArrayOutputStream stream, int... bytes) {
assertEquals(bytes.length, stream.size());
byte[] streamBytes = stream.toByteArray();
for (int i = 0; i < bytes.length; i++) {
assertEquals((byte) bytes[i], streamBytes[i]);
}
}
}

BIN
gradle/wrapper/gradle-wrapper.jar vendored Normal file

Binary file not shown.

View File

@ -0,0 +1,6 @@
#Mon Mar 16 20:59:34 CET 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.1-all.zip

164
gradlew vendored Executable file
View File

@ -0,0 +1,164 @@
#!/usr/bin/env bash
##############################################################################
##
## Gradle start up script for UN*X
##
##############################################################################
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"