Exemple #1
0
/*___________________________________________________________________________________________________*/

        public void HandleClient(object obj)
        {
            TcpClient client = (TcpClient)obj;

            StreamWriter sWriter = new StreamWriter(client.GetStream(), Encoding.UTF8);
            StreamReader sReader = new StreamReader(client.GetStream(), Encoding.UTF8);

            Boolean bClientConnected = true;

            String[]          dbCred     = sReader.ReadLine().Split('$');
            DatabaseInterface dbInter    = new DatabaseInterface(dbCred[0], dbCred[1], dbCred[2]);
            String            check_auth = dbInter.check().ToString();

            sWriter.WriteLine(check_auth);
            sWriter.Flush();
            /*Connessione al database fallita*/
            if (check_auth.Equals("1"))
            {
                client.Close();
                return;
            }
            /*Connessione al database andata a buon fine*/
            /*Prendiamo i dati dal database*/

            /*Dobbiamo separare i dati contenuti in infoAlbero[] in lista alberi con relativi attributi (Divisi tra
             * vertex ed edge)*/
            /*NomeAlbero#attr1Vertex?tipo:attr2Vertex?tipo#attrEdge?tipo:attrEdge2?tipo$NomeAlbero2#etc*/
            InfoAlbero[] infoAlbero = dbInter.getListaAlberi();
            if (infoAlbero.Length == 0)
            {
                sWriter.WriteLine("Vuoto");
                sWriter.Flush();
            }
            else
            {
                //Console.WriteLine(parametersParser(dbInter.getListaAlberi()));
                sWriter.WriteLine(parametersParser(infoAlbero));
                sWriter.Flush();
            }
            /*Dobbiamo spedire la attribute definitions*/
            Dictionary <String, String> attributeDefinition = dbInter.getAttributeDefinition();

            /*foreach (KeyValuePair<String, String> val in attributeDefinition)
             * {
             *  Console.WriteLine(val);
             * }*/
            //Console.WriteLine(parametersParser(attributeDefinition));
            sWriter.WriteLine(parametersParser(attributeDefinition));
            sWriter.Flush();
            String response = "";
            /*_____Inizio scelta funzioni in base all'operazione scelta__________________________________________________*/
            NetworkStream   netStr = client.GetStream();
            TreeLogicEngine engine = new TreeLogicEngine();

            while (bClientConnected)
            {
                response = sReader.ReadLine();
                if (response.Equals("Return"))
                {
                    continue;
                }

                switch (response)
                {
                /******************IMPORTA FILE*****************************/
                case "Importa":
                    sWriter.WriteLine("Importa");
                    sWriter.Flush();
                    /*Leggiamo la dimensione del file da leggere*/
                    response = sReader.ReadLine();
                    if (response.Equals("Return"))
                    {
                        break;
                    }
                    int length = int.Parse(response);
                    //Console.WriteLine(length);
                    byte[] file = new byte[length];
                    netStr.Read(file, 0, length);
                    //Console.WriteLine(file.Length);
                    MemoryStream stream = new MemoryStream(file);


                    /*  public static int import(MemoryStream XmlStream, DatabaseInterface   DbConnection);*/

                    sWriter.WriteLine(FileEngine.import(stream, dbInter).ToString());
                    sWriter.Flush();

                    break;

                /******************ESPORTA FILE*****************************/
                case "Esporta":
                    sWriter.WriteLine("Esporta");
                    sWriter.Flush();
                    response = sReader.ReadLine();
                    if (response.Equals("Return"))
                    {
                        break;
                    }
                    /*public static MemoryStream export(String treeName,  DatabaseInterface DbConnection);*/
                    //byte[] fileXml = File.ReadAllBytes("Tree.xml");
                    byte[] fileXml = FileEngine.export(response, dbInter).ToArray();
                    sWriter.WriteLine(fileXml.Length.ToString());
                    sWriter.Flush();
                    netStr.Write(fileXml, 0, fileXml.Length);
                    break;

                /******************CREA ALBERO*****************************/
                case "Crea":
                    sWriter.WriteLine("Crea");
                    sWriter.Flush();
                    String[] NomTipSplitDepth = new String[4];
                    String[] SplitDept        = new String[2];

                    //Nome$Tipo$Splitsize$Depth
                    response = sReader.ReadLine();
                    if (response.Equals("Return"))
                    {
                        break;
                    }
                    NomTipSplitDepth = response.Split('$');
                    SplitDept[0]     = NomTipSplitDepth[2];
                    SplitDept[1]     = NomTipSplitDepth[3];

                    //NomeAttrVertex1#tipoAttr:valore$NomeAttrVertex2#tipoAttr2:valore2$etc
                    response = sReader.ReadLine();
                    if (response.Equals("Return"))
                    {
                        break;
                    }

                    Dictionary <String, String[]> vertexAttrList = parametersParser(response);
                    //NomeAttrEdge1#tipoAttr:valore$NomeAttrEdge2#tipoAttr2:valore2$etc
                    response = sReader.ReadLine();
                    if (response.Equals("Return"))
                    {
                        break;
                    }

                    Dictionary <String, String[]> edgeAttrList = parametersParser(response);


                    //byte[] fileX = File.ReadAllBytes("Tree.xml");

                    byte[] fileX = engine.create(NomTipSplitDepth[0], NomTipSplitDepth[1], SplitDept, vertexAttrList, edgeAttrList).ToArray();
                    sWriter.WriteLine(fileX.Length.ToString());
                    sWriter.Flush();
                    netStr.Write(fileX, 0, fileX.Length);
                    break;

                /******************MODIFICA ALBERO*****************************/
                case "Modifica":
                    /* int edit(String graphName,int startNode,int endNode, Dictionary<String, String> newNodeAttrList, Dictionary<String, String> newEdgeAttrList, DatabaseInterface dbConnection);*/
                    sWriter.WriteLine("Modifica");
                    sWriter.Flush();
                    infoAlbero = dbInter.getListaAlberi();
                    if (infoAlbero.Length == 0)
                    {
                        sWriter.WriteLine("Vuoto");
                        sWriter.Flush();
                    }
                    else
                    {
                        //Console.WriteLine(parametersParser(dbInter.getListaAlberi()));
                        /*NomeAlbero#attr1Vertex?tipo:attr2Vertex?tipo#attrEdge?tipo:attrEdge2?tipo$NomeAlbero2#etc*/
                        sWriter.WriteLine(parametersParser(infoAlbero));
                        sWriter.Flush();
                    }

                    response = sReader.ReadLine();
                    if (response.Equals("Return"))
                    {
                        break;
                    }

                    String[] nomeInizFin = response.Split('$');
                    response = sReader.ReadLine();
                    String newAttrVertex = response;

                    if (response.Equals("Return"))
                    {
                        break;
                    }

                    Dictionary <String, String> newNodeAttrList = paramParser(response);

                    response = sReader.ReadLine();
                    String newAttrEdge = response;

                    if (response.Equals("Return"))
                    {
                        break;
                    }

                    Dictionary <String, String> newEdgeAttrList = paramParser(response);

                    sWriter.WriteLine(engine.edit(nomeInizFin[0], int.Parse(nomeInizFin[1]), int.Parse(nomeInizFin[2]), newNodeAttrList, newEdgeAttrList, dbInter).ToString());
                    sWriter.Flush();
                    break;

                /******************CALCOLA ALBERO*****************************/
                case "Calcola":
                    sWriter.WriteLine("Calcola");
                    sWriter.Flush();
                    attributeDefinition = dbInter.getAttributeDefinition();
                    infoAlbero          = dbInter.getListaAlberi();
                    if (infoAlbero.Length == 0)
                    {
                        sWriter.WriteLine("Vuoto");
                        sWriter.Flush();
                    }
                    else
                    {
                        //Console.WriteLine(parametersParser(dbInter.getListaAlberi()));
                        /*NomeAlbero#attr1Vertex?tipo:attr2Vertex?tipo#attrEdge?tipo:attrEdge2?tipo$NomeAlbero2#etc*/
                        sWriter.WriteLine(parametersParser(infoAlbero));
                        sWriter.Flush();
                    }

                    response = sReader.ReadLine();
                    if (response.Equals("Return"))
                    {
                        break;
                    }

                    String[] nif = response.Split('$');

                    response = sReader.ReadLine();
                    if (response.Equals("Return"))
                    {
                        break;
                    }

                    String[] vertAttr = response.Split('$');
                    response = sReader.ReadLine();
                    if (response.Equals("Return"))
                    {
                        break;
                    }

                    String[] edgAttr = response.Split('$');

                    /*long calculate(int startNode, int endNode, String treeName, Dictionayr<String, String> edgeAttrList, Dictionary<String, String> vertexAttrList, DatabaseInterface dbInterface)*/
                    Dictionary <String, String> edgeAttr   = getAttrType(edgAttr, attributeDefinition);
                    Dictionary <String, String> vertexAttr = getAttrType(vertAttr, attributeDefinition);

                    sWriter.WriteLine(engine.calculate(int.Parse(nif[1]), int.Parse(nif[2]), nif[0], edgeAttr, vertexAttr, dbInter).ToString());
                    sWriter.Flush();
                    break;

                case "Disconnect":
                    bClientConnected = false;
                    break;


                default:
                    Console.WriteLine("Errore...");
                    bClientConnected = false;
                    break;
                } //Fine switch
            }     //Fine while
            client.Close();
        }//Fine HandleClient
Exemple #2
0
        calculate(int startNode,
                  int endNode,
                  String treeName,
                  Dictionary <String, String> edgeAttrList,
                  Dictionary <String, String> nodeAttrList,
                  DatabaseInterface dbConnection)
        {
            ///////////////////////////////////////////////

            /*
             * DbConnection deve essere passato come argomento perche' e' costruito
             * con i parametri di connessione; in questo modo si mantiene la stessa
             * connessione per tutta la sessione di lavoro.
             */


            List <String> nodeAttrToFwd = new List <String>();
            List <String> edgeAttrToFwd = new List <String>();
            int           sumMode;

            if (nodeAttrList.Count != 0)
            {
                sumMode = 2;
            }
            else
            {
                sumMode = 0;
            }

            if (edgeAttrList.Count != 0)
            {
                sumMode += 1;
            }
            else
            {
                sumMode += 0;
            }

            switch (sumMode)
            {
            case 3:             // considera sia archi che nodi per la somma
                nodeAttrToFwd = TreeLogicEngine.handleAttrs(nodeAttrList);
                edgeAttrToFwd = TreeLogicEngine.handleAttrs(edgeAttrList);
                break;

            case 2:             // considera solo i nodi per la somma
                nodeAttrToFwd = TreeLogicEngine.handleAttrs(nodeAttrList);
                // edgeAttrToFwd = null;
                break;

            case 1:             // considera solo gli archi per la somma
                // nodeAttrToFwd = null;
                edgeAttrToFwd = TreeLogicEngine.handleAttrs(edgeAttrList);
                break;

            case 0:             // le liste per il calcolo sono vuote
                return(0);
            }



            List <String> valuesList = dbConnection.getValues(treeName, startNode, endNode, nodeAttrToFwd, edgeAttrToFwd);

            //List <String> valuesList = LogicEngineTest.getValues(treeName, startNode, endNode, nodeAttrToFwd, edgeAttrToFwd);
            /*** Check difensivo ***/
            if (valuesList == null)
            {
                return(1);
            }


            long totalSum       = 0;
            int  convertedToInt = 0;

            /*** per ogni stringa nella lista... ***/
            for (int i = 0; i < valuesList.Count; i++)
            {
                convertedToInt = 0;
                /*** per ogni carattere della stringa i-esima della lista ***/
                for (int j = 0; j < valuesList[i].Length; j++)
                {
                    convertedToInt = convertedToInt * 10 + (valuesList[i][j] - '0');
                }
                totalSum += convertedToInt;
            }

            /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
            *
            *  Secondo benchmark testati dal nostro team, la conversione con metodo custom
            *  è notevolmente piu' veloce del sistema fornito da .NET:
            *  Referenza -> http://cc.davelozinski.com/c-sharp/fastest-way-to-convert-a-String-to-an-int
            *
            *  // alternativa al metodo custom
            *  foreach (String value in valuesList) {
            *        totalSum += int.Parse(value)
            *  }
            *
            * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

            return(totalSum);
        } // End of method calculate()
Exemple #3
0
/*___________________________________________________________________________________________________*/

        public void HandleClient(object obj)
        {
            TcpClient client = (TcpClient)obj;

            StreamWriter sWriter = new StreamWriter(client.GetStream(), Encoding.UTF8);
            StreamReader sReader = new StreamReader(client.GetStream(), Encoding.UTF8);

            Boolean bClientConnected = true;
            String[] dbCred =sReader.ReadLine().Split('$');
            DatabaseInterface dbInter = new DatabaseInterface(dbCred[0], dbCred[1], dbCred[2]);
            String check_auth = dbInter.check().ToString();
            sWriter.WriteLine(check_auth);
            sWriter.Flush();
            /*Connessione al database fallita*/
            if (check_auth.Equals("1"))
            {
                client.Close();
                return;
            }
            /*Connessione al database andata a buon fine*/
            /*Prendiamo i dati dal database*/
            /*Dobbiamo separare i dati contenuti in infoAlbero[] in lista alberi con relativi attributi (Divisi tra 
             * vertex ed edge)*/
            /*NomeAlbero#attr1Vertex?tipo:attr2Vertex?tipo#attrEdge?tipo:attrEdge2?tipo$NomeAlbero2#etc*/
            InfoAlbero[] infoAlbero= dbInter.getListaAlberi();
            if (infoAlbero.Length == 0)
            {
                sWriter.WriteLine("Vuoto");
                sWriter.Flush();
            }
            else
            {
                //Console.WriteLine(parametersParser(dbInter.getListaAlberi()));
                sWriter.WriteLine(parametersParser(infoAlbero));
                sWriter.Flush();
            }
            /*Dobbiamo spedire la attribute definitions*/
            Dictionary<String, String> attributeDefinition = dbInter.getAttributeDefinition();
            /*foreach (KeyValuePair<String, String> val in attributeDefinition)
            {
                Console.WriteLine(val);
            }*/
            //Console.WriteLine(parametersParser(attributeDefinition));
            sWriter.WriteLine(parametersParser(attributeDefinition));
            sWriter.Flush();
            String response = "";
            /*_____Inizio scelta funzioni in base all'operazione scelta__________________________________________________*/
            NetworkStream netStr = client.GetStream();
            TreeLogicEngine engine = new TreeLogicEngine();
            
            while (bClientConnected)
            {
                response = sReader.ReadLine();
                if(response.Equals("Return")){continue;}

              switch (response)
                    {
                        /******************IMPORTA FILE*****************************/
                        case "Importa":
                            sWriter.WriteLine("Importa");
                            sWriter.Flush();
                            /*Leggiamo la dimensione del file da leggere*/
                            response = sReader.ReadLine();
                            if (response.Equals("Return")) { break; }
                            int length = int.Parse(response);
                            //Console.WriteLine(length);
                            byte[] file = new byte[length];
                            netStr.Read(file, 0, length);
                            //Console.WriteLine(file.Length);
                            MemoryStream stream = new MemoryStream(file);
                            
                           
                            /*  public static int import(MemoryStream XmlStream, DatabaseInterface   DbConnection);*/
                            
                            sWriter.WriteLine(FileEngine.import(stream, dbInter).ToString());
                            sWriter.Flush();
                            
                            break;

                        /******************ESPORTA FILE*****************************/
                        case "Esporta":
                            sWriter.WriteLine("Esporta");
                            sWriter.Flush();
                            response = sReader.ReadLine();
                            if (response.Equals("Return")) { break; }
                            /*public static MemoryStream export(String treeName,  DatabaseInterface DbConnection);*/
                            //byte[] fileXml = File.ReadAllBytes("Tree.xml");
                            byte[] fileXml = FileEngine.export(response, dbInter).ToArray();
                            sWriter.WriteLine(fileXml.Length.ToString());
                            sWriter.Flush();
                            netStr.Write(fileXml, 0, fileXml.Length);
                            break;

                        /******************CREA ALBERO*****************************/
                        case "Crea":
                            sWriter.WriteLine("Crea");
                            sWriter.Flush();
                            String[] NomTipSplitDepth = new String[4];
                            String[] SplitDept = new String[2];
                            
                            //Nome$Tipo$Splitsize$Depth
                            response = sReader.ReadLine();
                            if (response.Equals("Return")) { break; }
                            NomTipSplitDepth = response.Split('$');
                            SplitDept[0] = NomTipSplitDepth[2];
                            SplitDept[1] = NomTipSplitDepth[3];

                            //NomeAttrVertex1#tipoAttr:valore$NomeAttrVertex2#tipoAttr2:valore2$etc
                            response = sReader.ReadLine();
                            if (response.Equals("Return")) { break; }
                            
                            Dictionary<String, String[]> vertexAttrList = parametersParser(response);
                            //NomeAttrEdge1#tipoAttr:valore$NomeAttrEdge2#tipoAttr2:valore2$etc
                            response = sReader.ReadLine();
                            if (response.Equals("Return")) { break; }
                           
                            Dictionary<String, String[]> edgeAttrList = parametersParser(response);
                            

                            //byte[] fileX = File.ReadAllBytes("Tree.xml");
                           
                            byte[] fileX = engine.create(NomTipSplitDepth[0], NomTipSplitDepth[1], SplitDept, vertexAttrList, edgeAttrList).ToArray();
                            sWriter.WriteLine(fileX.Length.ToString());
                            sWriter.Flush();
                            netStr.Write(fileX, 0, fileX.Length);
                            break;
                        
                        /******************MODIFICA ALBERO*****************************/
                        case "Modifica":
                       /* int edit(String graphName,int startNode,int endNode, Dictionary<String, String> newNodeAttrList, Dictionary<String, String> newEdgeAttrList, DatabaseInterface dbConnection);*/
                            sWriter.WriteLine("Modifica"); 
                            sWriter.Flush();
                            infoAlbero= dbInter.getListaAlberi();
                            if (infoAlbero.Length == 0)
                            {
                            sWriter.WriteLine("Vuoto");
                            sWriter.Flush();
                            }
                            else
                            {
                          //Console.WriteLine(parametersParser(dbInter.getListaAlberi()));
                          /*NomeAlbero#attr1Vertex?tipo:attr2Vertex?tipo#attrEdge?tipo:attrEdge2?tipo$NomeAlbero2#etc*/
                             sWriter.WriteLine(parametersParser(infoAlbero));
                             sWriter.Flush();
                             }
                            
                            response = sReader.ReadLine();
                            if (response.Equals("Return")) { break; }
                           
                            String[] nomeInizFin = response.Split('$');
                            response = sReader.ReadLine();
                            String newAttrVertex = response;
                           
                            if (response.Equals("Return")) { break; }
                            
                            Dictionary<String, String> newNodeAttrList = paramParser(response);
                           
                            response = sReader.ReadLine();
                            String newAttrEdge = response;
                         
                            if (response.Equals("Return")) { break; }
                            
                            Dictionary<String, String> newEdgeAttrList = paramParser(response);
                           
                            sWriter.WriteLine(engine.edit(nomeInizFin[0], int.Parse(nomeInizFin[1]), int.Parse(nomeInizFin[2]), newNodeAttrList, newEdgeAttrList, dbInter).ToString());
                            sWriter.Flush();
                            break;

                        /******************CALCOLA ALBERO*****************************/
                        case "Calcola":
                            sWriter.WriteLine("Calcola");
                            sWriter.Flush();
                            attributeDefinition = dbInter.getAttributeDefinition();
                            infoAlbero= dbInter.getListaAlberi();
                            if (infoAlbero.Length == 0)
                            {
                            sWriter.WriteLine("Vuoto");
                            sWriter.Flush();
                            }
                            else
                            {
                          //Console.WriteLine(parametersParser(dbInter.getListaAlberi()));
                          /*NomeAlbero#attr1Vertex?tipo:attr2Vertex?tipo#attrEdge?tipo:attrEdge2?tipo$NomeAlbero2#etc*/
                             sWriter.WriteLine(parametersParser(infoAlbero));
                             sWriter.Flush();
                             }

                            response = sReader.ReadLine();
                            if (response.Equals("Return")) { break; }
                           
                            String[] nif = response.Split('$');
                      
                            response = sReader.ReadLine();
                            if (response.Equals("Return")) { break; }
                          
                            String[] vertAttr = response.Split('$');
                            response = sReader.ReadLine();
                            if (response.Equals("Return")) { break; }
                       
                            String[] edgAttr = response.Split('$');
                            
                            /*long calculate(int startNode, int endNode, String treeName, Dictionayr<String, String> edgeAttrList, Dictionary<String, String> vertexAttrList, DatabaseInterface dbInterface)*/
                            Dictionary<String, String> edgeAttr = getAttrType(edgAttr, attributeDefinition);
                            Dictionary<String, String> vertexAttr = getAttrType(vertAttr, attributeDefinition);

                            sWriter.WriteLine(engine.calculate(int.Parse(nif[1]), int.Parse(nif[2]), nif[0], edgeAttr, vertexAttr, dbInter).ToString());
                            sWriter.Flush();
                            break;
                        
                        case "Disconnect":
                            bClientConnected = false;
                            break;
                                
                        
                        default:
                            Console.WriteLine("Errore...");
                            bClientConnected = false;
                            break;
                           

                    }//Fine switch
                }//Fine while
            client.Close();
        }//Fine HandleClient