Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package server;
- import java.net.*;
- import client.*;
- import server.data.*;
- import server.database.*;
- import server.mining.*;
- import java.io.*;
- import server.utility.*;
- import server.keyboardinput.*;
- import java.util.*;
- class ServerOneClient extends Thread {
- private Socket socket; // porta per il canale di connessione con il client
- private ClosedPatternArchive archive; // riferimento all'archivio di closed
- // pattern correttemente scoperti
- private BufferedReader in;
- private PrintWriter pout;
- public ServerOneClient(Socket s) throws IOException {
- socket = s;
- in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- pout = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
- start(); // avvia il thread, invoca run()
- }
- public void run() {
- String choice = ""; // Operazione scelta sul client
- String table_name = "";
- float minSup = 0F;
- float eps = 0F;
- String path = "";
- try {
- // Ricevo la scelta effettuata sul menu
- choice = in.readLine();
- System.out.println("Ricevuto da Client: " + choice);
- // Se si richiede la scoperta di pattern chiusi allora si richiedono
- // gli altri valori
- if (choice.equals("1")) {
- // Invio la stringa e ricevo il nome della tabella
- pout.println("Inserire nome tabella");
- table_name = in.readLine();
- System.out.println("Tabel_name: " + table_name);
- // Il server richiede il valore di minSup
- pout.println("Inserire il valore di minSup");
- minSup = Float.parseFloat(in.readLine());
- do {
- if (minSup <= 0 || minSup > 1) {
- pout.println("Inserire nuovamente il valore di minSup");
- minSup = Float.parseFloat(in.readLine());
- }
- } while (minSup <= 0 || minSup > 1);
- System.out.println("minSup: " + minSup);
- // Il Server richiede il valore di eps e lo riceve dal client
- pout.println("Inserire il valore di eps");
- eps = Float.parseFloat(in.readLine());
- do {
- if (eps <= 0 || eps > 1) {
- pout.println("Inserire nuovamente il valore di eps");
- eps = Float.parseFloat(in.readLine());
- }
- } while (eps <= 0 || eps > 1);
- System.out.println("esp: " + eps);
- // Il Server richiede il nome del file più estensione su cui
- // salvare (Si assume di salvare sul workspace)
- pout.println("Inserire il nome del file su cui salvare");
- path = in.readLine();
- System.out.println("path: " + path);
- // Il Server fornisce il servizio di scoperta di pattern chiusi
- try {
- if (DBAccess.initConnection() == false)
- throw new DatabaseConnectionException();
- else {
- Data data = new Data(table_name); // Carico la matrice
- // Scoperta patten frequenti
- LinkList outputFP = FrequentPatternMiner.frequentPatternDiscovery(data, minSup);
- // Copio i pattern frequenti in una LinkedList che mi
- // servirà per la scoperta di pattern chiusi
- LinkedList<FrequentPattern> out = new LinkedList<FrequentPattern>();
- Puntatore p = outputFP.firstList();
- int i = 1;
- while (!outputFP.endList(p)) {
- FrequentPattern FP = (FrequentPattern) outputFP.readList(p);
- out.add(FP); // copia nella lista
- // System.out.println(i + ":" + FP); // stampa di
- // pattern
- // frequenti
- p = outputFP.succ(p);
- i++;
- }
- Collections.sort(out);// ordinamento della lista di
- // pattern
- // frequenti
- // calcolo dei pattern chiusi
- ClosedPatternArchive archivio = ClosedPatternMiner.closedPatternDiscovery(out, eps);
- // System.out.print(archivio); // stampa pattern chiusi
- // if (archivio == null)
- // throw new ServerException();
- // salvataggio
- try {
- ClosedPatternArchive.salvataggio(archivio, path);
- } catch (IOException e) {
- System.err.println("Errore nel salvataggio");
- }
- pout.println("END");
- }
- } catch (DatabaseConnectionException e) {
- System.err.println("Errore caricamento driver");
- } catch (EmptySetException e) {
- System.err.println("Matrice vuota");
- }
- } else if (choice.equals("2")) {
- try {
- pout.println("Inserire il nome del file con estensione");
- path = in.readLine();
- System.out.println("Ricevo dal client il nome del file: " + path);
- File file = new File(path);
- if (!(file.exists())) {
- pout.println("File non trovato");
- }
- ClosedPatternArchive ar = ClosedPatternArchive.caricamento(path);
- pout.println("END");
- } catch (IOException exc) {
- System.err.println("Errore nel caricamento, archivio non trovato");
- } catch (Exception e) {
- e.getMessage();
- }
- }
- } catch (IOException exc) {
- System.err.println("IOException");
- } finally {
- try {
- socket.close();
- } catch (IOException exc) {
- System.err.println("Socket not closed");
- }
- }
- }
- }
- public class MultiServer extends Thread {
- static final int PORT = 8080;
- public MultiServer() {
- start();
- System.out.println("Server Avviato");
- }
- public void run() {
- ServerSocket s = null;
- try {
- s = new ServerSocket(PORT);
- while (true) {
- Socket socket = s.accept();
- try {
- new ServerOneClient(socket);
- } catch (IOException e) {
- System.err.println("Fallito chiudo");
- socket.close();
- }
- }
- } catch (IOException e) {
- System.err.println("IOException run");
- } finally {
- try {
- s.close();
- } catch (IOException e) {
- System.err.println("Chiusura finale");
- }
- }
- }
- public static void main(String[] args) {
- MultiServer ml = new MultiServer();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement