1 / 37

Distributed Java

Distributed Java. Notes Dr. Raed Alqadi. TCP for Client/Server . Client "knows" a particular type of service that exists at given address and port   - E.g., at www.microsoft.com. port 80, there is a web server Client: Connects to well known address & port Sends request to server

toshi
Télécharger la présentation

Distributed Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Distributed Java Notes Dr. RaedAlqadi

  2. TCP for Client/Server • Client "knows" a particular type of service that exists at given address and port   • - E.g., at www.microsoft.com.port 80, there is a web server • Client: • Connects to well known address & port • Sends request to server • Receives Response from Server • The client must know the particular type of service that exists at a particular address and port. For example, to make a request from Miirosoft'sweb server, you .must know that the web servverexist at www.microsoft.com port 80 and that there is a web or HTTP server there that you can connect and send a request to through an outgoing stream. It will then respond back through the input stream.

  3. Different TCP Protocols • Many different socket-based protocols made popular: • FTP for downloading & uploading potentially many files, simultaneously • HTTP for downloading single file, very quickly • Each protocol is designed for efficiency • All can ·fit the same basic request/response ceremony • The specifics of the hand shaking and how it is accomplished are what is known s the protocol. There are many different socket-based protocols that have been made popular, such as FTP and HTTP. Each protocol is designed for a particular type of efficiency-for example, FTP for the efficient downloading of simultaneously multiple files, while HTTP for the efficient display to the client. Though each is different based on what its designed goal is, but all can fit the same basic request/response ceremony.

  4. URLs for Addressing • Many different protocols provide resources on request • A resource is a stream of data, like r/o file • A Universal Resource Locator is a generic formalism to refer to resources. • A URL is a string that includes • A protocol, server address, port, and sufficient info to make a request • The response is the "resource'" of the URL • URLs or Universal Resource Locators are for addressing. Many protocols provide resource on request. A resource is a stream of data, like a read-only file. • URL is a generic formalism to refer to downloadable resources. For example, there is a part G'f FTP that allows you to download a file upon request. That downloadable file would be a resource, reachable by FTP. • FTP also supports uploading a file resource that a client can read. They are not : things that a client gives out. • A URL is a string that includes: • A protocol- that is a way to request something. • A server address - that is where this thing is located. • A port. • Sufficient information to make a request.  

  5. Parts of a URL • Example : http://www.yahoo.com:80/index.htm • Protocol: format of request and response • Address: where the server app is running • Port: often can be assumed • E.g., HTTP protocol is usually so • Request data: Interpreted differently by different protocols . • often just a file path and name • May include arbitrary additional info • The followings are Part of the URL • Protocol - which is the part in the beginning of the URL; the format of the request and response. The name of the protocol is terminated with a colon. • Address of the server - where the application is running, e.g. yahoo.com. • Port - sometimes assumed. • 4.Request data - piece of data that can be turned into a valid

  6. The URL Class • In Java code, a URL object is made from a string URL, can download response., • Built from a string …. • URL url = new URL(http://www_''); II e,g, • Also other ways to build • URL object methods give info about URL • Protocol, port, server, path, filename, arid "ref' (or bookmark name) • In Java, we represent URL's with a URL object, which is the java.net.URLclass. A URL object is made from a URL string. It gives information about the URL, such as' the protocol, port, server name, path, filename, as w'e1l as other information that is buried inside of that UI\L string. Also, it can be used to contact and get the response back from the server.

  7. Downloading Content • Cooler than parsing: downloading • URL.getContent() will : • Contact server • -Do handshaking • -Request resource • Get response • -Try to turn response into equivalent Java object • e.g. GIF file into an Image object • A better capability that this URL class has than simply parsing the string is the ability to actually contact and download the server. • The URL.getContent() method will perform the following: • It will contact the server for us. The server that is indicated in the URL. • It will do handshaking with that server, establishing a valid communication using the protocol that is indicated in the URL object itself. • It will request the indicated resource. • It will get whatever response comes back. • It attempts to turn the response into an equivalent or useful Java object.

  8. Raw and Processed Content • getContent() attempts to process content • Turn it i.nto equivalent Java type • openStream() will cause URL to make request, but not to interpret • Will return an InputStream so you can read raw content • The URL's getContent() method attempts to process content and turn it into an equivalent Java type. • Another method called openStream( ) causes the URL to make the request to the server and to do the handshaking with the server, but" not to interpret the response that comes back, but instead the URL will give you direct access to the inputStreamthat is returned so that you can process it yourself.

  9. The Two Benefits of URL • First, handshaking handled for us • -A protocol handler defined for protocol • -We don't need to know specifics of protocol • Second, content handled for us • -Don't have to know specifics of file formats • E.g., GIF files automatically interpreted • Performed by a content handler • The protocol Handler has 2 distinct benefits: • The protocol handler handles all of the handshaking for the protocol for us. • The content handler automatically converts the content to the correct objects. • Note: In the cases where you want to invent a brand new protocol or a brand new. type of content, there are many protocol handlers that are given to us as part of the Core API, such as the HTTP and the FTP protocols. • You can also create your own protocol handler once an~ have it registered on the local virtual machine-allowing you to start using that protocol in URLs. You don't have to modify the URL class or do anything other than that. It is a very modular system allowing you to add and update both protocol and content handlers dynamically.

  10. Disadvantage • Many protocols ·can handle much more complex transactions • E.g., FTP allows us to downl.oad multiple files • Generic URL fonnat too simple for more complex transactions, • Can't do them without knowledge of protocol • There are many protocols which can handle much more complex transactions than are represented by URLs. For example, FTP allows us to download multiple files. A generic URL format is extremely simple for the more complex transactions.

  11. URL: Simple Transactions • The generic URL model is for simple transactions • -Single-line request, stream response • Many protocols support much more complex' transactions • E.g., FTP supports downloading multiple files simultaneously • -E.g., HTIP supports sending several, very complex contextual parameters with a req • URlobjects are meant to represent a single request for a single resource . For example, simple single or multi-line request, as long as the request and response are clearly delimited and does not require a vast amount of interaction between the two systems. } • There are many protocols that support much more complex transaction such as FTP or HTTP. FTP supports downloading multiple files simultaneously and HTTP supports sending several, very complex request, • An HTTP POST transaction is a way of sending large piece of data off with you HTTP Request to a web server. This type of data is usually used when you fill out a form and hit the submit buttori on your browser.

  12. HTTP Requests • HTTP Requests made up of two parts • -Header: Contextual information - client auth, browser type, document requested, etc. • -Body: an arbitrary chunk of data sent with the request, e.g. form data POST / HTTP/1.1 Accept: */* Accept-language: en-us User-agent: Mozilla/4.0 <blank-line:> <empty-body> • HTTP Request is made up of two parts: • A header: contains contextual information about the request, such as a client authentication, the browser type, which document is requested, etc. • A body: contains large piece of data sent with a request, such as a form data.

  13. Request Type • The GET request is the simplest - just name the resource you 'want returned • URL objects represent GET requests • URL length is limited • More complex request types exist: • POST: arbitrarily long body • HEAD: Requesting meta-data about a response • Etc. • The GET request is the simplest of all request types. Basically you just type the word GET and the name of the resource that you want returned. A URL object normally represents a GET request or an HTTP URL object. The length of the actual request is limited. • Complex request types do exist if you need to request a more complex data back. For example, an HTTP POST request for a long body, or a head request for a meta­data about a resource.

  14. Response Format • As request, response has Head and Body • Head: meta-data about the response • Length :MIME-type, etc. • OR, error message (e.g., "no such resource") • Body: the content of the resource . - As a stream of bytes • - URL.openStream( ) returns an InputStream • whose contents is the Body • An HTTP Response format responds as a header and a body. A header has meta­data about the response, such as its length, its MIME-Type, or an error message, etc. It. can be very important because they ~an contain extremely important information, such as client or server authentication information. A body is the actual conteni or resource sent as a stream of bytes . • The API for the URL class does not allow the access to the header information. Therefore you have to use the URLConnectionclass to obtain those headers.  

  15. A URLConnection Object • Provides fine-grained control over request/response • Lets you ...   • Modify request - POST instead of GET, set particular headers, etc. • -Examine exact response - examine headers, get direct access to content bytes • A URLConnection object is essentially a URL request/response control .mechanism that provides fine-grained cont~ol over the request and the response that comes from the server.   • URLConnection object allows us to both modify the request and examine the exact response when requesting a particular resource from the server. It also allow~ us to interact with the server such as sending and receiving streams of-bytes many times during a single connection.

  16. URLConnection Lifetime • URLConnection has 2-phase lifetime • 1) Before connection is. made : • - Set request headers • 2) During Request/Response : - Interact with the server - send request data - Reader server's intermediate replies • Note: a very general view of a client/server interaction • A URLConnection object has two-phase lifetime: • Before connection is made -- where a URLConnection object is made, but there has been no attempt to contact the actual server that the connection object represents. At this point, you can modify the request. • During Request/Response - where at this point you can interact with the server, such as send POST data and tack data onto the end of the request. You can also read the server's immediate reply once you had sent all your request across . • Note: Just like the URL object provides us an extremely constrained view of what , a client/server interaction was, URLConnection tried to provide us a somewhat . wider view but still a very generic view of what an interaction is.

  17. Creating URLConnection • Created by a URL object • -See URL.openConnection( ) • In fact, created by URLStreamHandlerFactory • URL object asks factory for a URLConnection . . • -Factory finds URLStreamHandler for the given protocol • To create a URLConnection object, you start by creating a URL object or at least the basis of the request. For example, a URL object must include at least the protocol and the server you are going to connect to. Once you have made that URL, you call the URL.openConnection() method, and this going to return to you a URLConnection that represents ? connection to that server. That URLConnection is able to handle some of the mundane detail of the actual protocol. • You do not create a URLConnectionobject using a constructor yourself, you just use a URL. The URL object creates the URLConnection by calling the URLStreamHandlerFactory. The URLStreamHandlerFactory creates the protocol handlers for the particular protocol and the URLConnection uses that handler to talk to that particular server.

  18. Request Model (Headers) • URLConnectionAPI implies a request model- a series of request headers • Different protocols map these headers' • E.g., HTTP is simple, since all requests are a series of request headers • setRequestProperty() sets a header: URL url = new URL(“http://ww.klscorn.com”); URLConnectionurIc = url.openConnection( ); urlc.setRequestProperty("User_agent". "Homegrown browser"); • URLConnection class is based on a particular model of a request, in which a request is basically a series of headers . • To set these request headers and any type of URLConnection, you call the URLConnection object setRequestProperty() method.

  19. Other Request Parameters • A couple of other values you can set before leaving phase 1 • setDoOutput() • Means you expect to open a raw OutputStream to send bytes to server as part of request • setUseCaches( ) • setIfModifiedSince( ) • In addition to request header field, there are a couple of other values you can change on a request before actually connecting to the server. For example, you can call setDoOutput(), which means you are going to be writing extra data along with the request to an OutputStream. The URLConnection object will supply that OutputStream to you. • There are other values you can check or set as part of the request, such as setUsecaches( ) which allows HTTP to be cached, setIfModifiedSince() which allows you to make a request that will not be completed if the particular resource is not changed in a certain amount of time, etc.

  20. Raw Stream to Server • setAllowUserlnteraction(), setDoOuput( ), and getOutputStream( ) combo • -Opens a direct stream to the server • -After all, URLs represent TCP connections ...   • Different protocols interpret stream different - E.g. HTTP: stream used to write request body • To write data to the server as part of a request, you must call three methods:· • setAIlowUserInteraction() -which indicates that you want to write data to  the server. This will open a stream or a socket connection to the server, send off a request header if necessary and gives you access to the OutputStream. • setDoOutput() - which indicates that you want to write output to the server.   • getOutputStream() - which indicates that you are ready to write data to the server. • Note: Different protocols will interpret streams differently.

  21. Connecting • The URLConnection.connect( ) method connects to the server • -Does preliminary handshaking, sending request params (header fields) • Once connected, you may interact with the server if the proto supports • E.g., FTP supports many req/res • HTTP POST supports sending POST data • HTTP GET does not support additional data • The simplest way to connect to the server by the URLConnection.connect( ) method. Once connected you may interact with the server if the protocol supports it.

  22. Response Model • Response model is as simple as request • A set of header fields, and a body stream • -Note how easy HTTP fits, others not so easy • Only when interaction done are header fields available • Large set of getXYZ( ) methods make it easy to putt header fields out • The format of the response model is as simple as a request model. It is a set of header fields and a body stream. Once only after the interaction is completed will the header fields be available for any particular protocol • The response model has a large set of get( ) methods. These can be used to get the response header and the body or the meta-data about the response body.

  23. Response API package java.net; public class URLConnection { intgetContentLength( ); String getContentType( ); long getDate(); II millis since epoch  -- - String getHeaderField(String HeaderName); } • Only available when interaction complete - Very protocol-specific delimitation   • The getContentType() tells you the format of the stream. The getdate() tells you when the resources was originally created, etc. The getHeaderField() is where you can pass a header name. In fact, some of these get methods are really the thin wrappers around the getHeaderField.   • Note: The response data is only available when interaction is complete, although that's a very protocol-specific delimitation.

  24. Raw Response Stream • setAllowUserlnteraction(), setDolnput() and getInputStream( ) combo • Used to get raw input stream from server • •Different protocols map differently • E.g. HTTP: InputStream lets you read response body • To get the response body, we use the setAllowUserlnteraction() which tells the connection object that we are planning on interacting with the server through streams. The setdolnputStream() allows us to get the input stream

  25. Doing HTTP POST • Rather common on the Internet • Availability of HTTP servers, etc. • Content-type request header indica.tes encoding of request data • -Popular: "application/x-www-forrn-urlencoded" • URLEncoder.encode() provided • Write POST data to output stream • <vamame>=<var- value>& <vamame>=<varvalue> • HTTP Post transactions are extremely popular to do on the Internet, especially with the Distributed programming in Java. • HTTP server has become a backbone in a lot of distribution application and is used quite often. • Note: The URLEncoder class was provided as part of the standard for the API to do the encoding for us, e.g. www-form-urlencoded. This will make it easier to do HTTP Post transactions

  26. Doing HTTP POST • Rather common in many apps • Sequence: • Build "http" URL object • setAllowUserInteraction (true) • Set request headers • getOutputStream( ) to write POST data • -getlnputStream() to read raw response • The following DoHTTPPostjava file is written to do the HTTP Post in Java. import java.net. URL; import java.net. U RL Connection; import java.net. URLEncoder; import java.net.MalforrnedURLException; import java.util.Properties; import java.util.Enumeration; import java.io.lnputStream; Import java.io.OutputStream; Import java.io.IOException; Import java.io.FilelnputStream;

  27. Public class DoHTTPPost { public static void main(String[] args) { if(args.length < 2) { System.err. println("USA G E:\n \tDoHTIPPost <url> <properties- file>"); System.exit(1); } String strURL = args[0]; String strPropFilename = args[ 1]; . II First, build URL object URLConnectionurlc = null; try { URL url = new URL(strURL); urlc = url.openConnection(); urlc.setAllowUserInteraction(true); urlc .setDoOutput(true); urlc.setDoInput(true); } catch (MalformedURLExceptionmue) { Sysrem.err.println("URL no good: "+ strURL); System.exit(1); } catch (IOExceptionioe) { System.err.println("Errpr creating URLConnection: " + ioe); System.exit(1); } II Now, open Properties file and read in contents String strPostData = null; try { Properties props = new Properties(); InputStream is = new FilelnputStream(strPropFilename); props.load(is); Enumeration keys = props.keys(); boolean first = true; StringBuffersb = new StringBuffer(); . while(keys.hasMoreElements()) { String key = (String)keys.nextElementO; String v~aue = props.getProperty(key); if(! first) sb.append("&'·); else first = false

  28. sb. append (URLEncoder.encode(key)); sb.append("="); sb.append(URLEncoder.encode( value)); }// while strPostData = sb.toString(); } catch (lOExceptionioe) { System.err.println("Error with props file: " + ioe); System.exit( 1); } II Now we have connection and post data, so try to II do the request and read the response! InputStreamisResponse = null; try { urlc.setRequestProperry("Content-type". "appIication/x-www-form-urlencoded"); OutputStreamos = urlc.getOutputStream(); byte[] data = strPostData.getByte(); os.write( data); isResponse = urlc.getlnputStream(); } catch (IOExceptionioe) { System.err.println("Error communicating with server: .. + ioe); System.exit( 1); } try{ for(intch=0 ; -1 != (ch=isResponse.read()) ; ) System.out. write( ch); } catch (IOExceptionioe) { System.err.println("Error reading response: .. + ioe); System.exit(1); } }// main }//class

  29. URL Object Can Download • Create URL from any protocol • Throws malformedURLExceptionif string format is bad • Also thrown if protocol is unknown myprivateproto:// server/ pathfiIename • Protocol handler is a generic extension mechanism for the URL capabilities of the Java Core API. This allows you to add new recognized protocols into the URL mechanism. You can create a URL object from any protocol, but you have to be careful and prepared to catch any malformedURLExceptionif the: • String URL is bad or incorrect format. • Protocol you have indicated is unknown.

  30. URL Internals • URL Internals • - Performs the handshaking with the server • Can turn pathfilename into a real request the • server will understand • The URL object has an internal reference to the URLStreamHandler object. It is· this handler that performs the handshaking and connection with the server. It can turn the pathfilename into a real request that the server will understand for a real resource. Server InputStream

  31. Finding a Handler • Different handler classes exit, one for each unique protocol • -Not part of the Core API • -Sun's implementation in sub-packages of • sun.net.www.protocol package • •Default, to find handler for "proto"   • Creates class name "sun.net.www.protocol" + ". proto" + ".Handler‘” • For the URL to find a handler for a protocol, it is going to create a class name from the protocol name, prefix it with stin.net.www.protocol and suffix it with the word “Handler" For example, if the protocol's name is proto, it will look like this: sun.net. www.protocol.proto.Handler.

  32. sun.net.www.protocol • Where does package prefix come from? • System property • - "java.protocol.handler.pkgs" • - Value is a "I" -separated list of package prefixes • - If not set, defaults to "sun.net.www.protocol" • -"sun.net.www.protocol" always appended to the list • There is a system property that you can set called java.protocol.handler.pkgs. This is a pipe-separated list of package prefixes. By default if this value is not set, it will assume that it is "sun.net.www.protocol," which is the Sun's way of making sure that their implementation get used and nobody else's. But if you want you can set that property to something else. • Even if you do that, the "sun.net.www.protocol" is always appended to the end of this pipe-separated list. So if you set your own package name, such as brian.maso, what the system internally will actually see is brian.masolsun.net1www.protocol.

  33. Finding a Handler (Do-over) • • Given protocol- "proto" ... • Get java.protocol.handler.pkgs" -or default • Add to package prefix ".proto" + ".Handler" • Use class.newlnstance( ) to create a new andlerinstance - Pooled for efficiency   • Handler class must extend java.net. URLStreamHandler • Common superclass for all handlers   • To find a handler, the protocol will look for java.protocol.handler.pkgssystem property. If that is set, it will use that value. If not, it uses the default "sun.net.www.protocol." Given the protocol name "proto", it will add that name ".proto" and ".Handler". Then it will use the class.newlnstance() method to create a new handler instance. If it is unable to load that class or create an instance, it will cycle through to the next package named in that system property. If it really cannot find a protocol handler, it throws a malformedURLExceptionindicating that it is an unknown protocol. • Note: All handler classes have the exact same name, which is "Handler" (with al uppercase H). The rule for the handler class is that it must extend the java.net.URLStreamHandlerclass. This is the common superclass for all handlers.  

  34. URLStreamHandler Class • One instance at a"time • Made by URL class - Pooled   • Each instance will be asked to … • Parse URLs • Create a URLConnection for given URLs • Only one instance of a particular class is ever created at a time. Once that first one is made, it is always remembered. It is pooled so that there is only one handler of anyparticular protocol present in the virtual machine at a time. - • The following are the two jobs oflJRLStreamHandler class: • To parse a URL. • To create a URLConnection object.

  35. Protocol Handlers • URLStreamHandler Class package java.net: public class URLStreamHandler { protected void parseURL( tJRLurl, String strURL, intstart,int limit; protected void setURL(URL url, String protocol, String hOS, int port, String file, String ref): protected abstract URLConnection openConnection( URL url) throws IOException: } • A "Time" Handler We want to be able to write URLs of the format time://server:port/ - Note: anything after server name and port can be ignored by our handler • Three things in handler - parseURL() (uses setURL()) - openConnection( ) - where socket is made - toExternalForm( ) - recreate URL stages

  36. With one of the TextTimeServers running, we want to be able to write the URL of his format: Time://server: port .. And have that automatically be understood. Note: For our URLs, anything after the port, we really do not care about. For the protocol handler, we need to create a subclass of the URLStreamHandler lass. The following are three things in handler. • parseURL() - to parse out the URLStream and take all the data and put it into a URL object. • openConnection() - to create a URLConnection object. • toExternaIForm() - to take a URL object and put it back into a parse-able string. Note: Remember that the Handler implementation class must be in a package that has at least 3 different parts to it-a package prefiX (e.g. brian.maso), a protocol name (e.g. time), a class (i.e. Handler). The following is the Handler.java code in brian.maso.time package: Import java.net. U RLS treamHandler; import java.nee. URL; Import java.net.URLCounection; import java.io.IO Exception; public class Handler extends URLStreamHandler { /* * Instance of this class are created using * Class.newlnstanceO, which means we need to * ensure a public contructor is available . */ public HandlerO() { }

  37. /** parseUR() is called by a newly created URL object to set the URL object's fields from values contained in a srring. Lucky for us we can just use the inherited implementation. which assumes URLs are of the normal form "f/server:portlpathfilename" (as opposed to wierd URL types like "mailto") . **/ protected void parseURL(URL url, String srrURL, int start, im limit) { super.parseURL(url, srrURL, start, limit); } /** Output the URL in its humand-readable format, i.e .. the format users would type the URL into .a browser originally. We can just use the inherited implementation. since our URLs are of a "normal" format **/ protected String toExtemalForm(URL url) { return super.toExtemaIForm(url); } /** Create a new URLConnection that can make a request of the TimeTExtServer. Send class, TimeTextURLConnection, is used as the connection class. URLConnection should be in first (i.e., unconnected) phase of its lifetime. **/ protected URLConnectionopenConnection(URL url) throws IOException { return new TimeTextURLConnection(url, url.getHostO, url.gtPortO); } }

More Related