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

  1. //==========================================================================
  2. //  File:       StockNet.cs
  3. //
  4. //  Summary:    This file implements StockQuote class for NGWS Remoting
  5. //           
  6. //
  7. //  Classes:    StockQuote class in StockNet namespace
  8. //
  9. //  Functions:  GetLastTradePrice
  10. //
  11. //--------------------------------------------------------------------------
  12. //  This file is part of the Microsoft NGWS Samples.
  13. //
  14. //  Copyright (C) 2000 Microsoft Corporation.  All rights reserved.
  15. //==========================================================================
  16.  
  17. using System;
  18. using System.Text;
  19. using System.Net;
  20. using System.IO;
  21. using System.Globalization;
  22. using System.Runtime.Serialization;
  23. using System.Threading;
  24.  
  25. using System.Web;
  26.  
  27. namespace StockNet
  28. {
  29.     // Marshal By Value
  30.     [serializable]
  31.     public class Quote
  32.     {
  33.         public Quote(String Symbol)
  34.         {
  35.             Company = Company.FindCompany(Symbol);
  36.         }
  37.  
  38.         public Company Company;
  39.         public String  LastTradePrice;
  40.     }
  41.  
  42.     // Marshal By Value
  43.     [serializable]
  44.     public class Company
  45.     {
  46.         public override String ToString()
  47.         {
  48.             return Symbol + " " + Name;
  49.         }
  50.  
  51.         public Company(String _Symbol, String _Name) 
  52.         {
  53.             Symbol = _Symbol;
  54.             Name = _Name;
  55.  
  56.             Companies[cCompanies++] = this;
  57.         }
  58.  
  59.         public String Name;
  60.         public String Symbol;
  61.         public Company[] Partners;
  62.         public Company[] Competitors;
  63.  
  64.         private static Company[] Companies;
  65.         private static int cCompanies = 0;
  66.         private static bool fInitialized = false;
  67.  
  68.         internal static Company FindCompany(String Symbol)
  69.         {
  70.             if (!fInitialized)
  71.             {
  72.                 fInitialized = true;
  73.                 BuildCompanies();
  74.             }
  75.  
  76.             for (int i=0; i<cCompanies; i++)
  77.             {
  78.                 if (Companies[i].Symbol == Symbol)
  79.                 {
  80.                     return Companies[i];
  81.                 }
  82.             }
  83.  
  84.             throw new Exception("Company not found: " + Symbol);
  85.         }
  86.  
  87.         internal static void BuildCompanies()
  88.         {
  89.             Companies  = new Company[10];
  90.  
  91.             new Company("MSFT",   "Microsoft");
  92.             new Company("AOL",    "America Online");
  93.             new Company("SUNW",   "Sun Microsystems");
  94.             new Company("AAPL",   "Apple Computers");
  95.             new Company("ORCL",   "Oracle");
  96.             new Company("INTC",   "Intel");
  97.             new Company("CPQ",   "Compaq");
  98.             new Company("DELL",   "Dell");
  99.  
  100.             Company c = Company.FindCompany("MSFT");
  101.             c.Competitors = new Company[4];
  102.             c.Competitors[0] = Company.FindCompany("AOL");
  103.             c.Competitors[1] = Company.FindCompany("SUNW");
  104.             c.Competitors[2] = Company.FindCompany("AAPL");
  105.             c.Competitors[3] = Company.FindCompany("ORCL");
  106.             c.Partners = new Company[3];
  107.             c.Partners[0] = Company.FindCompany("INTC");
  108.             c.Partners[1] = Company.FindCompany("ORCL");
  109.             c.Partners[2] = Company.FindCompany("DELL");
  110.  
  111.             c = Company.FindCompany("AOL");
  112.             c.Competitors = new Company[1];
  113.             c.Competitors[0] = Company.FindCompany("MSFT");
  114.             c.Partners = new Company[1];
  115.             c.Partners[0] = Company.FindCompany("SUNW");
  116.  
  117.             c = Company.FindCompany("SUNW");
  118.             c.Competitors = new Company[2];
  119.             c.Competitors[0] = Company.FindCompany("MSFT");
  120.             c.Competitors[1] = Company.FindCompany("INTC");
  121.             c.Partners = new Company[2];
  122.             c.Partners[0] = Company.FindCompany("INTC");
  123.             c.Partners[1] = Company.FindCompany("AOL");
  124.  
  125.             c = Company.FindCompany("AAPL");
  126.             c.Competitors = new Company[1];
  127.             c.Competitors[0] = Company.FindCompany("AAPL");
  128.             c.Partners = new Company[0];
  129.  
  130.             c = Company.FindCompany("ORCL");
  131.             c.Competitors = new Company[1];
  132.             c.Competitors[0] = Company.FindCompany("MSFT");
  133.             c.Partners = new Company[1];
  134.             c.Partners[0] = Company.FindCompany("SUNW");
  135.  
  136.             c = Company.FindCompany("INTC");
  137.             c.Competitors = new Company[3];
  138.             c.Competitors[0] = Company.FindCompany("SUNW");
  139.             c.Competitors[1] = Company.FindCompany("CPQ");
  140.             c.Competitors[2] = Company.FindCompany("DELL");
  141.             c.Partners = new Company[1];
  142.             c.Partners[0] = Company.FindCompany("SUNW");
  143.  
  144.             c = Company.FindCompany("DELL");
  145.             c.Competitors = new Company[2];
  146.             c.Competitors[0] = Company.FindCompany("CPQ");
  147.             c.Competitors[1] = Company.FindCompany("SUNW");
  148.             c.Partners = new Company[2];
  149.             c.Partners[0] = Company.FindCompany("INTC");
  150.             c.Partners[1] = Company.FindCompany("MSFT");
  151.  
  152.             c = Company.FindCompany("CPQ");
  153.             c.Competitors = new Company[2];
  154.             c.Competitors[0] = Company.FindCompany("DELL");
  155.             c.Competitors[1] = Company.FindCompany("SUNW");
  156.             c.Partners = new Company[2];
  157.             c.Partners[0] = Company.FindCompany("INTC");
  158.             c.Partners[1] = Company.FindCompany("MSFT");
  159.  
  160.         }
  161.     }
  162.  
  163.     // Marshal By Ref : Well Known WebService
  164.     public class StockQuote: MarshalByRefObject
  165.     {
  166.  
  167.         // Return the quote as an Object
  168.         public Quote GetQuoteFromStatic(String symbol) 
  169.         {
  170.             Quote quote = new Quote(symbol);
  171.  
  172.             quote.LastTradePrice = "100 1/4";
  173.  
  174.             return quote;
  175.         }
  176.  
  177.         //  GetStockValue function get call from client side after establishing connection to server
  178.         //  It opens stock.txt and extract stock value for given company title.
  179.         public Quote GetQuoteFromFile(String symbol) 
  180.         { 
  181.             Quote quote = new Quote(symbol);
  182.             StreamReader    reader;
  183.  
  184.             String          filepath;
  185.             FileStream      fs;
  186.  
  187.             HttpContext     httpContext = HttpContext.Current;
  188.  
  189.             try
  190.             {
  191.                 if (null == httpContext)
  192.                 {
  193.                     fs = new FileStream("stock.txt",FileMode.Open,FileAccess.Read);
  194.                 } else
  195.                 {
  196.                     filepath = httpContext.Server.MapPath("stock.txt");
  197.                     fs = new FileStream(filepath,FileMode.Open,FileAccess.Read);
  198.                 }
  199.             } catch (Exception e)
  200.             {
  201.                 e = e;
  202.                 // This is the last resort, return a quote from static data
  203.                 return GetQuoteFromStatic(symbol);
  204.             }
  205.  
  206.             String          output;
  207.             String[]        tokens;
  208.             char[]          sep;
  209.  
  210.             sep = new char[1];
  211.             sep[0] = '#';
  212.             reader  =  new StreamReader(fs); // set FileStream to StreamReader
  213.  
  214.             //start reading file and serach for given title
  215.             while (reader.Peek() > -1)
  216.             {
  217.                 output =  reader.ReadLine(); // read line by line data file<stock.txt>
  218.  
  219.                 tokens= output.Split(sep);               
  220.  
  221.                 if (( String.Compare(tokens[0],symbol, true)) == 0 )
  222.                 {
  223.                     quote.LastTradePrice = tokens[1];
  224.                     return quote;
  225.                 } else
  226.                 {
  227.                     quote.LastTradePrice = "0";
  228.                 }
  229.             }
  230.             reader.Close();          //close StreamReader
  231.  
  232.             {
  233.                 quote.LastTradePrice = "0";
  234.                 return quote;
  235.             }
  236.         }
  237.  
  238.         //
  239.         // This method performs scnreen scraping from a web page that can change 
  240.         //
  241.         public Quote GetQuoteFromLiveFeed(String symbol) 
  242.         {
  243.             Quote quote = new Quote(symbol);
  244.             int     BufferSize = 1024;
  245.             String                  Host;
  246.             WebRequest              Request;
  247.             WebResponse             Response;
  248.             Stream                  RespStream;
  249.             byte []                 Buffer;
  250.             int                     BytesRead;
  251.  
  252.  
  253.             StringBuilder sbHost = new StringBuilder("http://moneycentral.msn.com/scripts/webquote.dll?ipage=qd&Symbol=");
  254.             sbHost.Append(symbol);
  255.             Host =  sbHost.ToString();
  256.  
  257.             /* -- uncommment this code and replace "httpproxy" with the name of your
  258.                -- http proxy server
  259.  
  260.  
  261.             String defaultProxyName = "httpproxy";
  262.             int defaultProxyPort = 80;
  263.  
  264.             // create a new Object with the Proxy Information that 
  265.             //  you wish to use
  266.  
  267.             DefaultControlObject proxyObject = new DefaultControlObject(      defaultProxyName, defaultProxyPort); 
  268.  
  269.             // Disable Proxy use when the host is local i.e. without periods.
  270.             proxyObject.ProxyNoLocal = true;
  271.  
  272.             // Now actually take over the global with our new settings, all new requests 
  273.             // use this proxy info
  274.  
  275.             GlobalProxySelection.Select = proxyObject;
  276.             */
  277.  
  278.             //        Request = WebRequestFactory.Create(Host + "/");
  279.             Request = WebRequestFactory.Create(Host);
  280.  
  281.             Response = Request.GetResponse();
  282.  
  283.             RespStream = Response.GetResponseStream();
  284.  
  285.             BufferSize = (int)Response.ContentLength;
  286.  
  287.             Buffer = new byte[BufferSize];
  288.  
  289.             BytesRead = RespStream.Read(Buffer, 0, BufferSize);
  290.  
  291.             String quoteString = null;
  292.             while (BytesRead != 0)
  293.             {
  294.                 quoteString = Encoding.ASCII.GetString(Buffer, 0, BytesRead); 
  295.  
  296.                 int n=0;
  297.                 int i=0;
  298.  
  299.                 n = quoteString.IndexOf("http://moneycentral.msn.com/scripts/webquote.dll?iPage=qd&Symbol=" + symbol + "&FYI=True");
  300.                 if (n >= 0)
  301.                 {
  302.                     n = quoteString.IndexOf("<TD ALIGN=RIGHT NOWRAP>", n);
  303.                     if (n>=0)
  304.                     {
  305.                         n = quoteString.IndexOf(" ", n);
  306.                         if (n >=0)
  307.                         {
  308.                             n = n + 6;
  309.                             i = quoteString.IndexOf("<", n);
  310.                             if (i >= 0)
  311.                             {
  312.                                 if (quoteString == null)
  313.                                     quoteString = quoteString.Substring(n, i - n);
  314.                                 break;
  315.                             }
  316.                         }
  317.                     }
  318.                 }
  319.  
  320.                 BytesRead = RespStream.Read(Buffer, 0, BufferSize);
  321.             }
  322.  
  323.             quote.LastTradePrice = quoteString;
  324.  
  325.             return quote;
  326.         }
  327.  
  328.  
  329.         // As String ------------------------------------------
  330.  
  331.         public String GetLastTradePriceFromStatic(String symbol) 
  332.         {
  333.             Quote quote = GetQuoteFromStatic(symbol);
  334.             return quote.LastTradePrice;
  335.         }
  336.  
  337.         public String GetLastTradePriceFromFile(String symbol) 
  338.         {
  339.             Quote quote = GetQuoteFromFile(symbol);
  340.             return quote.LastTradePrice;
  341.         }
  342.  
  343.         public String GetLastTradePriceFromLiveFeed(String symbol) 
  344.         {
  345.             Quote quote = GetQuoteFromLiveFeed(symbol);
  346.             return quote.LastTradePrice;
  347.         }
  348.  
  349.         public String GetLastTradePrice(String symbol) 
  350.         {
  351.             //return GetLastTradePriceFromStatic(symbol);
  352.             return GetLastTradePriceFromFile(symbol);
  353.             //return GetLastTradePriceFromLiveFeed(symbol);
  354.         }
  355.  
  356.         public Quote GetQuote(String symbol) 
  357.         {
  358.             //    return GetQuoteFromStatic(symbol);
  359.             return GetQuoteFromFile(symbol);
  360.             //    return GetQuoteFromLiveFeed(symbol);
  361.         }
  362.  
  363.  
  364.         public Quote[] GetQuotes(String[] symbols) 
  365.         {
  366.             int count = symbols.Length;
  367.             Quote[] quotes = new Quote[count];
  368.             for (int index=0;index<count;index++)
  369.             {
  370.                 //quotes[index] = GetQuoteFromStatic(symbols[index]);
  371.                 quotes[index] = GetQuoteFromFile(symbols[index]);
  372.                 //quotes[index] = GetQuoteFromLiveFeed(symbols[index]);
  373.             }
  374.             Console.WriteLine("StockQuote.GetQuotes");
  375.             return quotes;
  376.         }
  377.  
  378.     }
  379.  
  380.     public class BrokerageAccount : MarshalByRefObject
  381.     {
  382.         public String _name;
  383.         public int totalShares = 0;
  384.  
  385.         public BrokerageAccount()
  386.         {
  387.             Console.WriteLine("BrokerageAccount constructor - no args");
  388.             _name = "Jane Doe";
  389.         }
  390.  
  391.         public bool Trade(String symbol, int shares)
  392.         {
  393.             //@@TODO - replace with writes to DB
  394.             Monitor.Enter(this);
  395.             Console.WriteLine("Symbol: " + symbol + " change: " + shares);
  396.             totalShares += shares;
  397.             Monitor.Exit(this);
  398.             return true;
  399.         }
  400.  
  401.         public int RetrieveTotalShared()
  402.         {
  403.             return totalShares;
  404.         }
  405.  
  406.         public String RetrieveName()
  407.         {
  408.             return _name;
  409.         }
  410.  
  411.         protected override void Finalize()
  412.         {
  413.             Console.WriteLine("Finalize called");
  414.         }
  415.     }
  416.  
  417.     public class BrokerageService : MarshalByRefObject
  418.     {
  419.         public BrokerageService()
  420.         {
  421.         }
  422.  
  423.         public BrokerageAccount RetrieveAccount(String accountNumber)
  424.         {
  425.             return new BrokerageAccount();
  426.         }
  427.     }
  428. }
  429.  
  430.