Пример #1
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);
                }
            }
        }
Пример #2
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);
                }
            }
        }