2015-03-20 14:18:29 +01:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2015-12-02 17:45:50 +01:00
|
|
|
import ch.dissem.bitmessage.entity.CustomMessage;
|
2015-05-19 19:16:20 +02:00
|
|
|
import ch.dissem.bitmessage.entity.ObjectMessage;
|
2015-05-01 17:14:43 +02:00
|
|
|
import ch.dissem.bitmessage.entity.valueobject.InventoryVector;
|
2015-06-16 06:41:59 +02:00
|
|
|
import ch.dissem.bitmessage.utils.Property;
|
2015-03-20 14:18:29 +01:00
|
|
|
|
2015-05-23 10:27:05 +02:00
|
|
|
import java.io.IOException;
|
2015-08-28 13:48:01 +02:00
|
|
|
import java.net.InetAddress;
|
2016-06-01 17:38:49 +02:00
|
|
|
import java.util.Collection;
|
2015-11-08 10:14:37 +01:00
|
|
|
import java.util.concurrent.Future;
|
2015-05-23 10:27:05 +02:00
|
|
|
|
2015-03-20 14:18:29 +01:00
|
|
|
/**
|
2015-04-07 18:48:58 +02:00
|
|
|
* Handles incoming messages
|
2015-03-20 14:18:29 +01:00
|
|
|
*/
|
2015-04-07 18:48:58 +02:00
|
|
|
public interface NetworkHandler {
|
2016-05-28 10:22:47 +02:00
|
|
|
int NETWORK_MAGIC_NUMBER = 8;
|
2016-07-29 07:49:53 +02:00
|
|
|
int HEADER_SIZE = 24;
|
2016-06-01 17:38:49 +02:00
|
|
|
int MAX_PAYLOAD_SIZE = 1600003;
|
2016-07-29 07:49:53 +02:00
|
|
|
int MAX_MESSAGE_SIZE = HEADER_SIZE + MAX_PAYLOAD_SIZE;
|
2016-05-28 10:22:47 +02:00
|
|
|
|
2015-09-24 08:09:20 +02:00
|
|
|
/**
|
|
|
|
* Connects to the trusted host, fetches and offers new messages and disconnects afterwards.
|
2015-10-29 12:34:29 +01:00
|
|
|
* <p>
|
|
|
|
* An implementation should disconnect if either the timeout is reached or the returned thread is interrupted.
|
|
|
|
* </p>
|
2015-09-24 08:09:20 +02:00
|
|
|
*/
|
2016-09-12 08:18:30 +02:00
|
|
|
Future<?> synchronize(InetAddress server, int port, long timeoutInSeconds);
|
2015-12-02 17:45:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a custom message to a specific node (that should implement handling for this message type) and returns
|
|
|
|
* the response, which in turn is expected to be a {@link CustomMessage}.
|
|
|
|
*
|
|
|
|
* @param server the node's address
|
|
|
|
* @param port the node's port
|
|
|
|
* @param request the request
|
|
|
|
* @return the response
|
|
|
|
*/
|
|
|
|
CustomMessage send(InetAddress server, int port, CustomMessage request);
|
2015-09-24 08:09:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Start a full network node, accepting incoming connections and relaying objects.
|
|
|
|
*/
|
2016-09-12 08:18:30 +02:00
|
|
|
void start();
|
2015-04-07 18:48:58 +02:00
|
|
|
|
2015-09-24 08:09:20 +02:00
|
|
|
/**
|
|
|
|
* Stop the full network node.
|
|
|
|
*/
|
2015-04-17 13:01:46 +02:00
|
|
|
void stop();
|
2015-04-07 18:48:58 +02:00
|
|
|
|
2015-09-24 08:09:20 +02:00
|
|
|
/**
|
|
|
|
* Offer new objects to up to 8 random nodes.
|
|
|
|
*/
|
2015-05-01 17:14:43 +02:00
|
|
|
void offer(InventoryVector iv);
|
2015-04-07 18:48:58 +02:00
|
|
|
|
2016-06-01 17:38:49 +02:00
|
|
|
/**
|
|
|
|
* Request each of those objects from a node that knows of the requested object.
|
|
|
|
*
|
|
|
|
* @param inventoryVectors of the objects to be requested
|
|
|
|
*/
|
|
|
|
void request(Collection<InventoryVector> inventoryVectors);
|
|
|
|
|
2015-06-16 06:41:59 +02:00
|
|
|
Property getNetworkStatus();
|
|
|
|
|
2015-08-05 19:52:18 +02:00
|
|
|
boolean isRunning();
|
|
|
|
|
2015-04-07 18:48:58 +02:00
|
|
|
interface MessageListener {
|
2015-05-23 10:27:05 +02:00
|
|
|
void receive(ObjectMessage object) throws IOException;
|
2015-04-07 18:48:58 +02:00
|
|
|
}
|
2015-03-20 14:18:29 +01:00
|
|
|
}
|