]> matita.cs.unibo.it Git - logicplayer.git/commitdiff
New version
authorClaudio Sacerdoti Coen <claudio@zenone.casamia.org>
Tue, 9 Dec 2014 14:12:12 +0000 (15:12 +0100)
committerClaudio Sacerdoti Coen <claudio@zenone.casamia.org>
Tue, 9 Dec 2014 14:12:12 +0000 (15:12 +0100)
server/com/company/DirectoryWhatcer.java
server/com/company/dbConnect.java
server/com/company/global.java
server/com/company/guestServer.java
server/com/company/httpget.java
server/com/company/personalServer.java
server/com/company/superServer.java
server/com/company/xmlOperation.java

index 1d219b2367c7a0bba8bd3d0afc8bcdd2ca55ba53..630c9083f2dc77dbd9d54d43fa669951c90083d3 100644 (file)
@@ -39,9 +39,8 @@ class DirectoryWatcher implements Runnable {
                 xmlOperation es=new xmlOperation();
                 File car=new File(global.listaEsercizi);
                 if (car.exists()) {
-                    dbConnect db=new dbConnect();
                     try {
-                        ArrayList<String> esercizi=suString.stringToArrayList(db.dbQuery("4"));
+                        ArrayList<String> esercizi=suString.stringToArrayList(global.request.dbQuery("4"));
                         boolean add=true;
                         for(String esercizio : esercizi)
                         {
@@ -66,9 +65,8 @@ class DirectoryWatcher implements Runnable {
                 }
             }
         }
-        dbConnect db=new dbConnect();
         try {
-            String[] esercizi=suString.stringToVectorString(db.dbQuery("4"));
+            String[] esercizi=suString.stringToVectorString(global.request.dbQuery("4"));
             boolean delete=true;
             for(String esercizio : esercizi)
             {
index e7c7e20224ce72bb98cd83ffc7a5874e6cd44be1..c89c18e225b8ba44e21c25c72f33e13e38df8353 100644 (file)
@@ -18,7 +18,21 @@ public class dbConnect
     private static Connection db;
     private static Statement stmt;
     dbConnect()
-    {}
+    {
+        try {
+            Class.forName("com.mysql.jdbc.Driver").newInstance();
+            db = DriverManager.getConnection(global.dbConstant.dbLink + global.dbConstant.dbName, global.dbConstant.dbAdmin, global.dbConstant.dbPassword);
+            stmt = db.createStatement();
+        } catch (ClassNotFoundException e) {
+            e.printStackTrace();
+        } catch (SQLException e) {
+            e.printStackTrace();
+        } catch (InstantiationException e) {
+            e.printStackTrace();
+        } catch (IllegalAccessException e) {
+            e.printStackTrace();
+        }
+    }
 
 
     public static Long time()
@@ -319,9 +333,6 @@ public class dbConnect
     }
 
     public String dbQuery(String input) throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException, IOException, NoSuchAlgorithmException {
-        Class.forName("com.mysql.jdbc.Driver").newInstance();
-        db = DriverManager.getConnection(global.dbConstant.dbLink+global.dbConstant.dbName, global.dbConstant.dbAdmin, global.dbConstant.dbPassword);
-        stmt = db.createStatement();
         deletetmp();
         if (input.charAt(0) == '0')
             return splitUsrPass(input.substring(2), 0);
index 304f97039a9695c4e5eb0914755a1a802f25cd7f..7c82d27c0c929f111c492715c7bf5168440640d7 100644 (file)
@@ -2,6 +2,10 @@ package com.company;
 
 
 public class global {
+    //variabili globali per utilizzare la dbConnect
+    public static dbConnect request;
+    //*********************************************
+
     public static final String listaEsercizi = "/projects/logicplayer/server/com/company/esercizi.xml";
     public static final String locationEsercizi = "/projects/logicplayer/public_html/esercizi/";
     public static String home = "/projects/logicplayer/server/com/company/";
@@ -38,5 +42,4 @@ public class global {
         public static final String clEsercizio = "esercizio";
         public static final String clVoto = "voto";
     }
-    
 }
index 1fb6ce947d8f43c5a8d9aff252772923023d41ed..ee7f79ed4c8c8eecca8c7a217ef00680fa6342ed 100644 (file)
@@ -24,24 +24,14 @@ class guestServer implements Runnable
                        BufferedReader br = new BufferedReader(isr);
                        String requestMessage = br.readLine();
                        System.out.println("ho ricevuto: "+requestMessage);
-                       /*System.out.println("Message received from client is "+returnMessage);
-                       dbConnect request=new dbConnect();
-                       String returnQuery=request.dbQuery(returnMessage);
-                       OutputStream os = socket.getOutputStream();
-                       OutputStreamWriter osw = new OutputStreamWriter(os);
-                       BufferedWriter bw = new BufferedWriter(osw);
-                       System.out.println(returnQuery);
-                       bw.write(returnQuery+"\n");
-                       bw.flush();*/
                        if(requestMessage!=null)
                        {
-                               dbConnect request = new dbConnect();
                                String returnQuery;
                                if (    requestMessage.charAt(0) == '0' ||
                                                requestMessage.charAt(0) == '1' ||
                                                requestMessage.charAt(0) == '2' ||
                                                (requestMessage.charAt(0) == 'a' && requestMessage.charAt(1) == '0')) {
-                                       returnQuery = request.dbQuery(requestMessage);
+                                       returnQuery = global.request.dbQuery(requestMessage);
                                        OutputStream os = socket.getOutputStream();
                                        OutputStreamWriter osw = new OutputStreamWriter(os);
                                        BufferedWriter bw = new BufferedWriter(osw);
@@ -51,7 +41,7 @@ class guestServer implements Runnable
                                }
                                else
                                {
-                                       returnQuery = request.dbQuery("5/" + requestMessage);
+                                       returnQuery =global.request.dbQuery("5/" + requestMessage);
                                        if (returnQuery.contains("request-login") || returnQuery.contains("request-error"))
                                        {
                                                OutputStream os = socket.getOutputStream();
index 079cc019d6e9d3ddca729145608eb30a91e137ce..fa3b43130754c3c07eea00e0c589440cd4e34a7d 100644 (file)
@@ -33,44 +33,36 @@ public class httpget implements Runnable {
                        server.createContext("/test", new MyHandler());
                        server.setExecutor(null); // creates a default executor
                        server.start();
-               }catch (Exception e)
-               {
+               } catch (IOException e) {
                        e.printStackTrace();
                }
-    }
+       }
 
     static class MyHandler implements HttpHandler 
     {
         public void handle(HttpExchange t) throws IOException
         {
-                       
-            /*String response = "This is the response";
-            System.out.println("mannaggia santa");
-            t.sendResponseHeaders(200, response.length());
-            OutputStream os = t.getResponseBody();
-            os.write(response.getBytes());*/
                        URI req=t.getRequestURI();
             String query=req.getQuery();
             Map<String, String> result=splitQuery(query);
             String id=result.get("id");
-            //System.out.println(id);
             try
             {
-                               dbConnect request=new dbConnect();
-                               String returnQuery=request.dbQuery("3"+id);
+
+                               String returnQuery=global.request.dbQuery("3"+id);
                                t.sendResponseHeaders(200, returnQuery.length());
                                OutputStream os = t.getResponseBody();
                                os.write(returnQuery.getBytes());
                                os.close();
-                       }
-                       catch(SQLException e)
-                       {}
-                       catch(ClassNotFoundException c)
-                       {}
-                       catch(IllegalAccessException a)
-                       {}
-                       catch(InstantiationException i)
-                       {} catch (NoSuchAlgorithmException e) {
+                       } catch (ClassNotFoundException e) {
+                               e.printStackTrace();
+                       } catch (SQLException e) {
+                               e.printStackTrace();
+                       } catch (InstantiationException e) {
+                               e.printStackTrace();
+                       } catch (NoSuchAlgorithmException e) {
+                               e.printStackTrace();
+                       } catch (IllegalAccessException e) {
                                e.printStackTrace();
                        }
                }
index 1d338eb618f6bc7ff8f22f98bc5d98401b65f7df..5dfccf0111f73ffb8c0a5fc91a5ecd7339edaad4 100644 (file)
@@ -27,15 +27,14 @@ class personalServer implements Runnable
        {
                try
                {
-            dbConnect request=new dbConnect();
-            String returnQuery=request.dbQuery(requestMessage);
+            String returnQuery=global.request.dbQuery(requestMessage);
             OutputStream os = socket.getOutputStream();
             OutputStreamWriter osw = new OutputStreamWriter(os);
             BufferedWriter bw = new BufferedWriter(osw);
             bw.write(returnQuery+"\n");
             bw.flush();
             System.out.println("ho inviato: "+returnQuery);
-            request.dbQuery("6/"+key);
+                       global.request.dbQuery("6/"+key);
             socket.close();
                }
                catch (Exception e) 
@@ -43,8 +42,6 @@ class personalServer implements Runnable
                        try
                        {
                                e.printStackTrace();
-                               dbConnect request=new dbConnect();
-                               request.dbQuery("7/"+key);
                        }
                        catch (Exception k)
                        {
index bac54fda29d9966e64a24eea8bb417116eea775d..0889ed440d6a1ff2e938912432a8264528edb91a 100644 (file)
@@ -11,12 +11,11 @@ import java.nio.file.Path;
 
 class superServer
 {
-       public static void main(String[] args)
-       {
-               String ksName = global.home+global.keyStoreName;
+       public static void main(String[] args) {
+               String ksName = global.home + global.keyStoreName;
                char ksPass[] = global.keyStorePass.toCharArray();
                char ctPass[] = global.keyStorePass.toCharArray();
-
+               global.request=new dbConnect();
                //thread di controllo directory esercizi
                Path pathToWatch = FileSystems.getDefault().getPath(global.locationEsercizi);
                DirectoryWatcher dirWatcher = new DirectoryWatcher(pathToWatch);
@@ -24,35 +23,47 @@ class superServer
                dirWatcherThread.start();
 
                //thread che si occupa del lato http
-               httpget regServer=new httpget();
-               Thread httpServer=new Thread(regServer);
+               httpget regServer = new httpget();
+               Thread httpServer = new Thread(regServer);
                httpServer.start();
-
-               while(true)
-               {
-                       try
-                       {
-                               KeyStore ks = KeyStore.getInstance("JKS");
-                               ks.load(new FileInputStream(ksName), ksPass);
-                               KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
-                               kmf.init(ks, ctPass);
-                               SSLContext sc = SSLContext.getInstance("TLS");
-                               sc.init(kmf.getKeyManagers(), null, null);
-                               SSLServerSocketFactory ssf = sc.getServerSocketFactory();
-                               SSLServerSocket s= (SSLServerSocket) ssf.createServerSocket(global.portaServer);
-                               System.out.println("Server started:");
-                               while(true)
-                               {
+               SSLServerSocket s = null;
+               try {
+                       KeyStore ks = KeyStore.getInstance("JKS");
+                       ks.load(new FileInputStream(ksName), ksPass);
+                       KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
+                       kmf.init(ks, ctPass);
+                       SSLContext sc = SSLContext.getInstance("TLS");
+                       sc.init(kmf.getKeyManagers(), null, null);
+                       SSLServerSocketFactory ssf = sc.getServerSocketFactory();
+                       s = (SSLServerSocket) ssf.createServerSocket(global.portaServer);
+               } catch (CertificateException e) {
+                       e.printStackTrace();
+               } catch (UnrecoverableKeyException e) {
+                       e.printStackTrace();
+               } catch (NoSuchAlgorithmException e) {
+                       e.printStackTrace();
+               } catch (KeyStoreException e) {
+                       e.printStackTrace();
+               } catch (KeyManagementException e) {
+                       e.printStackTrace();
+               } catch (FileNotFoundException e) {
+                       e.printStackTrace();
+               } catch (IOException e) {
+                       e.printStackTrace();
+               }
+               if (s != null) {
+                       System.out.println("Server started:");
+                       while (true) {
+                               try {
                                        SSLSocket c = (SSLSocket) s.accept();
-                                       guestServer guest=new guestServer(c);
-                                       Thread t=new Thread(guest);
+                                       guestServer guest = new guestServer(c);
+                                       Thread t = new Thread(guest);
                                        t.start();
+                               } catch (IOException e) {
+                                       e.printStackTrace();
                                }
                        }
-                       catch (Exception e)
-                       {
-                               e.printStackTrace();
-                       }
                }
+               System.out.println("Server die!");
        }
 }
index f15cd796f6d1e0da27837a8100a39edbbaae8559..7a07f93fd66a713d5294b799d490f22ed696d3a6 100644 (file)
@@ -105,16 +105,17 @@ public class xmlOperation {
 
             Document doc = (Document) builder.build(xmlFile);
             Element rootNode = doc.getRootElement();
-
-            for(Element esercizio:rootNode.getChildren())
+            Element tmp = null;
+            for(Element esercizio : rootNode.getChildren())
             {
                 if(esercizio.getText().contentEquals(name))
                 {
-                    System.out.println("trovato");
-                    rootNode.removeContent(esercizio);
+                    tmp=esercizio;
+                    break;
                 }
             }
-
+            if(tmp!=null)
+                rootNode.removeContent(tmp);
             XMLOutputter xmlOutput = new XMLOutputter();
 
             // display nice nice