Ejemplo n.º 1
0
        public static String generateID()
        {
            lock (fileLock){
                lastID++;
                String idToUpdate = Convert.ToString(lastID);
                idToUpdate = idToUpdate.Replace(" ", ""); //in case of any
                Boolean idValid = false;

                //Make sure that the id hasn't already been assigned to an online client (could occur if server restarted.
                while (!idValid)
                {
                    foreach (var entry in clients)
                    {
                        ClientHandler client = entry.Value;
                        if (client.getID().Equals(idToUpdate))
                        {
                            lastID++;
                            idToUpdate = Convert.ToString(lastID);
                        }
                    }
                    idValid = true;
                }

                updateIDFile(idToUpdate);
                return(Convert.ToString(lastID));
            }
        }
Ejemplo n.º 2
0
        public static void newConnection(ClientHandler newClient)
        {
            clients.Add(newClient.getID(), newClient);
            Stock sampleStock = stock[0];

            if ((clients.Count == 1) && (sampleStock.getOwner() == null))
            {
                sampleStock.setOwner(newClient);
            }
        }
Ejemplo n.º 3
0
        public static void disconnectClient(ClientHandler disconnectingClient)
        {
            clients.Remove(disconnectingClient.getID());


            if ((stock[0].getOwner() == disconnectingClient))
            {
                resetStock();
                Console.WriteLine("Stock is now unowned, waiting for next connection");
            }
        }
Ejemplo n.º 4
0
        public String connectionsToString()
        {
            StringBuilder result = new StringBuilder("[CONN]");

            foreach (var entry in Market.clients)
            {
                ClientHandler client = entry.Value;
                result.Append(" " + client.getID());
            }


            return(result.ToString());
        }
Ejemplo n.º 5
0
        //Updates the users of the current market
        public static void updateMarket(String message)
        {
            object lockObject = new Object();

            /*
             * if (!(message.startsWith("[UPDATE]"))){
             *  if (!(message.startsWith("[CONN]"))){
             *      message = "[UPDATE] " + message;
             *  }
             * }*/
            Console.WriteLine("sending update to clients. Message: " + message);
            lock (lockObject){
                foreach (var entry in clients)
                {
                    ClientHandler client = entry.Value;
                    Console.WriteLine("Sending to client: " + client.getID());
                    client.sendMessage(message);
                }
            }
        }
Ejemplo n.º 6
0
        //TODO doesnt work: may have to sync threads.
        public static Boolean trade(ClientHandler oldOwner, ClientHandler newOwner, Stock stock)
        {
            Console.WriteLine(newOwner.getID() + " is in trade");

            // UNCOMMENT FOR TESTING IF A TRADER DISCONNECTS MID-TRADE

/*
 *      try {
 *          Thread.sleep(10000);
 *      } catch (InterruptedException e) {
 *          e.printStackTrace();
 *      }
 */

            lock (fileLock){
                //ClientHandler oldOwner = stock.getOwner();
                if (oldOwner == stock.getOwner())
                {
                    //Check if the owner is also the buyer
                    if (stock.getOwner() == newOwner)
                    {
                        Console.WriteLine("stock owner: " + stock.getOwner().getID());
                        Console.WriteLine("new owner: " + newOwner.getID());
                        newOwner.sendMessage("Buy/Sell failed: cannot sell to owner.");
                        Console.WriteLine("There was an attempted trade of: " + stock.getName() + " but failed, because the owner tried to trade with themselves.");
                        return(false);
                    }
                    else
                    {
                        stock.setOwner(newOwner);
                        Console.WriteLine("new owner: " + stock.getOwner().getID());
                        //Check that new owner has been assigned
                        if (stock.getOwner() == newOwner)
                        {
                            if ((oldOwner.isConnected()) && (newOwner.isConnected())) //Check if traders are still connected before finalisation of trade.
                            {
                                String updateMsg = "[UPDATE]Stock is now owned by trader: " + Market.getStock("sample stock").getOwner().getID();
                                updateMarket(updateMsg);
                                return(true);
                            }
                            else
                            {
                                stock.setOwner(oldOwner);
                                Console.WriteLine("new owner offline");
                                return(false);
                            }
                        }
                        else
                        {
                            Console.WriteLine("Stock owner was unable to be changed.");
                            return(false);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("client: " + oldOwner.getID() + " tried to sell the stock, but are not the owner.");
                    return(false);
                }
            }
        }