Exemple #1
0
        private void Expire(Produs p)
        {
            p.State = "Expired";
            String notification = "notification$" + p.Name + " expired!$";

            foreach (User u in clients)
            {
                SendPacket(notification, u);
            }
            refresh_users_auctions();
        }
Exemple #2
0
        private void Form1_Load(object sender, EventArgs e)
        {
            //Declare and Initialize the IP Adress
            IPAddress ipAd = IPAddress.Parse("192.168.3.108");

            //Declare and Initilize the Port Number;
            int PortNumber = 8888;

            /* Initializes the Listener */
            ServerListener = new TcpListener(ipAd, PortNumber);
            clients        = new List <User>();
            auctions       = new List <Produs>();

            ServerListener.Start();


            Produs p = new Produs("test", "Ion", 50, 50);

            auctions.Add(p);

            Thread Connections = new Thread(WaitForConnections);

            Connections.Start();
        }
Exemple #3
0
        private void DoWorkOnPacket(string[] packet)
        {
            switch (packet[0])
            {
            case "upload_produs":
                int    error = 0;
                String owner, name;
                int    starting_price, current_price;

                name = packet[1];


                foreach (Produs p in auctions)
                {
                    if (p.Name == name)
                    {
                        foreach (User u in clients)
                        {
                            if (u.Username.Substring(0, p.Owner.Length) == p.Owner)
                            {
                                String notificaiton = "notification$There already exists an offer for this product!$";
                                SendPacket(notificaiton, u);
                            }
                        }
                        error = 1;
                    }
                }
                if (error == 0)

                {
                    owner          = packet[2];
                    starting_price = Int32.Parse(packet[3]);
                    current_price  = Int32.Parse(packet[4]);

                    Produs p = new Produs(name, owner, starting_price, current_price);

                    auctions.Add(p);

                    Task.Delay(new TimeSpan(0, 1, 0)).ContinueWith(o => { Expire(p); });


                    refresh_users_auctions();

                    foreach (User u in clients)
                    {
                        if (u.Username != p.Owner)
                        {
                            String notification = "notification$A new offer" + p.Name + " by " + p.Owner + " with starting price of " + p.Starting_price + " has been listed!!$";
                            SendPacket(notification, u);
                        }
                    }
                }

                break;

            case "bid":
                foreach (Produs pr in auctions)
                {
                    if (pr.Name == packet[1])
                    {
                        if (pr.Current_price >= Int32.Parse(packet[2]))
                        {
                            foreach (User u in clients)
                            {
                                if ((u.Username.Substring(0, packet[3].Length)) == packet[3])
                                {
                                    String notification = "notification$Error: Your bid cannot be lower or equal to the current bid!$";
                                    SendPacket(notification, u);
                                }
                            }
                        }
                        else
                        {
                            pr.Current_price = Int32.Parse(packet[2]);

                            foreach (User u in clients)
                            {
                                if (u.Username.Substring(0, packet[3].Length) == packet[3])
                                {
                                    pr.BidWinner = u;
                                    if (!pr.Subscribers.Contains(u))
                                    {
                                        ;
                                    }
                                    pr.Subscribers.Add(u);
                                }
                            }

                            foreach (User u in pr.Subscribers)
                            {
                                if (u.Username.Substring(0, pr.Owner.Length) == pr.Owner)
                                {
                                    String notification = ("notification$Somebody has placed a higher bid on" + pr.Name + "!$");
                                    SendPacket(notification, u);
                                }
                                if (u != pr.BidWinner)
                                {
                                    String notification = ("notification$Somebody has placed a higher bid on" + pr.Name + "!$");
                                    SendPacket(notification, u);
                                }
                            }
                        }
                    }
                }
                refresh_users_auctions();
                break;
            }
        }