home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / Remoting / MyStock / MyStockApp.cs < prev    next >
Encoding:
Text File  |  2000-06-23  |  8.4 KB  |  263 lines

  1. //==========================================================================
  2. //  File:       MyStockApp.cs
  3. //
  4. //  Summary:    This file implements client side of NGWS Remoting. It 
  5. //              illustrates contexts and remoting library calls for NGWS 
  6. //              Remoting client.
  7. //           
  8. //  Classes:    MyStockApp
  9. //
  10. //--------------------------------------------------------------------------
  11. //  This file is part of the Microsoft NGWS Samples.
  12. //
  13. //  Copyright (C) 1999 Microsoft Corporation.  All rights reserved.
  14. //==========================================================================
  15.  
  16. using System;
  17. using System.IO;
  18. using System.Net;
  19. using System.Text;
  20. using System.Runtime.Remoting;
  21. using StockNet;
  22.  
  23. public class StockClient
  24. {
  25.     public static void Scenario1()  
  26.     {
  27.         Console.WriteLine("new StockQuote");
  28.         String symbol = "INTC";
  29.         StockQuote stockQuote = new StockQuote();
  30.         String lastTradePrice = stockQuote.GetLastTradePrice(symbol);
  31.         Console.WriteLine("LastTradePrice for : " + symbol + " is " + lastTradePrice);
  32.     }
  33.  
  34.     public static void ScenarioStockInteractiveQuote()  
  35.     {
  36.         try
  37.         {
  38.             // Connecting to remote server and obtaining the remote object
  39.             StockQuote stockQuote = new StockQuote();
  40.  
  41.             DateTime dt = new DateTime();
  42.             dt = DateTime.Now;
  43.  
  44.             Console.WriteLine("**** Welcome to StockNet, " + dt.ToString() + " ****\n");
  45.             Console.Write("Enter company code (ALL: To list all, 0: Exit): ");
  46.  
  47.             String symbol = Console.ReadLine();
  48.             char[] sep = {';'};
  49.  
  50.             while (String.Compare(symbol,"0", true) != 0)
  51.             {
  52.  
  53.                 String[] Symbols = symbol.Split(sep);
  54.  
  55.                 Quote[] quotes = null;
  56.  
  57.                 try
  58.                 {
  59.  
  60.                     quotes = stockQuote.GetQuotes(Symbols);
  61.  
  62.                 } catch (WebException we)
  63.                 {
  64.                     Console.WriteLine(we.ToString());
  65.  
  66. /*
  67.         int     BufferSize = 1024;
  68.         String                  Host;
  69.         WebRequest              Request;
  70.         WebResponse             Response;
  71.         Stream                  RespStream;
  72.         byte []                 Buffer;
  73.         int                     BytesRead;
  74.  
  75.         Response = we.Response;
  76.         
  77.         Console.WriteLine("Status code: " + Response.Status.ToString());
  78.  
  79.         RespStream = Response.GetResponseStream();
  80.  
  81.     BufferSize = (int)Response.ContentLength;
  82.  
  83.         Console.WriteLine("Entity body:");
  84.  
  85.         Buffer = new byte[BufferSize];
  86.  
  87.         BytesRead = RespStream.Read(Buffer, 0, BufferSize);
  88.  
  89.         while (BytesRead != 0)
  90.         {
  91.             String content = Encoding.ASCII.GetString(Buffer, 0, BytesRead); 
  92.             Console.Write(content);
  93.  
  94.             BytesRead = RespStream.Read(Buffer, 0, BufferSize);
  95.  
  96.         }
  97. */
  98.  
  99.                 }
  100.  
  101.  
  102.  
  103.                 for (int index=0;index < quotes.Length;index++)
  104.                 {
  105.                     Console.WriteLine("Symbol          : {0}", quotes[index].Company.Name);
  106.                     Console.WriteLine("Company         : {0}", quotes[index].Company.Symbol);
  107.                     Console.WriteLine("Last Trade Price: {0}", quotes[index].LastTradePrice);
  108.                     Console.WriteLine("\nCompetitors: ");
  109.                     for (int ic=0; ic < quotes[index].Company.Competitors.Length; ic++)
  110.                         Console.WriteLine("   " + quotes[index].Company.Competitors[ic]);
  111.  
  112.                     Console.WriteLine("\nPartners: ");
  113.                     for (int ic=0; ic < quotes[index].Company.Partners.Length; ic++)
  114.                         Console.WriteLine("   " + quotes[index].Company.Partners[ic]);
  115.  
  116.                     Console.WriteLine("\n");
  117.                 }
  118.  
  119.                 Console.WriteLine();
  120.                 Console.Write("\nEnter company code (ALL: To list all, 0: Exit): ");
  121.                 symbol = Console.ReadLine();
  122.             }
  123.         } catch (Exception e)
  124.         {
  125.             Console.WriteLine("EXCEPTION THROWN!!!!!!");
  126.             Console.WriteLine(e.Message);
  127.             Console.WriteLine(e.GetType().FullName);
  128.             Console.WriteLine(e.StackTrace);
  129.         }
  130.  
  131.     }
  132.  
  133.     public static void Scenario3()  
  134.     {
  135.         Console.WriteLine("new BrokerageService");
  136.  
  137.         bool result = true;
  138.         String symbol = "MSFT";
  139.  
  140.         BrokerageService brokerageService = new BrokerageService();
  141.  
  142.         BrokerageAccount brokerageAccount = brokerageService.RetrieveAccount("10002000");
  143.  
  144.         Console.WriteLine("Name: " + brokerageAccount.RetrieveName());
  145.  
  146.         result = brokerageAccount.Trade(symbol, 100);
  147.         Console.WriteLine("After Trade Total Shares for : " + symbol + " is " + brokerageAccount.RetrieveTotalShared());
  148.  
  149.         result = brokerageAccount.Trade(symbol, 200);
  150.         Console.WriteLine("After Trade Total Shares for : " + symbol + " is " + brokerageAccount.RetrieveTotalShared());
  151.  
  152.         result = brokerageAccount.Trade(symbol, -150);
  153.         Console.WriteLine("Total Shares for : " + symbol + " is " + brokerageAccount.RetrieveTotalShared());
  154.  
  155.         //LeaseService leaseService = new LeaseService(brokerageAccount);
  156.         ILease lease = (ILease)brokerageAccount.GetLifetimeService();
  157.  
  158.         Console.WriteLine("CurrentLeaseTime " + lease.CurrentLeaseTime);
  159.         Console.WriteLine("InitialLeaseTime " + lease.InitialLeaseTime);
  160.         Console.WriteLine("RenewOnCallTime " + lease.RenewOnCallTime);
  161.         Console.WriteLine("SponsorshipTimeout " + lease.SponsorshipTimeout);
  162.         Console.WriteLine("RenewOnCallTime " + lease.RenewOnCallTime);
  163.  
  164.         RemotingServices.Disconnect(brokerageAccount);
  165.         brokerageAccount = null;
  166.  
  167.         Console.WriteLine("Sleeping for 180 seconds");  
  168.         System.Threading.Thread.Sleep(180000);
  169.     }
  170.  
  171.     public static int Main(string[] args)
  172.     {
  173.         int ret = Initialize(args);
  174.         Console.WriteLine("Stock Remoting Sample\n");
  175.  
  176.         if (ret != -1)
  177.         {
  178.             switch (scenario)
  179.             {
  180.             case 1: 
  181.                 Scenario1();
  182.                 break;
  183.             case 2:
  184.                 ScenarioStockInteractiveQuote();
  185.                 break;
  186.             case 3:
  187.                 Scenario3();
  188.                 break;
  189.             }
  190.         }
  191.  
  192.         Console.WriteLine("\nEnd\n");
  193.  
  194.         return ret;
  195.     }
  196.  
  197.  
  198.     //---------------------------------------------------
  199.     public static int scenario = 1;
  200.  
  201.     public static int Initialize(String[] args)
  202.     {
  203.         if (args.Length == 0)
  204.         {
  205.             RemotingServices.ConfigureRemoting("MyStockApp.cfg");
  206.             return 0;
  207.         }
  208.  
  209.  
  210.         for (int i=0;i<args.Length;i++)
  211.         {
  212.             if (
  213.                String.Compare(args[i],"HELP", true) == 0 ||
  214.                String.Compare(args[i],"?", true) == 0 ||
  215.                String.Compare(args[i],"/h", true) == 0 ||
  216.                String.Compare(args[i],"-h", true) == 0 ||
  217.                String.Compare(args[i],"-?", true) == 0 ||
  218.                String.Compare(args[i],"/?", true) == 0
  219.                )
  220.             {
  221.                 Usage();
  222.                 return -1;
  223.             }
  224.  
  225.             if (args[i].CompareTo("-s1")==0)
  226.             {
  227.                 scenario = 1;
  228.             }
  229.  
  230.             if (args[i].CompareTo("-i")==0)
  231.             {
  232.                 scenario = 2;
  233.             }
  234.  
  235.             if (args[i].CompareTo("-s3")==0)
  236.             {
  237.                 scenario = 3;
  238.             }
  239.  
  240.             if (args[i].CompareTo("-cfg")==0)
  241.             {
  242.                 RemotingServices.ConfigureRemoting(args[i+1]);
  243.             }
  244.         }
  245.  
  246.         return 0;
  247.     }
  248.  
  249.     public static void Usage()
  250.     {
  251.         Console.WriteLine("Usage: MyStockApp [-i] [-cfg Configfile.cfg]\n");
  252.         Console.WriteLine("Examples : MyStockApp");
  253.         Console.WriteLine("Examples : MyStockApp -s1   (for GetLastTradePrice)");
  254.         Console.WriteLine("Examples : MyStockApp -i   (for Interactive Mode)");
  255.         Console.WriteLine("Examples : MyStockApp -s3   (for BrokerageService)");
  256.         Console.WriteLine("Examples : MyStockApp -cfg MyStockApp.cfg");
  257.         Console.WriteLine("Examples : MyStockApp -i -cfg MyStockApp.cfg");
  258.         Console.WriteLine("Examples : MyStockApp -a -cfg MyStockApp.cfg");
  259.     }
  260. }
  261.  
  262.  
  263.