// Path: Client/src/main/java/Util/Config.java
// public class Config {
// 
//     public static String USER_NAME = "Client1";
//     public static String USER_PASS = "Pass1";
// 
//     public static int MESSAGE_OUTGOING = 200;
//     public static int MESSAGE_OUTGOING_RESPONSE = 201;
//     public static int MESSAGE_MAX_TRIALS = 4;
//     public static int MESSAGE_ACK = 900;
//     public static int MESSAGE_REQUEST_KEYSET = 301;
//     public static int MESSAGE_REQUEST_BLOCK = 302;
//     public static int MESSAGE_TIMEOUT = 2500;
// 
//     public static int MESSAGE_RESPONSE_INVALIDKEY = 401;
//     public static int MESSAGE_RESPONSE_INVALIDHASH = 402;
//     public static int MESSAGE_RESPONSE_VALID = 403;
// 
//     public static int MESSAGE_SERVER_TEST = 999;
// 
//     public static int UPLOAD_EXPIRATION_TIME = 100000;
//     public static int BLOCKCHAIN_BATCH_TIMEOUT = 10000;
//     public static int BLOCKCHAIN_BATCH_PERIOD = 8000;
//     public static int TRANSACTION_VALIDATION_TIMEOUT = 5000;
//     public static int BLOCK_CREATION_TIMEOUT = 300000;
// 
//     public static String KEY_SPLITTER = "////";
// 
//     public static String DB_TABLE_NAME="blockchain";
// 
//     public static String SERVER_ADDRESS = "46.101.245.232";
//     public static int SERVER_PORT = 4141;
//     public static int SERVER_TIMEOUT = 5000;
// 
//     public static int CLIENT_HEARTBEAT_PORT = 4141;
//     public static int CLIENT_SERVER_PORT = 4142;
//     public static String CLIENT_MESSAGE_SPLITTER = "////";
//     public static String CLIENT_MESSAGE_PEERSIZE = "X";
// 
//     public static int HEARTBEAT_FLAG_CLIENT = 101;
//     public static int HEARTBEAT_FLAG_SERVER = 100;
//     public static int HEARTBEAT_ACK = 102;
//     public static int HEARTBEAT_PERIOD = 5000;
//     public static int HEARTBEAT_TIMEOUT = 10000;
//     public static int HEARTBEAT_MAX_TRIALS = 3;
// 
//     public static int FLAG_BROADCAST_TRANSACTION = 1;
//     public static int FLAG_BROADCAST_HASH = 2;
//     public static int FLAG_BLOCKCHAIN_INVALID = 4;
// 
//     public static String UPLOAD_BUCKETNAME = "crypdist-trial-bucket-mfs";
// 
// 
// 
//     public static String PRIVATE_KEY = "";
// 
// }


import Util.Config;
import org.apache.log4j.Logger;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Map;
import java.util.TimerTask;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
package P2P;



/**
 * P2P.HeartBeatTask class for both Server and P2P.Client for refreshing their peerList by sending heartbeats.
 *
 * Created by od on 17.02.2017.
 */


public class HeartBeatTask extends TimerTask {

    private static Logger log = Client.log;

    private class SendHeartBeat implements Callable<Peer> {
        private Peer peer;

        public SendHeartBeat(Peer peer) {
            this.peer = peer;
        }

        public Peer call() {
            Socket clientSocket = null;
            try {
                clientSocket = new Socket(peer.getAddress(),peer.getPeerHeartBeatPort());
                DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
                DataInputStream in = new DataInputStream(clientSocket.getInputStream());

// Path: Client/src/main/java/Util/Config.java
// public class Config {
// 
//     public static String USER_NAME = "Client1";
//     public static String USER_PASS = "Pass1";
// 
//     public static int MESSAGE_OUTGOING = 200;
//     public static int MESSAGE_OUTGOING_RESPONSE = 201;
//     public static int MESSAGE_MAX_TRIALS = 4;
//     public static int MESSAGE_ACK = 900;
//     public static int MESSAGE_REQUEST_KEYSET = 301;
//     public static int MESSAGE_REQUEST_BLOCK = 302;
//     public static int MESSAGE_TIMEOUT = 2500;
// 
//     public static int MESSAGE_RESPONSE_INVALIDKEY = 401;
//     public static int MESSAGE_RESPONSE_INVALIDHASH = 402;
//     public static int MESSAGE_RESPONSE_VALID = 403;
// 
//     public static int MESSAGE_SERVER_TEST = 999;
// 
//     public static int UPLOAD_EXPIRATION_TIME = 100000;
//     public static int BLOCKCHAIN_BATCH_TIMEOUT = 10000;
//     public static int BLOCKCHAIN_BATCH_PERIOD = 8000;
//     public static int TRANSACTION_VALIDATION_TIMEOUT = 5000;
//     public static int BLOCK_CREATION_TIMEOUT = 300000;
// 
//     public static String KEY_SPLITTER = "////";
// 
//     public static String DB_TABLE_NAME="blockchain";
// 
//     public static String SERVER_ADDRESS = "46.101.245.232";
//     public static int SERVER_PORT = 4141;
//     public static int SERVER_TIMEOUT = 5000;
// 
//     public static int CLIENT_HEARTBEAT_PORT = 4141;
//     public static int CLIENT_SERVER_PORT = 4142;
//     public static String CLIENT_MESSAGE_SPLITTER = "////";
//     public static String CLIENT_MESSAGE_PEERSIZE = "X";
// 
//     public static int HEARTBEAT_FLAG_CLIENT = 101;
//     public static int HEARTBEAT_FLAG_SERVER = 100;
//     public static int HEARTBEAT_ACK = 102;
//     public static int HEARTBEAT_PERIOD = 5000;
//     public static int HEARTBEAT_TIMEOUT = 10000;
//     public static int HEARTBEAT_MAX_TRIALS = 3;
// 
//     public static int FLAG_BROADCAST_TRANSACTION = 1;
//     public static int FLAG_BROADCAST_HASH = 2;
//     public static int FLAG_BLOCKCHAIN_INVALID = 4;
// 
//     public static String UPLOAD_BUCKETNAME = "crypdist-trial-bucket-mfs";
// 
// 
// 
//     public static String PRIVATE_KEY = "";
// 
// }


// Path: Client/src/main/java/P2P/HeartBeatTask.java
import Util.Config;
import org.apache.log4j.Logger;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Map;
import java.util.TimerTask;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

package P2P;



/**
 * P2P.HeartBeatTask class for both Server and P2P.Client for refreshing their peerList by sending heartbeats.
 *
 * Created by od on 17.02.2017.
 */


public class HeartBeatTask extends TimerTask {

    private static Logger log = Client.log;

    private class SendHeartBeat implements Callable<Peer> {
        private Peer peer;

        public SendHeartBeat(Peer peer) {
            this.peer = peer;
        }

        public Peer call() {
            Socket clientSocket = null;
            try {
                clientSocket = new Socket(peer.getAddress(),peer.getPeerHeartBeatPort());
                DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
                DataInputStream in = new DataInputStream(clientSocket.getInputStream());

                out.writeInt(Config.HEARTBEAT_FLAG_CLIENT);  //0 for heartbeats