Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. package server;
  2.  
  3. import java.net.*;
  4. import client.*;
  5. import server.data.*;
  6. import server.database.*;
  7. import server.mining.*;
  8. import java.io.*;
  9. import server.utility.*;
  10. import server.keyboardinput.*;
  11. import java.util.*;
  12.  
  13. class ServerOneClient extends Thread {
  14. private Socket socket; // porta per il canale di connessione con il client
  15. private ClosedPatternArchive archive; // riferimento all'archivio di closed
  16. // pattern correttemente scoperti
  17. private BufferedReader in;
  18. private PrintWriter pout;
  19.  
  20. public ServerOneClient(Socket s) throws IOException {
  21. socket = s;
  22. in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  23. pout = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
  24. start(); // avvia il thread, invoca run()
  25. }
  26.  
  27. public void run() {
  28. String choice = ""; // Operazione scelta sul client
  29. String table_name = "";
  30. float minSup = 0F;
  31. float eps = 0F;
  32. String path = "";
  33.  
  34. try {
  35. // Ricevo la scelta effettuata sul menu
  36. choice = in.readLine();
  37. System.out.println("Ricevuto da Client: " + choice);
  38. // Se si richiede la scoperta di pattern chiusi allora si richiedono
  39. // gli altri valori
  40. if (choice.equals("1")) {
  41. // Invio la stringa e ricevo il nome della tabella
  42. pout.println("Inserire nome tabella");
  43. table_name = in.readLine();
  44. System.out.println("Tabel_name: " + table_name);
  45.  
  46. // Il server richiede il valore di minSup
  47. pout.println("Inserire il valore di minSup");
  48. minSup = Float.parseFloat(in.readLine());
  49. do {
  50. if (minSup <= 0 || minSup > 1) {
  51. pout.println("Inserire nuovamente il valore di minSup");
  52. minSup = Float.parseFloat(in.readLine());
  53. }
  54. } while (minSup <= 0 || minSup > 1);
  55. System.out.println("minSup: " + minSup);
  56.  
  57. // Il Server richiede il valore di eps e lo riceve dal client
  58. pout.println("Inserire il valore di eps");
  59. eps = Float.parseFloat(in.readLine());
  60. do {
  61. if (eps <= 0 || eps > 1) {
  62. pout.println("Inserire nuovamente il valore di eps");
  63. eps = Float.parseFloat(in.readLine());
  64. }
  65. } while (eps <= 0 || eps > 1);
  66. System.out.println("esp: " + eps);
  67.  
  68. // Il Server richiede il nome del file più estensione su cui
  69. // salvare (Si assume di salvare sul workspace)
  70. pout.println("Inserire il nome del file su cui salvare");
  71. path = in.readLine();
  72. System.out.println("path: " + path);
  73.  
  74. // Il Server fornisce il servizio di scoperta di pattern chiusi
  75. try {
  76. if (DBAccess.initConnection() == false)
  77. throw new DatabaseConnectionException();
  78. else {
  79. Data data = new Data(table_name); // Carico la matrice
  80.  
  81. // Scoperta patten frequenti
  82. LinkList outputFP = FrequentPatternMiner.frequentPatternDiscovery(data, minSup);
  83.  
  84. // Copio i pattern frequenti in una LinkedList che mi
  85. // servirà per la scoperta di pattern chiusi
  86. LinkedList<FrequentPattern> out = new LinkedList<FrequentPattern>();
  87. Puntatore p = outputFP.firstList();
  88. int i = 1;
  89. while (!outputFP.endList(p)) {
  90. FrequentPattern FP = (FrequentPattern) outputFP.readList(p);
  91. out.add(FP); // copia nella lista
  92. // System.out.println(i + ":" + FP); // stampa di
  93. // pattern
  94. // frequenti
  95. p = outputFP.succ(p);
  96. i++;
  97. }
  98.  
  99. Collections.sort(out);// ordinamento della lista di
  100. // pattern
  101. // frequenti
  102.  
  103. // calcolo dei pattern chiusi
  104. ClosedPatternArchive archivio = ClosedPatternMiner.closedPatternDiscovery(out, eps);
  105. // System.out.print(archivio); // stampa pattern chiusi
  106.  
  107. // if (archivio == null)
  108. // throw new ServerException();
  109.  
  110. // salvataggio
  111. try {
  112. ClosedPatternArchive.salvataggio(archivio, path);
  113. } catch (IOException e) {
  114. System.err.println("Errore nel salvataggio");
  115. }
  116. pout.println("END");
  117. }
  118. } catch (DatabaseConnectionException e) {
  119. System.err.println("Errore caricamento driver");
  120. } catch (EmptySetException e) {
  121. System.err.println("Matrice vuota");
  122. }
  123.  
  124. } else if (choice.equals("2")) {
  125. try {
  126. pout.println("Inserire il nome del file con estensione");
  127. path = in.readLine();
  128. System.out.println("Ricevo dal client il nome del file: " + path);
  129.  
  130. File file = new File(path);
  131. if (!(file.exists())) {
  132. pout.println("File non trovato");
  133. }
  134. ClosedPatternArchive ar = ClosedPatternArchive.caricamento(path);
  135. pout.println("END");
  136. } catch (IOException exc) {
  137. System.err.println("Errore nel caricamento, archivio non trovato");
  138. } catch (Exception e) {
  139. e.getMessage();
  140. }
  141.  
  142. }
  143.  
  144. } catch (IOException exc) {
  145. System.err.println("IOException");
  146. } finally {
  147. try {
  148. socket.close();
  149. } catch (IOException exc) {
  150. System.err.println("Socket not closed");
  151.  
  152. }
  153. }
  154.  
  155. }
  156. }
  157.  
  158. public class MultiServer extends Thread {
  159. static final int PORT = 8080;
  160.  
  161. public MultiServer() {
  162. start();
  163. System.out.println("Server Avviato");
  164.  
  165. }
  166.  
  167. public void run() {
  168. ServerSocket s = null;
  169. try {
  170. s = new ServerSocket(PORT);
  171. while (true) {
  172. Socket socket = s.accept();
  173. try {
  174. new ServerOneClient(socket);
  175. } catch (IOException e) {
  176. System.err.println("Fallito chiudo");
  177. socket.close();
  178. }
  179. }
  180. } catch (IOException e) {
  181. System.err.println("IOException run");
  182. } finally {
  183. try {
  184. s.close();
  185. } catch (IOException e) {
  186. System.err.println("Chiusura finale");
  187. }
  188. }
  189. }
  190.  
  191. public static void main(String[] args) {
  192. MultiServer ml = new MultiServer();
  193.  
  194. }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement