Java

DX Cluster in Java

Condivido il codice sorgente in Java per la realizzazione di un DX cluster client (che ho chiamato DX Fighter cluster), con annessa serie di video in cui spiego il l’implementazione. Guarda anche la versione scritta in Python 3.

Playlist completa

La classe ClusterHost

package model;

/**
 * Classe per l'host
 * @author Lorenzo IZ0KBA
 *
 */
public class ClusterHost {
	
	private String name;
	private final String host;
	private final int port;
	
	public ClusterHost(String host, int port) {
		this.host = host;
		this.port = port;
	}
	
	/**
	 * Restituisce il valore dell'hostname
	 * @return
	 */
	public String getHost() {
		return host;
	}
	
	/**
	 * Restituisce il valore della porta
	 * @return
	 */
	public int getPort() {
		return port;
	}
	
	public String toString() {
		return host + " port: " + port;
	}

}

La classe DxClient

package model;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Observable;

public class DxClient extends Observable {

	private Entities entities;
	private LOTW lotw;
	private ClusterHost host;
	private Socket s = new Socket();
	private BufferedReader s_in;
	private PrintWriter s_out;
	private static final String myCall = "IZ0KBA";

	public DxClient(ClusterHost clusterHost) {
		this.host = clusterHost;
		try {
			this.entities = new Entities();
			this.lotw = new LOTW();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Spot.setEntities(entities);
		Spot.setLotw(lotw);
	}

	public void run() throws IOException {
		System.out.println("Tentativo di connesione a: " + host);
		try {
			s.connect(new InetSocketAddress(host.getHost(), host.getPort()));
			s_out = new PrintWriter(s.getOutputStream(), true);
			s_in = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));
			s_out.println(myCall);
		} catch (final IOException e) {
			System.err.println("Impossibile connettersi all'Host " + host);
		} finally {
			System.out.println("Connessione stabilita!");
		}

		String response;
		while ((response = s_in.readLine()) != null) {
			for (String line : response.split("\n")) {
				if (line.startsWith("DX de ")) {
					Spot spot = new Spot(line);
					if (spot.getFrequency() > 14000) {
						System.out.println(spot);
					} 
				} else {
					System.out.println(line);
				}
			}
		}

	}

	public static void main(String[] args) throws IOException {
		DxClient dxc = new DxClient(new ClusterHost("dxfun.com", 8000));
		dxc.run();
	}

}			

La classe SPOT

package model;

public class Spot {
	
	private static Entities entities;
	private static LOTW lotwClass;
	private String line;
	private final String callsign;
	private final double frequency;
	private final Entita entita;
	private final String[] lotw;

	public Spot(String line) {
		this.line = line;
		int punto = line.indexOf(".");
		int duePunti = line.indexOf(":");
		callsign = line.substring(punto + 2).strip().split(" ")[0];
		frequency = Double.valueOf(line.substring(duePunti + 1, punto + 2).strip());
		entita = entities.getEntities(callsign);
		lotw = lotwClass.getLotw(callsign);
	}
	
	/**
	 * Imposta il riferimento all'oggetto Entities
	 * @param e
	 */
	public static void setEntities(Entities e) {
		entities = e;
	}
	
	/**
	 * Imposta il riferimento all'oggetto LOTW
	 * @param lotw
	 */
	public static void setLotw(LOTW lotw) {
		lotwClass = lotw;
	}
	
	/**
	 * Restituisce la frequenza
	 * @return
	 */
	public double getFrequency() {
		return frequency;
	}
	
	@Override
	public String toString() {
		String lotw = this.lotw[0].contains("-") ? "< LOTW <" : "<      <";
		return String.join(" ", line, lotw, entita.name, this.lotw[0]);
	}
	
}

Lascia una risposta

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *