Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 48 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
48
Dung lượng
0,95 MB
Nội dung
open System.Net.Sockets open System.Threading open System.Windows.Forms let form = let temp = new Form() temp.Text <- "F# Talk Client" temp.Closing.Add(fun e -> Application.Exit() Environment.Exit(0)) let output = new TextBox(Dock = DockStyle.Fill, ReadOnly = true, Multiline = true) temp.Controls.Add(output) let input = new TextBox(Dock = DockStyle.Bottom, Multiline = true) temp.Controls.Add(input) let tc = new TcpClient() tc.Connect("localhost", 4242) let load() = let run() = let sr = new StreamReader(tc.GetStream()) while(true) do let text = sr.ReadLine() if text <> null && text <> "" then temp.Invoke(new MethodInvoker(fun () -> output.AppendText(text + Environment.NewLine) output.SelectionStart <- output.Text.Length)) |> ignore let t = new Thread(new ThreadStart(run)) t.Start() temp.Load.Add(fun _ -> load()) let sw = new StreamWriter(tc.GetStream()) let keyUp _ = if(input.Lines.Length > 1) then let text = input.Text if (text <> null && text <> "") then CHAPTER 10 ■ DISTRIBUTED APPLICATIONS 246 7575Ch10.qxp 4/27/07 1:06 PM Page 246 begin try sw.WriteLine(text) sw.Flush() with err -> MessageBox.Show(sprintf "Server error\n\n%O" err) |> ignore end; input.Text <- "" input.KeyUp.Add(fun _ -> keyUp e) temp [<STAThread>] do Application.Run(form) Figure 10-1 shows the resulting client-server application. Figure 10-1. The chat client-ser v er application Now you’ll look at how the client in Listing 10-2 works. The first portion of code in the client is taken up initializing various aspects of the form; this is not of interest to you at the moment, though you can find details of how WinForms applications work in Chapter 8. The first part of Listing 10-2 that is relevant to TCP/IP sockets programming is when you connect to the server. You do this by creating a new instance of the TcpClient class and calling its Connect method: CHAPTER 10 ■ DISTRIBUTED APPLICATIONS 247 7575Ch10.qxp 4/27/07 1:06 PM Page 247 let tc = new TcpClient() tc.Connect("localhost", 4242) In this example, you specify localhost, which is the local computer, and port 4242, which is the same port on which the server is listening. In a more realistic example, you’d probably g ive the DNS name of the server or allow the user to give the DNS name, but l ocalhost i s good because it allows you to easily run the sample on one computer. The function that drives the reading of data from the server is the load function. You attach this to the form’s Load event; to ensure this executes after the form is loaded and initialized properly, you need to interact with the form’s controls: temp.Load.Add(fun _ -> load()) To ensure that you read all data coming from the server in a timely manner, you create a new thread to read all incoming requests. To do this, you define the function run, which is then used to start a new thread: let t = new Thread(new ThreadStart(run)) t.Start() Within the definition of run, you first create a StreamReader to read text from the connec- tion, and then you loop infinitely, so the thread does not exit and reads from the connection. When you find data, you must use the form’s Invoke method to update the form; you need to do this because you cannot update the form from a thread other than the one on which it was created: temp.Invoke(new MethodInvoker(fun () -> output.AppendText(text + Environment.NewLine) output.SelectionStart <- output.Text.Length)) The other part of the client that is functionally important is writing messages to the server. You do this in the keyUp function, which is attached to the input text box’s KeyUp event so that every time a key is pressed in the text box, the code is fired: input.KeyUp.Add(fun _ -> keyUp e) The implementation of the keyUp function is fairly straightforward: if you find that there is more than one line—meaning the Enter key has been pressed—you send any available text across the wire and clear the text box. Now that you understand both the client and server, you’ll take a look at a few general points about the application. In both Listings 10-1 and 10-2, y ou called Flush() after each network operation. Otherwise, the information will not be sent across the network until the stream cache fills up, which leads to one user having to type many messages before they appear on the other user’ s scr een. This approach has several problems, particularly on the server side. Allocating a thread for each incoming client ensures a good response to each client, but as the number of client connections gr o ws, so will the amount of context switching needed for the threads, and the overall performance of the server will be reduced. Also, since each client requires its own thread, the maximum number of clients is limited by the maximum number of threads a process can contain. Although these pr oblems can be solv ed, it’s often easier to simply use one of the more abstract protocols discussed next. CHAPTER 10 ■ DISTRIBUTED APPLICATIONS 248 7575Ch10.qxp 4/27/07 1:06 PM Page 248 Using HTTP T he Web uses Hypertext Transfer Protocol (HTTP) to communicate, typically with web browsers, but you might want to make web requests from a script or a program for several reasons, for example, to aggregate site content through RSS or Atom feeds. T o make an HTTP request, you use the static method C reate f rom the S ystem.Net. WebRequest class. This creates a WebRequest object that represents a request to the uniform resource locator (URL, an address used to uniquely address a resource on a network) that was passed to the Create method. You then use the GetResponse method to get the server’s response to your request, represented by the System.Net.WebResponse class. The following example (Listing 10-3) illustrates calling an RSS on the BBC’s website. The core of the example is the function getUrlAsXml, which does the work of retrieving the data from the URL and loading the data into an XmlDocument. The rest of the example illustrates the kind of post-processing you might want to do on the data, in this case displaying the title of each item on the console and allowing users to choose which item to display. Listing 10-3. Using HTTP #light open System.Diagnostics open System.Net open System.Xml let getUrlAsXml (url : string) = let request = WebRequest.Create(url) let response = request.GetResponse() let stream = response.GetResponseStream() let xml = new XmlDocument() xml.Load(new XmlTextReader(stream)) xml let url = "http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/sci/tech/rss.xml" let xml = getUrlAsXml url let mutable i = 1 for node in xml.SelectNodes("/rss/channel/item/title") do printf "%i. %s\r\n" i node.InnerText i <- i + 1 let item = read_int() let newUrl = let xpath = sprintf "/rss/channel/item[%i]/link" item let node = xml.SelectSingleNode(xpath) node.InnerText let proc = new Process() CHAPTER 10 ■ DISTRIBUTED APPLICATIONS 249 7575Ch10.qxp 4/27/07 1:06 PM Page 249 proc.StartInfo.UseShellExecute <- true proc.StartInfo.FileName <- newUrl proc.Start() The results of this example at the time of writing (your results will vary) were as follows: 1. Five-step check for nano safety 2. Neanderthal DNA secrets unlocked 3. Stem cells 'treat muscle disease' 4. World Cup site threat to swallows 5. Clues to pandemic bird flu found 6. Mice star as Olympic food tasters 7. Climate bill sets carbon target 8. Physics promises wireless power 9. Heart 'can carry out own repairs' 10. Average European 'is overweight' 11. Contact lost with Mars spacecraft 12. Air guitar T-shirt rocks for real 13. Chocolate 'cuts blood clot risk' 14. Case for trawl ban 'overwhelming' 15. UN chief issues climate warning 16. Japanese begin annual whale hunt 17. Roman ship thrills archaeologists 18. Study hopeful for world's forests Calling Web Services Web services are based on standards (typically SOAP) that allow applications to exchange data using HTTP. Web services consist of web methods, that is, methods that have been exposed for execution over a network. You can think of this as somewhat similar to F# functions, since a web method has a name, can have parameters, and returns a result. The parameters and results are described in metadata that the web services also exposes, so clients know how to call it. You can call a web service in F# in two ways. You can use the HttpRequest class and gener- ate the XML you need to send, or you can use the wsdl.exe tool that comes with the .NET Framework SDK to generate a proxy for you. Generally, most people prefer using an automati- cally gener ated pr oxy, because it is much easier, but some like to generate the XML themselves since they think it’s easier to handle changes to a web service this way. You’ll look at both options, starting with generating the XML yourself. The example in Listing 10-4 calls the Microsoft Developers Network (MSDN) web service . (MSDN is a vast library containing details about all the APIs and other software aimed at developers that Micr osoft pr o vides.) The call to the web service will retrieve details about a class or method in the BCL. The listing first defines a generic function, getWebService, to call the w eb ser vice. This is slightly more complicated than the getUrlAsXml function in Listing 10-4, because y ou need to send extr a data to the ser v er ; that is, you need to send the name of the w eb method y ou ar e calling and the r equest body—the data that makes up the request’s parameters. CHAPTER 10 ■ DISTRIBUTED APPLICATIONS 250 7575Ch10.qxp 4/27/07 1:06 PM Page 250 You need to use the HTTP POST protocol, rather than the default HTTP GET protocol, so y ou set this in the M ethod p roperty of the W ebRequest c lass. You also need to set the content type to "text/xml": webRequest.Method <- "POST" webRequest.ContentType <- "text/xml" Then you add the web method name to the HTTP header: webRequest.Headers.Add("Web-Method", methodName) And finally you use the GetRequestStream method to get a stream of data into which you write requestBody, the data that makes up the parameters of the request: using (new StreamWriter(webRequest.GetRequestStream())) (fun s -> s.Write(requestBody)) You then go on to define a more specific function, queryMsdn, just to query MSDN using the web service it exposes. This function calls the web service using a template of the request bound to the identifier requestTemplate. The rest of queryMsdn uses XPath to determine whether any results are available and if so writes them to the console. Listing 10-4 shows the full example. Listing 10-4. Calling the MSDN Web Service #light open System open System.IO open System.Net open System.Windows.Forms open System.Xml let getWebService (url : string) (methodName : string) (requestBody : string) = let webRequest = WebRequest.Create(url, Method = "POST", ContentType = "text/xml") webRequest.Headers.Add("Web-Method", methodName) using (new StreamWriter(webRequest.GetRequestStream())) (fun s -> s.Write(requestBody)) let webResponse = webRequest.GetResponse() let stream = webResponse.GetResponseStream() let xml = new XmlDocument() xml.Load(new XmlTextReader(stream)) xml let (requestTemplate : Printf.string_format<_>) = @"<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> <soap:Body> <getContentRequest xmlns=""urn:msdn-com:public-content-syndication""> <contentIdentifier>%s</contentIdentifier> CHAPTER 10 ■ DISTRIBUTED APPLICATIONS 251 7575Ch10.qxp 4/27/07 1:06 PM Page 251 <locale xmlns=""urn:mtpg-com:mtps/2004/1/key"">en-us</locale> <version xmlns=""urn:mtpg-com:mtps/2004/1/key"">VS.80</version> <requestedDocuments> <requestedDocument type=""common"" selector=""Mtps.Search"" /> <requestedDocument type=""primary"" selector=""Mtps.Xhtml"" /> </requestedDocuments> </getContentRequest> </soap:Body> </soap:Envelope>" let url = "http://services.msdn.microsoft.com" + "/ContentServices/ContentService.asmx" let xpath = "/soap:Envelope/soap:Body/c:getContentResponse/" + "mtps:primaryDocuments/p:primary" let queryMsdn item = let request = Printf.sprintf requestTemplate item let xml = getWebService url "GetContent" request let namespaceManage = let temp = new XmlNamespaceManager(xml.NameTable) temp.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/") temp.AddNamespace("mtps", "urn:msdn-com:public-content-syndication") temp.AddNamespace("c", "urn:msdn-com:public-content-syndication") temp.AddNamespace("p", "urn:mtpg-com:mtps/2004/1/primary") temp match xml.SelectSingleNode(xpath, namespaceManage) with | null -> print_endline "Not found" | html -> print_endline html.InnerText queryMsdn "System.IO.StreamWriter" Running the code in Listing 10-4 queries MSDN to find out about the System.IO. StreamWriter class . F igur e 10-2 shows the results of such a query, which is run inside F# interactive hosted in Visual Studio. It’s easy to define other queries to the web service— just call queryMsdn, passing it a different string parameter. Although the r esults of this web service can appear poorly formatted, since the body text you grab is HTML and you simply strip the formatting tags, I often find this is the quickest way to search for information on MSDN. If I know that I’m going to be searching MSDN a lot, I load this scr ipt into fsi hosted in Visual Studio, and then I can query MSDN just by typing queryMsdn, which can be much quicker than loading a browser. CHAPTER 10 ■ DISTRIBUTED APPLICATIONS 252 7575Ch10.qxp 4/27/07 1:06 PM Page 252 Figure 10-2. Querying MSDN in Visual Studio This method of calling web services has its advocates who claim it’s more resistant to changes in the service interface than generated proxies. However, this example is flawed for at least two reasons. It’s not typically a good idea to place a large quantity of string data in source code, as you did with the requestTemplate identifier in Listing 10-4, and it’s often easier to work with strongly typed objects rather than querying an XML document. To explore the alternatives, let’s look at an example that queries Google’s web service using a generated proxy. First, you need to generate the proxy; you do this using the wsdl.exe tool. Using wsdl.exe is straightforward. Just pass it the URL of the service you want to use, and wsdl.exe will generate a proxy. So, to create the Google proxy, use the command line wsdl.exe http://strangelights.com/ EvilAPI/GoogleSearch.wsdl . This creates a C# proxy class that can easily be compiled into a .NET assembly and used in F#. CHAPTER 10 ■ DISTRIBUTED APPLICATIONS 253 7575Ch10.qxp 4/27/07 1:06 PM Page 253 ■Note You will have noticed that the Google search is hosted on http://www.strangelights.com,my web site. This provides a copy of Google’s old web service API implemented by screen scraping the results from Google. This idea was copied from EvilAPI.com, implemented originally by Collin Winter, in response to Google’s decision to discontinue its SOAP API. Many companies use web services internally as a way of let- ting teams throughout the company, or partner companies, cooperate more easily. The services provided by Amazon.com and eBay.com are good examples of this but were not suitable for use in this example because they require a long sign-up process. The huge advantage of using a proxy is that once the proxy has been created, there is very little plumbing to do. It’s simply a matter of creating an instance of the proxy class and calling the service’s methods. This is illustrated in the following example (Listing 10-5) where you query Google for the first three pages of F#. Creating an instance of the GoogleSearchService class and calling its doGoogleSearch method is straightforward, and processing the result is straightforward since it’s available in a strongly typed class. Listing 10-5. Calling the Google Web Service #light #r "Proxy.dll";; let key = "xxxx xxxx xxxx xxxx" let google = new GoogleSearchService(Url = "http://strangelights.com/EvilAPI/google.asmx") let result = google.doGoogleSearch(key=key, q="FSharp", start=0, maxResults=3, filter=false, restrict="", safeSearch=false, lr="", ie="", oe="") CHAPTER 10 ■ DISTRIBUTED APPLICATIONS 254 7575Ch10.qxp 4/27/07 1:06 PM Page 254 result.resultElements |> Array.iteri (fun i result -> printf "%i. %s\r\n%s\r\n%s\r\n\r\n" i result.title result.URL result.snippet) read_line() |> ignore The results of this example, when executed (on the day of this writing), are as follows: 0. <b>F#</b> http://research.microsoft.com/fsharp/fsharp.aspx A .NET variant of ML with a core language similar to that of the OCaml programmi ng <br> language. 1. <b>f#</b> language http://research.microsoft.com/fsharp/fsharp.aspx <b>F#</b> is a programming language that provides the much sought-after <b> </ b> The only <br> language to provide a combination like this is <b>F#</b> (pron ounced FSharp) - a <b> </b> 2. F Sharp programming language - Wikipedia, the free encyclopedia http://en.wikipedia.org/wiki/F_Sharp The correct title of this article is <b>F#</b> programming language. <b> </b> NET components <br> in <b>F#</b>. Consequently, the main <b>F#</b> libraries ar e the . <b> </b> Creating Web Services In addition to calling web services, you can create web services in F#, and this is also very straightforward. In fact, when creating a web service, the main problem is probably exposing code thr ough a web server. Web servers receive requests for files, in the form of a URL; you must tell the web server to which .NET class this request will map. Typically you use an .asmx file to run a specific F# class that will responded to the web service request if the web server gets a r equest for the .asmx file . The exact way of doing this varies depending on your develop- ment environment and the w eb server on which you host your services. V isual S tudio 2005 comes with a built-in web ser v er , so creating a new web site is just a matter of selecting File ➤ New ➤ Web Site and then choosing the location for the website. CHAPTER 10 ■ DISTRIBUTED APPLICATIONS 255 7575Ch10.qxp 4/27/07 1:06 PM Page 255 [...]... Combination, which consists of a list of tuples made up of float and LineDefinitions; remember, type definitions can be recursive float gives a weight to the line, allowing the programmer to specify how much of this section should appear LineDefinitions allows the user to specify sections that consist of a list of points or sections that consist of functions The definition of the type is as follows:... this kind of development is its union type Because you can use this type to represent items that are related yet do not share the same structure, it is great for representing languages The following example shows the abstract syntax tree: type Expr = | Ident of string | Val of System.Double | Multi of Expr * Expr | Div of Expr * Expr | Plus of Expr * Expr | Minus of Expr * Expr The tree consists of just... you do the work of calculating the real y values; you extract all the y values from the list of points within the list ptsl and create a vector of these lists of y values using the combinel function you have defined and the Vector .of_ list function Then you use the Vectors module’s dot function to scale each of the resulting vectors by the vector weights After this, it is just a matter of combining the... you use to describe the path of a line that will be plotted on the graph You want to allow three types of line: one defined by a list of x and y coordinates, one defined by a function, and one defined by a combination of functions and a list of points Languages are usually described formally by listing the valid constructs of the language, that is, the valid syntactic ways of constructing phrases in... perfect way to model the list of different possibilities, and thus a direct transcription from the formal definition of the language into code is often possible In this case, you define a type LineDefinition that consists of three possibilities—the three possible phrases in the language The first phrase is Points, an array of type Point The second is Function, which consists of a function that takes a... This allows you to specify a range of x values for a line definition and then calculate a list of points consisting of x, y values This function actually does the work of turning the definition in your language into points that you can use to draw a graph The sample function definition is shown next The first two cases are fairly straightforward If you have a list of points for each x value, you use... example, just returning a static unchanging XML document, is not particularly realistic, it is easy to see the potential of this sort of application Instead of a GetYeastMolecule method, you’d more realistically provide a GetMolecule method that took a name of a molecule, looked the details of the molecule up in a database, and returned the resulting molecule data The advantage is that a program running... array | Function of (float -> float) | Combination of (float * LineDefinition) list // Derived construction function 7575Ch11.qxp 4/27/07 1:07 PM Page 275 CHAPTER 11 I LANGUAGE-ORIENTED PROGRAMMING let PointList pts = Points(pts |> Array .of_ list |> Array.map (fun (x,y) -> {X=x;Y=y})) module LineFunctions = begin // Helper function to take a list of sequences and return a sequence of lists // where... operandsStack.Push(x) | _ -> 7575Ch11.qxp 4/27/07 1:07 PM Page 2 79 CHAPTER 11 I LANGUAGE-ORIENTED PROGRAMMING If it isn’t an integer, you check whether it is of several other types Listing 11-2 shows the full example Listing 11-2 Stack-Based Evaluation of F# Quoted Arithmetic Expressions #light open System.Collections.Generic open Microsoft.FSharp.Quotations open Microsoft.FSharp.Quotations.Typed let interpret exp =... LANGUAGE-ORIENTED PROGRAMMING type LineDefinition = | Points of Point array | Function of (float -> float) | Combination of (float * LineDefinition) list Simply knowing the path of a line doesn’t give you enough information to be able to draw it You also need to know the color, the width, and other attributes Fortunately, a simple way to provide this sort of information is the System.Drawing.Pen class, which . ignore The results of this example, when executed (on the day of this writing), are as follows: 0. <b> ;F#& lt;/b> http://research.microsoft.com/fsharp/fsharp.aspx A .NET variant of ML with a. potential of this sort of application. I nstead of a GetYeastMolecule method, you’d more realistically provide a GetMolecule method that took a name of a molecule, looked the details of the molecule. APPLICATIONS 2 49 7575Ch10.qxp 4/27/07 1:06 PM Page 2 49 proc.StartInfo.UseShellExecute <- true proc.StartInfo.FileName <- newUrl proc.Start() The results of this example at the time of writing