home *** CD-ROM | disk | FTP | other *** search
- //==========================================================================
- // File: MyStockApp.cs
- //
- // Summary: This file implements client side of NGWS Remoting. It
- // illustrates contexts and remoting library calls for NGWS
- // Remoting client.
- //
- // Classes: MyStockApp
- //
- //--------------------------------------------------------------------------
- // This file is part of the Microsoft NGWS Samples.
- //
- // Copyright (C) 1999 Microsoft Corporation. All rights reserved.
- //==========================================================================
-
- using System;
- using System.IO;
- using System.Net;
- using System.Text;
- using System.Runtime.Remoting;
- using StockNet;
-
- public class StockClient
- {
- public static void Scenario1()
- {
- Console.WriteLine("new StockQuote");
- String symbol = "INTC";
- StockQuote stockQuote = new StockQuote();
- String lastTradePrice = stockQuote.GetLastTradePrice(symbol);
- Console.WriteLine("LastTradePrice for : " + symbol + " is " + lastTradePrice);
- }
-
- public static void ScenarioStockInteractiveQuote()
- {
- try
- {
- // Connecting to remote server and obtaining the remote object
- StockQuote stockQuote = new StockQuote();
-
- DateTime dt = new DateTime();
- dt = DateTime.Now;
-
- Console.WriteLine("**** Welcome to StockNet, " + dt.ToString() + " ****\n");
- Console.Write("Enter company code (ALL: To list all, 0: Exit): ");
-
- String symbol = Console.ReadLine();
- char[] sep = {';'};
-
- while (String.Compare(symbol,"0", true) != 0)
- {
-
- String[] Symbols = symbol.Split(sep);
-
- Quote[] quotes = null;
-
- try
- {
-
- quotes = stockQuote.GetQuotes(Symbols);
-
- } catch (WebException we)
- {
- Console.WriteLine(we.ToString());
-
- /*
- int BufferSize = 1024;
- String Host;
- WebRequest Request;
- WebResponse Response;
- Stream RespStream;
- byte [] Buffer;
- int BytesRead;
-
- Response = we.Response;
-
- Console.WriteLine("Status code: " + Response.Status.ToString());
-
- RespStream = Response.GetResponseStream();
-
- BufferSize = (int)Response.ContentLength;
-
- Console.WriteLine("Entity body:");
-
- Buffer = new byte[BufferSize];
-
- BytesRead = RespStream.Read(Buffer, 0, BufferSize);
-
- while (BytesRead != 0)
- {
- String content = Encoding.ASCII.GetString(Buffer, 0, BytesRead);
- Console.Write(content);
-
- BytesRead = RespStream.Read(Buffer, 0, BufferSize);
-
- }
- */
-
- }
-
-
-
- for (int index=0;index < quotes.Length;index++)
- {
- Console.WriteLine("Symbol : {0}", quotes[index].Company.Name);
- Console.WriteLine("Company : {0}", quotes[index].Company.Symbol);
- Console.WriteLine("Last Trade Price: {0}", quotes[index].LastTradePrice);
- Console.WriteLine("\nCompetitors: ");
- for (int ic=0; ic < quotes[index].Company.Competitors.Length; ic++)
- Console.WriteLine(" " + quotes[index].Company.Competitors[ic]);
-
- Console.WriteLine("\nPartners: ");
- for (int ic=0; ic < quotes[index].Company.Partners.Length; ic++)
- Console.WriteLine(" " + quotes[index].Company.Partners[ic]);
-
- Console.WriteLine("\n");
- }
-
- Console.WriteLine();
- Console.Write("\nEnter company code (ALL: To list all, 0: Exit): ");
- symbol = Console.ReadLine();
- }
- } catch (Exception e)
- {
- Console.WriteLine("EXCEPTION THROWN!!!!!!");
- Console.WriteLine(e.Message);
- Console.WriteLine(e.GetType().FullName);
- Console.WriteLine(e.StackTrace);
- }
-
- }
-
- public static void Scenario3()
- {
- Console.WriteLine("new BrokerageService");
-
- bool result = true;
- String symbol = "MSFT";
-
- BrokerageService brokerageService = new BrokerageService();
-
- BrokerageAccount brokerageAccount = brokerageService.RetrieveAccount("10002000");
-
- Console.WriteLine("Name: " + brokerageAccount.RetrieveName());
-
- result = brokerageAccount.Trade(symbol, 100);
- Console.WriteLine("After Trade Total Shares for : " + symbol + " is " + brokerageAccount.RetrieveTotalShared());
-
- result = brokerageAccount.Trade(symbol, 200);
- Console.WriteLine("After Trade Total Shares for : " + symbol + " is " + brokerageAccount.RetrieveTotalShared());
-
- result = brokerageAccount.Trade(symbol, -150);
- Console.WriteLine("Total Shares for : " + symbol + " is " + brokerageAccount.RetrieveTotalShared());
-
- //LeaseService leaseService = new LeaseService(brokerageAccount);
- ILease lease = (ILease)brokerageAccount.GetLifetimeService();
-
- Console.WriteLine("CurrentLeaseTime " + lease.CurrentLeaseTime);
- Console.WriteLine("InitialLeaseTime " + lease.InitialLeaseTime);
- Console.WriteLine("RenewOnCallTime " + lease.RenewOnCallTime);
- Console.WriteLine("SponsorshipTimeout " + lease.SponsorshipTimeout);
- Console.WriteLine("RenewOnCallTime " + lease.RenewOnCallTime);
-
- RemotingServices.Disconnect(brokerageAccount);
- brokerageAccount = null;
-
- Console.WriteLine("Sleeping for 180 seconds");
- System.Threading.Thread.Sleep(180000);
- }
-
- public static int Main(string[] args)
- {
- int ret = Initialize(args);
- Console.WriteLine("Stock Remoting Sample\n");
-
- if (ret != -1)
- {
- switch (scenario)
- {
- case 1:
- Scenario1();
- break;
- case 2:
- ScenarioStockInteractiveQuote();
- break;
- case 3:
- Scenario3();
- break;
- }
- }
-
- Console.WriteLine("\nEnd\n");
-
- return ret;
- }
-
-
- //---------------------------------------------------
- public static int scenario = 1;
-
- public static int Initialize(String[] args)
- {
- if (args.Length == 0)
- {
- RemotingServices.ConfigureRemoting("MyStockApp.cfg");
- return 0;
- }
-
-
- for (int i=0;i<args.Length;i++)
- {
- if (
- String.Compare(args[i],"HELP", true) == 0 ||
- String.Compare(args[i],"?", true) == 0 ||
- String.Compare(args[i],"/h", true) == 0 ||
- String.Compare(args[i],"-h", true) == 0 ||
- String.Compare(args[i],"-?", true) == 0 ||
- String.Compare(args[i],"/?", true) == 0
- )
- {
- Usage();
- return -1;
- }
-
- if (args[i].CompareTo("-s1")==0)
- {
- scenario = 1;
- }
-
- if (args[i].CompareTo("-i")==0)
- {
- scenario = 2;
- }
-
- if (args[i].CompareTo("-s3")==0)
- {
- scenario = 3;
- }
-
- if (args[i].CompareTo("-cfg")==0)
- {
- RemotingServices.ConfigureRemoting(args[i+1]);
- }
- }
-
- return 0;
- }
-
- public static void Usage()
- {
- Console.WriteLine("Usage: MyStockApp [-i] [-cfg Configfile.cfg]\n");
- Console.WriteLine("Examples : MyStockApp");
- Console.WriteLine("Examples : MyStockApp -s1 (for GetLastTradePrice)");
- Console.WriteLine("Examples : MyStockApp -i (for Interactive Mode)");
- Console.WriteLine("Examples : MyStockApp -s3 (for BrokerageService)");
- Console.WriteLine("Examples : MyStockApp -cfg MyStockApp.cfg");
- Console.WriteLine("Examples : MyStockApp -i -cfg MyStockApp.cfg");
- Console.WriteLine("Examples : MyStockApp -a -cfg MyStockApp.cfg");
- }
- }
-
-
-