コード例 #1
0
 static internal void Run(TcpClient client, string json)
 {
     try
     {
         Request           r    = JsonSerializer.Deserialize <Request>(json);
         PromiseMapElement elem = new PromiseMapElement(client, r);
         elem.ToggleState();
         int id = Promisemap.Add(elem);
         SQL_Socket.Execute(id);
     }
     catch (JsonException)
     {
         Console.WriteLine("ERROR: The Request is not a valid JSON");
     }
 }
コード例 #2
0
        internal static void Run(int id)
        {
            PromiseMapElement elem = Promisemap.AcquireElement(id);

            string ip = elem.client.Client.RemoteEndPoint.ToString();

            string json = JsonSerializer.Serialize(elem.GetResponse());

            // Send back a response.
            Lib.Write(elem.client, json, false);
            Console.WriteLine($"({ip}) Sent: {json}");

            // Disconnect client after sending the response
            elem.client.Close();
            Console.WriteLine($"({ip}) Client disconnected");

            Promisemap.Remove(id);
        }
コード例 #3
0
        static internal void Run(int id)
        {
            PromiseMapElement elem = Promisemap.AcquireElement(id);

            try
            {
                Request    rq    = elem.request;
                List <Car> cars  = new List <Car>();
                bool       valid = true;

                //yyyymmdd
                string startDate = rq.start.Year.ToString() + rq.start.Month.ToString() + rq.start.Day.ToString();
                string endDate   = rq.end.Year.ToString() + rq.end.Month.ToString() + rq.end.Day.ToString();

                dbMutex.WaitOne(-1);

                if (!init)
                {
                    init = true;
                    string source      = $"..\\..\\..\\..\\Datenbank\\Datenbank.sql";
                    string destination = $"..\\..\\..\\..\\Datenbank\\Datenbank.db";

                    bool tmp = false;
                    if (!File.Exists(destination))
                    {
                        tmp = true;
                    }

                    db = new SqliteConnection("Data Source=" + destination);
                    db.Open();

                    if (tmp)
                    {
                        InitializeDatabase(source);
                    }

                    try
                    {
                        SqliteCommand cmd = db.CreateCommand();
                        cmd.CommandText =
                            @"Select VermietungID FROM Vermietung ORDER BY VermietungID DESC LIMIT 1";
                        SqliteDataReader r = cmd.ExecuteReader();
                        if (r.HasRows)
                        {
                            r.Read();
                            vermietungID = (uint)r.GetInt32(0) + 1;
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
                else
                {
                    db.Open();
                }

                SqliteCommand command = db.CreateCommand();

                if (rq.carID == -1)
                {
                    command.CommandText =
                        @"
                    SELECT *
                    FROM Autos
                    WHERE AutoID NOT IN(
                        SELECT AutoID
                        FROM Vermietung
                        WHERE(Anfang <= $endDate  AND Anfang >= $startDate)
                        OR   (Ende   <= $endDate  AND Ende   >= $startDate)
                        )
                    ";
                    command.Parameters.AddWithValue("$startDate", startDate);
                    command.Parameters.AddWithValue("$endDate", endDate);
                }
                else
                {
                    try
                    {
                        SqliteCommand cmd = db.CreateCommand();
                        cmd.CommandText =
                            @"
                        SELECT AutoID
                        FROM Vermietung
                        WHERE(AutoID = $id
                        AND ((Anfang <= $endDate  AND Anfang >= $startDate)
                        OR   (Ende   <= $endDate  AND Ende   >= $startDate))
                        )
                        ";
                        cmd.Parameters.AddWithValue("$id", elem.request.carID);
                        cmd.Parameters.AddWithValue("$startDate", startDate);
                        cmd.Parameters.AddWithValue("$endDate", endDate);
                        SqliteDataReader r = cmd.ExecuteReader();
                        if (r.HasRows)
                        {
                            r.Read();
                            valid = false;
                            Console.WriteLine($"Booking Validity Check returned: {r.GetInt32(0)}");
                            r.Close();
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Booking Validity Check returned:");
                        Console.WriteLine(e.Message);
                    }

                    if (valid)
                    {
                        command.CommandText =
                            @"INSERT INTO Vermietung (VermietungID, Anfang, Ende, AutoID)
                        VALUES($vermietungID, $startDate, $endDate, $id)                        
                        ";
                        command.Parameters.AddWithValue("$vermietungID", vermietungID++);
                        command.Parameters.AddWithValue("$startDate", startDate);
                        command.Parameters.AddWithValue("$endDate", endDate);
                        command.Parameters.AddWithValue("$id", id);
                    }
                }

                if (valid)
                {
                    SqliteDataReader reader = command.ExecuteReader();

                    IEnumerator readerEnumerator = reader.GetEnumerator();

                    while (readerEnumerator.MoveNext())
                    {
                        //AutoID int NOT NULL PRIMARY KEY, Modell char(50), Marke char(50),
                        //Kraftstoffart char(50), Leistung int, Typ char(50), Sitzplaetze int, Tueren int, Tagespreis int
                        int    _AutoID      = reader.GetInt32(0);
                        string _model       = reader.GetString(1);
                        string _brand       = reader.GetString(2);
                        string _fueltype    = reader.GetString(3);
                        int    _power       = reader.GetInt32(4);
                        string _type        = reader.GetString(5);
                        int    _seats       = reader.GetInt32(6);
                        int    _doors       = reader.GetInt32(7);
                        int    _pricePerDay = reader.GetInt32(8);

                        cars.Add(new Car(_AutoID, _model, _brand, _fueltype, _power, _type, _seats, _doors, _pricePerDay));
                    }
                }

                dbMutex.ReleaseMutex();

                db.Close();

                if (valid)
                {
                    elem.SetResponse(new Response("OK", cars));
                }
                else
                {
                    elem.SetResponse(new Response("INVALID"));
                }
                elem.ToggleState();
            }
            catch (Exception e)
            {
                Console.WriteLine($"Exception occured: {e.Message}");
                elem.SetResponse(new Response(e.Message));
            }

            Promisemap.ReleaseElement(elem);
            //Forward to DeliveryHandler
            DeliveryHandler.Handle(id);
        }