1 / 35

Object Oriented Analysis and Design

Object Oriented Analysis and Design. Networking. Contents. Network concepts. Network Concepts. Networks connect computers Each computer has a unique address MAC Address on network card IP Address assigned by router The Domain Name System Translates host names into IP addresses

nakia
Télécharger la présentation

Object Oriented Analysis and Design

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. Object Oriented Analysis and Design Networking

  2. Contents • Network concepts

  3. Network Concepts • Networks connect computers • Each computer has a unique address • MAC Address on network card • IP Address assigned by router • The Domain Name System • Translates host names into IP addresses • Networks transmit bytes • They neither know nor care what they bytes mean

  4. Connectionless Protocol • UDP send datagrams containing • The address to send to • The data to send • There is no guarantee a datagram will be received • Datagrams are • Efficient • Unreliable • Useful for streaming media

  5. Connection Oriented Protocols • TCP is a connection oriented protocol which sets up a virtual circuit • TCP adds controls to ensure that every packet is delivered correctly and in order • TCP is • Slower than UDP • Reliable • Used for communication where data cannot be lost

  6. Ports • Every computer has an address to which data can be sent • How do you know which program should receive the data? • Each computer has 65535 ports • Every network connection is assigned a port number • This let’s many programs share the same internet address and not get the data mixed up

  7. Sockets • A socket represents • One end of an internet connection • A socket consists of • An IP address for the computer • A port representing the program sending or receiving the data

  8. Servers • Internet servers • Listen for connections from clients • Provide data and / or services to the clients • A typical serial server • Listens on a known port • Waits for a client to connect • Creates a new client socket to represent the connection • Exchanges data with the client • Closes the client socket • Returns to listen for new connections

  9. Concurrent Servers • Sometimes new requests arrive before the first request has been handled • The operating maintains a short queue • When that is full, requests are refused • The problem is the server is handling requests rather than listening for connections • A concurrent server • Listens for connections • Creates a socket for the new connection • Spawns a thread to communicate with the client • Returns to listening for connections

  10. SDL_net • A set of networking functions for SDL • Download from libsdl.org/projects • Copy header files to SDL include directory • Copy .lib and .dll files to SDL lib directory • These are lower-level networking classes • We will use a set of classes built from the lower-level networking functions

  11. CNetMessage • A buffer of 256 characters which can be used to exchange data between a client and server class CNetMessage { public: CNetMessage(); //constructor virtual intNumToLoad(); // number of bytes left in buffer virtual intNumToUnLoad(); // number of bytes in buffer to be sent void LoadBytes(charbuf& inputbuffer, int n); //Load a char set into the message buffer void UnLoadBytes(charbuf& destbuffer); //Unload a char set from the buffer void finish(); //set the state object to full. No more data to be loaded };

  12. CIpAddress • Represents an internet address as a combination of an IP address and a port number class CIpAddress { public: CIpAddress(); //constructor CIpAddress (Uint16 port); //create and associate a port to the instance CIpAddress (std::string host, Uint16 port); //create and associate a port and a host //to the instance void SetIp (IPaddresssdl_ip); //set a CIpAddress object from an //existing SDL IPaddress bool Ok() const; //True if the object has a port and a host associated IPaddressGetIpAddress() const; //return a SDL_netIPaddress structure Uint32 GetHost() const; //return the host Uint16 GetPort() const; //return the port friend std::ostream& operator<<(std::ostream &os, CIpAddress const adr); };

  13. CHostSocket • Represents a server socket class CHostSocket : public CTcpSocket { public: CHostSocket (CIpAddress& the_ip_address); //create and open a new socket, //with an existing CIpAddressobject CHostSocket (Uint16 port); //create and open a new socket with the desired port bool Accept (CClientSocket&); //set a client CTcpSocket object after listening to // the port virtual void OnReady(); //pure virtual };

  14. CClientSocket • Used to create a client to connect to a server class CClientSocket : public CTcpSocket { public: CClientSocket(); //constructor CClientSocket (std::string host, Uint16 port); //Create the object and connect to a // host, in a given port bool Connect (CIpAddress& remoteip); //make a connection to communicate // with a remote host bool Connect (CHostSocket& the_listener_socket); //make a connection to // communicate with a client void SetSocket (TCPsocketthe_sdl_socket); //set a CTcpSocket object from an // existing SDL_net socket CIpAddressgetIpAddress () const; //return a CIpAddress object associated //to the remote host virtual void OnReady(); //pure virtual bool Receive(CNetMessage& rData); //receive data into a CNetMessage object bool Send (CNetMessage& sData); //send data from a CNetMessage object };

  15. Exchanging Data Across a Network • Networks deliver streams of bytes • We seldom want to deliver streams of bytes • We want to deliver • Integers, Strings, floats • Images, objects, structures, requests • This raises the question of how do we • Translate complex data structures to bytes • Translate the resulting stream of bytes back into the original data structures

  16. Serialization • Serialization is the process of transforming a series of complex connected objects into a stream of bytes • De-serialization transforms the stream of bytes into the original objects

  17. Challenges in Serialization • The problems are: • How to get a binary representation of everything • How to only encode each object once • How to store the format so it can be decoded

  18. Data Representations for Serialization • You could send everything as strings, but • It takes time to convert to / from strings • The string representation can be larger than the binary • This makes using strings slow • To get the binary representation • Get the address of what you want • Get the length of it in bytes using sizeof or strlen • Use memcpy to copy it to a byte buffer

  19. Network Protocols • A protocol is a pre-defined way for two entities to interact • For networking software a protocol defines • The messages which will be exchanged • The data representations which will be sent • Protocols range from the very simple to highly complex

  20. A Simple Command / Parameter Protocol • This can be made as a very simple binary protocol • Every request / response consists of • 1 byte identifying the type of the request or response • 4 bytes containing and integer or float • The type could be • Get value of specific variable • Set value of specific variable • Return value of requested variable • Perform some action on foreign machine

  21. A Simple Command / Parameter Protocol • This protocol has numerous drawbacks • There are a limited number of request/ response types • The number and type of parameter is fixed • Since pure binary is transferred, both machines must have the same number representation • Strings cannot be transferred • Adding new requests / responses requires that they programs at both ends be modified • It does have the advantages • Simple to implement • Short requests and responses

  22. Remote Procedure Calls • A remote procedure call allows • A method on one computer to call a method on another computer • Parameters to be passed with the method call • A result to be returned from the method • Remote procedure calls must • Be able to identify objects on a remote computer • Be able to invoke methods on these objects movePlayer( X, Y, Z)

  23. Remote References • To call a method on an object on a computer we need: • The address of the computer • The address of the object on the foreign computer • One way to represent this would be to create class RemoteReference { private: IPAddressremoteHost; unsigned intremoteAddress; ... }

  24. Using Remote References • To invoke a method with a remote reference we must • Get the remote reference • Add the name of the method • Add all of the parameters required • Send this to a class which knows how to send it across the net • The receiving end must • Unpack the request • Find the local object to call • Somehow call the method and pass it the parameters • Package the return value and send it back to the calling method

  25. The Trouble with Remote References • All of the data must be prepared • It must be passed to a class which can send it across the net • The result must be unpacked • This is • Not a natural way to call a method • Requires that you know how to package data for the network • Forces you to write extra code every time you want to call a remote method

  26. The Proxy Approach • A proxy is • Something which looks like the original • But acts slightly differently • A proxy class • Has the same methods as the original class • But the methods • Package the parameters for the net • Use a remote reference to call the remote method • Receive and unpack the return value • Calling a method on a remote proxy is just like calling a method on a local object

  27. The Proxy Approach 4. Proxy method receives call Computer 1 Computer 2 Program Program Internet Obj Obj Listen 1. Object calls proxy method 2. Proxy transmits call across internet 3. Listener receives call and makes local call to real object

  28. Data Serialization • The difficulty with serialization is that the receiver does not know what type of data is being received • If just data is being transmitted, the receiver needs to know • The type of the data • The definition of that data type • If a method is being called, the receiver must know • The name of the method • The types of all the parameters • The number of parameters • The definition of the types of all parameters

  29. Data Serialization • This means the receiver must have • The definition of all classes on which methods can be called • The definition of all methods which can be called • The definition of all data types which can be exchanged • We need a way to represent these definitions so that • They can be distributed to various computers • They can be sent across the net if necessary • They are independent of any computer language • They can be easily edited • Software is readily available to read the format

  30. Data Type Representation • One of the better solutions is XML • We can easily create an XML notation to represent • A class, its data members and methods • Primitive data types • For example, we could represent primitives as <int name=‘count’ value=‘42’ /> <string name=‘address’ value=‘unknown’ /> • We could easily add all the primitives • Name and value attributes could be optional if not needed in all cases

  31. Class Representation Representing classes is more difficult than representing primitives <class name=“Weapon” > <int name=“lethality” init=‘0’ /> <string name=‘description’ init=‘””’ /> <method name=‘setName’ return=‘void’ > <param name=‘name’ type=‘string’ /> </method> </class> • Representing classes as XML can get surprisingly complicated

  32. Transmitting Serialized Data Request sent over network Used for primitives and classes Type Name Value 1 Value 2 ... Value n Type name as a text string Binary values for the type @ARRAY Type Name length Value 1 Value 2 ... Value n Indicatesarray Type ofthe array length ofthe array Used for arrays, Including strings

  33. Data Which Cannot be Serialized • File pointers • I/O streams • Local pointers and references • Any data which has to be calculated when a class is constructed

  34. Requirements for Deserializability • Any class which can be serialized / deserialized must • Implement the Serializable interface • Provide a parameterless constructor • Provide public get and set methods following the pattern • void setVariableName(type variableValue) • variableTypegetVariableName()

  35. Automating Serialization & RPC • You can write programs to: • Read the XML data representations • Write methods (as C++ code) to serialize and deserialize the class • Write code to act as internet proxies to make remote method calls • Once these programs write the code • It can be compiled • It can be incorporated into your program

More Related