home *** CD-ROM | disk | FTP | other *** search
- //==========================================================================
- // File: StockNet.cs
- //
- // Summary: This file implements StockQuote class for NGWS Remoting
- //
- //
- // Classes: StockQuote class in StockNet namespace
- //
- // Functions: GetLastTradePrice
- //
- //--------------------------------------------------------------------------
- // This file is part of the Microsoft NGWS Samples.
- //
- // Copyright (C) 2000 Microsoft Corporation. All rights reserved.
- //==========================================================================
-
- using System;
- using System.Text;
- using System.Net;
- using System.IO;
- using System.Globalization;
- using System.Runtime.Serialization;
- using System.Threading;
-
- using System.Web;
-
- namespace StockNet
- {
- // Marshal By Value
- [serializable]
- public class Quote
- {
- public Quote(String Symbol)
- {
- Company = Company.FindCompany(Symbol);
- }
-
- public Company Company;
- public String LastTradePrice;
- }
-
- // Marshal By Value
- [serializable]
- public class Company
- {
- public override String ToString()
- {
- return Symbol + " " + Name;
- }
-
- public Company(String _Symbol, String _Name)
- {
- Symbol = _Symbol;
- Name = _Name;
-
- Companies[cCompanies++] = this;
- }
-
- public String Name;
- public String Symbol;
- public Company[] Partners;
- public Company[] Competitors;
-
- private static Company[] Companies;
- private static int cCompanies = 0;
- private static bool fInitialized = false;
-
- internal static Company FindCompany(String Symbol)
- {
- if (!fInitialized)
- {
- fInitialized = true;
- BuildCompanies();
- }
-
- for (int i=0; i<cCompanies; i++)
- {
- if (Companies[i].Symbol == Symbol)
- {
- return Companies[i];
- }
- }
-
- throw new Exception("Company not found: " + Symbol);
- }
-
- internal static void BuildCompanies()
- {
- Companies = new Company[10];
-
- new Company("MSFT", "Microsoft");
- new Company("AOL", "America Online");
- new Company("SUNW", "Sun Microsystems");
- new Company("AAPL", "Apple Computers");
- new Company("ORCL", "Oracle");
- new Company("INTC", "Intel");
- new Company("CPQ", "Compaq");
- new Company("DELL", "Dell");
-
- Company c = Company.FindCompany("MSFT");
- c.Competitors = new Company[4];
- c.Competitors[0] = Company.FindCompany("AOL");
- c.Competitors[1] = Company.FindCompany("SUNW");
- c.Competitors[2] = Company.FindCompany("AAPL");
- c.Competitors[3] = Company.FindCompany("ORCL");
- c.Partners = new Company[3];
- c.Partners[0] = Company.FindCompany("INTC");
- c.Partners[1] = Company.FindCompany("ORCL");
- c.Partners[2] = Company.FindCompany("DELL");
-
- c = Company.FindCompany("AOL");
- c.Competitors = new Company[1];
- c.Competitors[0] = Company.FindCompany("MSFT");
- c.Partners = new Company[1];
- c.Partners[0] = Company.FindCompany("SUNW");
-
- c = Company.FindCompany("SUNW");
- c.Competitors = new Company[2];
- c.Competitors[0] = Company.FindCompany("MSFT");
- c.Competitors[1] = Company.FindCompany("INTC");
- c.Partners = new Company[2];
- c.Partners[0] = Company.FindCompany("INTC");
- c.Partners[1] = Company.FindCompany("AOL");
-
- c = Company.FindCompany("AAPL");
- c.Competitors = new Company[1];
- c.Competitors[0] = Company.FindCompany("AAPL");
- c.Partners = new Company[0];
-
- c = Company.FindCompany("ORCL");
- c.Competitors = new Company[1];
- c.Competitors[0] = Company.FindCompany("MSFT");
- c.Partners = new Company[1];
- c.Partners[0] = Company.FindCompany("SUNW");
-
- c = Company.FindCompany("INTC");
- c.Competitors = new Company[3];
- c.Competitors[0] = Company.FindCompany("SUNW");
- c.Competitors[1] = Company.FindCompany("CPQ");
- c.Competitors[2] = Company.FindCompany("DELL");
- c.Partners = new Company[1];
- c.Partners[0] = Company.FindCompany("SUNW");
-
- c = Company.FindCompany("DELL");
- c.Competitors = new Company[2];
- c.Competitors[0] = Company.FindCompany("CPQ");
- c.Competitors[1] = Company.FindCompany("SUNW");
- c.Partners = new Company[2];
- c.Partners[0] = Company.FindCompany("INTC");
- c.Partners[1] = Company.FindCompany("MSFT");
-
- c = Company.FindCompany("CPQ");
- c.Competitors = new Company[2];
- c.Competitors[0] = Company.FindCompany("DELL");
- c.Competitors[1] = Company.FindCompany("SUNW");
- c.Partners = new Company[2];
- c.Partners[0] = Company.FindCompany("INTC");
- c.Partners[1] = Company.FindCompany("MSFT");
-
- }
- }
-
- // Marshal By Ref : Well Known WebService
- public class StockQuote: MarshalByRefObject
- {
-
- // Return the quote as an Object
- public Quote GetQuoteFromStatic(String symbol)
- {
- Quote quote = new Quote(symbol);
-
- quote.LastTradePrice = "100 1/4";
-
- return quote;
- }
-
- // GetStockValue function get call from client side after establishing connection to server
- // It opens stock.txt and extract stock value for given company title.
- public Quote GetQuoteFromFile(String symbol)
- {
- Quote quote = new Quote(symbol);
- StreamReader reader;
-
- String filepath;
- FileStream fs;
-
- HttpContext httpContext = HttpContext.Current;
-
- try
- {
- if (null == httpContext)
- {
- fs = new FileStream("stock.txt",FileMode.Open,FileAccess.Read);
- } else
- {
- filepath = httpContext.Server.MapPath("stock.txt");
- fs = new FileStream(filepath,FileMode.Open,FileAccess.Read);
- }
- } catch (Exception e)
- {
- e = e;
- // This is the last resort, return a quote from static data
- return GetQuoteFromStatic(symbol);
- }
-
- String output;
- String[] tokens;
- char[] sep;
-
- sep = new char[1];
- sep[0] = '#';
- reader = new StreamReader(fs); // set FileStream to StreamReader
-
- //start reading file and serach for given title
- while (reader.Peek() > -1)
- {
- output = reader.ReadLine(); // read line by line data file<stock.txt>
-
- tokens= output.Split(sep);
-
- if (( String.Compare(tokens[0],symbol, true)) == 0 )
- {
- quote.LastTradePrice = tokens[1];
- return quote;
- } else
- {
- quote.LastTradePrice = "0";
- }
- }
- reader.Close(); //close StreamReader
-
- {
- quote.LastTradePrice = "0";
- return quote;
- }
- }
-
- //
- // This method performs scnreen scraping from a web page that can change
- //
- public Quote GetQuoteFromLiveFeed(String symbol)
- {
- Quote quote = new Quote(symbol);
- int BufferSize = 1024;
- String Host;
- WebRequest Request;
- WebResponse Response;
- Stream RespStream;
- byte [] Buffer;
- int BytesRead;
-
-
- StringBuilder sbHost = new StringBuilder("http://moneycentral.msn.com/scripts/webquote.dll?ipage=qd&Symbol=");
- sbHost.Append(symbol);
- Host = sbHost.ToString();
-
- /* -- uncommment this code and replace "httpproxy" with the name of your
- -- http proxy server
-
-
- String defaultProxyName = "httpproxy";
- int defaultProxyPort = 80;
-
- // create a new Object with the Proxy Information that
- // you wish to use
-
- DefaultControlObject proxyObject = new DefaultControlObject( defaultProxyName, defaultProxyPort);
-
- // Disable Proxy use when the host is local i.e. without periods.
- proxyObject.ProxyNoLocal = true;
-
- // Now actually take over the global with our new settings, all new requests
- // use this proxy info
-
- GlobalProxySelection.Select = proxyObject;
- */
-
- // Request = WebRequestFactory.Create(Host + "/");
- Request = WebRequestFactory.Create(Host);
-
- Response = Request.GetResponse();
-
- RespStream = Response.GetResponseStream();
-
- BufferSize = (int)Response.ContentLength;
-
- Buffer = new byte[BufferSize];
-
- BytesRead = RespStream.Read(Buffer, 0, BufferSize);
-
- String quoteString = null;
- while (BytesRead != 0)
- {
- quoteString = Encoding.ASCII.GetString(Buffer, 0, BytesRead);
-
- int n=0;
- int i=0;
-
- n = quoteString.IndexOf("http://moneycentral.msn.com/scripts/webquote.dll?iPage=qd&Symbol=" + symbol + "&FYI=True");
- if (n >= 0)
- {
- n = quoteString.IndexOf("<TD ALIGN=RIGHT NOWRAP>", n);
- if (n>=0)
- {
- n = quoteString.IndexOf(" ", n);
- if (n >=0)
- {
- n = n + 6;
- i = quoteString.IndexOf("<", n);
- if (i >= 0)
- {
- if (quoteString == null)
- quoteString = quoteString.Substring(n, i - n);
- break;
- }
- }
- }
- }
-
- BytesRead = RespStream.Read(Buffer, 0, BufferSize);
- }
-
- quote.LastTradePrice = quoteString;
-
- return quote;
- }
-
-
- // As String ------------------------------------------
-
- public String GetLastTradePriceFromStatic(String symbol)
- {
- Quote quote = GetQuoteFromStatic(symbol);
- return quote.LastTradePrice;
- }
-
- public String GetLastTradePriceFromFile(String symbol)
- {
- Quote quote = GetQuoteFromFile(symbol);
- return quote.LastTradePrice;
- }
-
- public String GetLastTradePriceFromLiveFeed(String symbol)
- {
- Quote quote = GetQuoteFromLiveFeed(symbol);
- return quote.LastTradePrice;
- }
-
- public String GetLastTradePrice(String symbol)
- {
- //return GetLastTradePriceFromStatic(symbol);
- return GetLastTradePriceFromFile(symbol);
- //return GetLastTradePriceFromLiveFeed(symbol);
- }
-
- public Quote GetQuote(String symbol)
- {
- // return GetQuoteFromStatic(symbol);
- return GetQuoteFromFile(symbol);
- // return GetQuoteFromLiveFeed(symbol);
- }
-
-
- public Quote[] GetQuotes(String[] symbols)
- {
- int count = symbols.Length;
- Quote[] quotes = new Quote[count];
- for (int index=0;index<count;index++)
- {
- //quotes[index] = GetQuoteFromStatic(symbols[index]);
- quotes[index] = GetQuoteFromFile(symbols[index]);
- //quotes[index] = GetQuoteFromLiveFeed(symbols[index]);
- }
- Console.WriteLine("StockQuote.GetQuotes");
- return quotes;
- }
-
- }
-
- public class BrokerageAccount : MarshalByRefObject
- {
- public String _name;
- public int totalShares = 0;
-
- public BrokerageAccount()
- {
- Console.WriteLine("BrokerageAccount constructor - no args");
- _name = "Jane Doe";
- }
-
- public bool Trade(String symbol, int shares)
- {
- //@@TODO - replace with writes to DB
- Monitor.Enter(this);
- Console.WriteLine("Symbol: " + symbol + " change: " + shares);
- totalShares += shares;
- Monitor.Exit(this);
- return true;
- }
-
- public int RetrieveTotalShared()
- {
- return totalShares;
- }
-
- public String RetrieveName()
- {
- return _name;
- }
-
- protected override void Finalize()
- {
- Console.WriteLine("Finalize called");
- }
- }
-
- public class BrokerageService : MarshalByRefObject
- {
- public BrokerageService()
- {
- }
-
- public BrokerageAccount RetrieveAccount(String accountNumber)
- {
- return new BrokerageAccount();
- }
- }
- }
-
-