Esempio n. 1
0
        public async void start()
        {
            Console.WriteLine("Starting server...");

            IPAddress   ip       = IPAddress.Parse("127.0.0.1");
            TcpListener listener = new TcpListener(ip, 2920);

            listener.Start();

            ReaderWriterDb readerWriterDb = ReaderWriterDb.getInstance();

            Console.WriteLine("Server started...");

            while (true)
            {
                TcpClient client = listener.AcceptTcpClient();
                Console.WriteLine("Client connected");

                NetworkStream stream = client.GetStream();
                // read
                byte[] dataFromClient = new byte[1024];
                int    bytesRead      = stream.Read(dataFromClient, 0, dataFromClient.Length);
                string s = Encoding.ASCII.GetString(dataFromClient, 0, bytesRead);

                switch (s)
                {
                case "GetAccounts":
                {
                    content = await readerWriterDb.getAccountsAsync();

                    break;
                }

                case "GetAddresses":
                {
                    content = await readerWriterDb.getAddresses();

                    break;
                }

                case "GetBankInfos":
                {
                    content = await readerWriterDb.getBankInfos();

                    break;
                }

                case "Register":
                {
                    string   objectString = getClientsObject(stream);
                    Register addAccount   = JsonSerializer.Deserialize <Register>(objectString);
                    Account  newAccount   = await readerWriterDb.Register(addAccount);

                    content = JsonSerializer.Serialize(newAccount);
                    break;
                }

                case "GetRecipes":
                {
                    content = await readerWriterDb.getRecipesAsync();

                    break;
                }

                case "GetIngredients":
                {
                    string objectString = getClientsObject(stream);
                    int    recipeint    = JsonSerializer.Deserialize <int>(objectString);
                    content = await readerWriterDb.getIngredientsForRecipe(recipeint);

                    break;
                }

                case "GetAllIngredients":
                {
                    content = await readerWriterDb.getIngredientsAsync();

                    break;
                }

                case "GetShopIngredients":
                {
                    content = await readerWriterDb.getShopIngredientsAsync();

                    break;
                }

                case "Order":
                {
                    byte[] data1ToClient = Encoding.ASCII.GetBytes("Received");
                    stream.Write(data1ToClient, 0, data1ToClient.Length);
                    byte[] objectFromClient = new byte[1024];
                    Console.WriteLine(1);
                    int    objectRead   = stream.Read(objectFromClient, 0, objectFromClient.Length);
                    string objectString = Encoding.ASCII.GetString(objectFromClient, 0, objectRead);
                    Order  addOrder     = JsonSerializer.Deserialize <Order>(objectString);
                    Console.WriteLine(objectString);
                    IList <OrderedShopIngredients> newOrderedShopIngredients =
                        addOrder.OrderedShopIngredients;
                    await readerWriterDb.addNewOrder(addOrder, newOrderedShopIngredients);

                    content = JsonSerializer.Serialize("Added order");
                    break;
                }

                case "GetOrders":
                {
                    content = await readerWriterDb.getOrdersAsync();

                    break;
                }
                }
                // respond
                byte[] dataToClient = Encoding.ASCII.GetBytes(content);
                stream.Write(dataToClient, 0, dataToClient.Length);

                // close connection
                client.Close();
            }
        }
        public async void start()
        {
            Console.WriteLine("Starting server...");

            IPAddress ip = IPAddress.Parse("127.0.0.1");

            TcpListener listener = new TcpListener(ip, 2921);

            listener.Start();

            ReaderWriterDb readerWriterDb = ReaderWriterDb.getInstance();

            Console.WriteLine("Server started...");
            while (true)
            {
                TcpClient client = listener.AcceptTcpClient();
                Console.WriteLine("Client connected");
                NetworkStream stream = client.GetStream();

                byte[] rcvBytes = new byte[1024];
                stream.Read(rcvBytes);
                String rcv = System.Text.Encoding.ASCII.GetString(rcvBytes);

                //Get methods
                if (rcv.Contains("get"))
                {
                    if (rcv.Contains("Accounts"))
                    {
                        content = await readerWriterDb.getAccountsAsync();
                    }
                    else if (rcv.Contains("ShopIngredients"))
                    {
                        content = await readerWriterDb.getShopIngredientsAsync();
                    }
                    else if (rcv.Contains("Shops"))
                    {
                        content = await readerWriterDb.getShopsAsync();
                    }
                    else if (rcv.Contains("Ingredients"))
                    {
                        content = await readerWriterDb.getIngredientsAsync();
                    }
                    else if (rcv.Contains("Recipes"))
                    {
                        content = await readerWriterDb.getRecipesAsync();
                    }
                    else if (rcv.Contains("Orders"))
                    {
                        content = await readerWriterDb.getOrdersAsync();
                    }
                    else if (rcv.Contains("Categories"))
                    {
                        content = await readerWriterDb.getCategoriesAsync();
                    }
                    //Add methods
                }
                else if (rcv.Contains("add"))
                {
                    string subString = rcv.Substring(3);
                    if (rcv.Contains("ShopIngredient"))
                    {
                        content = await readerWriterDb.addObjectAsync(getClientsObject(stream, "ShopIngredient"), "shopIngredient");
                    }
                    else if (rcv.Contains("Ingredient"))
                    {
                        content = await readerWriterDb.addObjectAsync(getClientsObject(stream, "Ingredient"), "ingredient");
                    }
                    else if (rcv.Contains("Shop"))
                    {
                        content = await readerWriterDb.addObjectAsync(getClientsObject(stream, "Shop"), "shop");
                    }
                    else if (rcv.Contains("Recipe"))
                    {
                        String categoryName = "";
                        int    stringEnd    = rcv.IndexOf(":");
                        int    stringStart  = 9;
                        int    length       = stringEnd - 9;
                        categoryName = rcv.Substring(stringStart, length);
                        readerWriterDb.sendCategory(categoryName);
                        content = await readerWriterDb.addObjectAsync(getClientsObject(stream, "Recipe"),
                                                                      "recipe");
                    }
                    //Remove methods
                }
                else if (rcv.Contains("remove"))
                {
                    if (rcv.Contains("ShopIngredient"))
                    {
                        content = await readerWriterDb.removeObjectAsync(getClientsObject(stream, "ShopIngredient"), "shopIngredient");
                    }
                    else if (rcv.Contains("Ingredient"))
                    {
                        content = await readerWriterDb.removeObjectAsync(getClientsObject(stream, "Ingredient"), "ingredient");
                    }
                    else if (rcv.Contains("Shop"))
                    {
                        content = await readerWriterDb.removeObjectAsync(getClientsObject(stream, "Shop"), "shop");
                    }
                    //Update methods
                }
                else if (rcv.Contains("update"))
                {
                    if (rcv.Contains("Shop"))
                    {
                        content = await readerWriterDb.updateObjectAsync(getClientsObject(stream, "Shop"), "shop");
                    }
                    else if (rcv.Contains("Ingredient"))
                    {
                        content = await readerWriterDb.updateObjectAsync(getClientsObject(stream, "Ingredient"), "ingredient");
                    }
                }
                else if (rcv.Contains("link"))
                {
                    if (rcv.Contains("ShopIngredientToShop1"))
                    {
                        shopToLink = (Shop)getClientsObject(stream, "Shop");
                        content    = "received";
                    }
                    else if (rcv.Contains("ShopIngredientToShop2"))
                    {
                        ShopIngredient shopIngredientToLink = (ShopIngredient)getClientsObject(stream, "ShopIngredient");
                        if (shopToLink != null && shopIngredientToLink != null)
                        {
                            content = await readerWriterDb.linkObjectsAsync(shopIngredientToLink, shopToLink, "ShopIngredientToShop");
                        }
                        else
                        {
                            content = "null error";
                        }
                    }
                }
                // Sending
                byte[] toSendBytes = System.Text.Encoding.ASCII.GetBytes(content);
                stream.Write(toSendBytes);

                client.Close();
            }
        }