Composite Message Pattern


Published on

This pattern uses when marshal and unmarshal message.
if you will get the internal of marshaling/unmarshaling.
you see this paper.

1 Comment
  • this presentation is made by powerpoint 2007.

    so this file is broken.

    if you want to see the perfact page. you must download this file.

    thaks a lot ^^
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Composite Message Pattern

  1. 1. YoungSu, Son [email_address] Microsoft MVP EvaCast Leader Devpia Architecture Sysop EvaCast ( )
  2. 2. Motivation <ul><li>Composite Message Pattern </li></ul><ul><ul><li>Focus on organizing the communication infrastructure used by components. </li></ul></ul><ul><ul><ul><li>Communication overhead is minimized. </li></ul></ul></ul><ul><ul><ul><li>Be Easy added or removed with minimal change. </li></ul></ul></ul><ul><ul><li>When use marshaling / un-marshaling between components </li></ul></ul>
  3. 3. Motivation (cont’d) Data Layer Composite Message Messages UnMarshaling Marshaling Layer Decomposite Message Dispatching
  4. 4. Motivation (cont’d)
  5. 5. Seed Paper <ul><li>Composite Messages : </li></ul><ul><li>A Structural Pattern for Communication </li></ul><ul><li>between Components, </li></ul><ul><li>Aamod Sane, Roy Campbell, </li></ul><ul><li>In OOPSLA’ 95 Workshop on Design Patterns </li></ul><ul><li>for Concurrent, Distributed, and Parallel Object-Oriented Systems, 1995.” </li></ul>
  6. 6. Agenda <ul><li>Background Information </li></ul><ul><li>Problems </li></ul><ul><li>Solution </li></ul><ul><li>Applicability </li></ul><ul><li>Diagram (Static / Dynamic) </li></ul><ul><li>Benefits and Drawbacks </li></ul><ul><li>Implementation </li></ul><ul><li>Sample Code and Usage </li></ul><ul><li>Known Use </li></ul><ul><li>Variations </li></ul><ul><li>Related Patterns </li></ul>
  7. 8. Composite Pattern
  8. 9. Factory Method Pattern
  9. 10. Refactoring <ul><li>Introduce Parameter Object </li></ul><ul><ul><li>You have a group of parameters </li></ul></ul><ul><ul><li>that naturally go together . </li></ul></ul>
  10. 11. Refactoring <ul><li>Preserve Whole Object </li></ul><ul><ul><li>You are getting several values from an objec t and passing these values as parameters in a method call. </li></ul></ul>int low = daysTempRange().getLow(); int high = daysTempRange().getHigh(); withinPlan = plan,withinRange( low,high ); withinPlan = plan,withinRange( daysTempRange() );
  11. 13. Networked System <ul><li>Forces </li></ul><ul><ul><li>Reflexibility </li></ul></ul><ul><ul><ul><li>Packet Layout must be immune to addition and removal of layers . </li></ul></ul></ul><ul><ul><li>Message Composition/Decomposition </li></ul></ul><ul><ul><ul><li>Packet Layout has to simplify marshalling, fragmentation, reassembly, and unmarshalling of packets. </li></ul></ul></ul><ul><ul><li>Efficient Memory Management </li></ul></ul><ul><ul><ul><li>Memory Management should be Efficient . </li></ul></ul></ul>
  12. 14. Virtual File System Fault Address Space (Page Table) Read Communicate Communicate Page-Frame Physical Page Disk Block File Contents
  13. 15. Virtual File System (cont’d) <ul><li>If this interaction is implemented using simple method call, </li></ul><ul><ul><li>the methods end up with </li></ul></ul><ul><ul><li>too many parameters and complex control flow . </li></ul></ul><ul><ul><li>Resulting in programs that are brittle and hard to change . </li></ul></ul><ul><li>Thus, Simplify the interaction, </li></ul><ul><li>We have to package all the information . </li></ul><ul><ul><li>Introduce Parameter Object </li></ul></ul><ul><ul><li>Preserve Whole Object </li></ul></ul>
  14. 16. Virtual File System (cont’d) <ul><li>Forces </li></ul><ul><ul><li>Efficient Memory Management. </li></ul></ul><ul><ul><ul><li>Memory Management for communicate data </li></ul></ul></ul><ul><ul><ul><li> should be simple and efficient </li></ul></ul></ul><ul><ul><li>Reflexibility </li></ul></ul><ul><ul><ul><li>If new components are added or exiting one removed. </li></ul></ul></ul><ul><ul><ul><li>Any changes to the set of components should be localized </li></ul></ul></ul>
  15. 17. Distributed Virtual System
  16. 19. Loosely Coupled Component <ul><li>Problem </li></ul><ul><ul><li>Reflexibility </li></ul></ul><ul><ul><li>Message Composition/Decomposition </li></ul></ul><ul><ul><li>Efficient Memory Management </li></ul></ul>
  17. 20. Loosely Coupled Component <ul><li>Solution </li></ul><ul><ul><li>Create a standard interaction protocol . </li></ul></ul><ul><li>Example </li></ul><ul><ul><li>Packet object defines method </li></ul></ul><ul><ul><li>for adding and removing headers and payload, </li></ul></ul><ul><ul><li> query packet size , and so on. </li></ul></ul>
  18. 21. Loosely Coupled Component
  19. 22. Loosely Coupled Component Data Layer pushPacket() calls makeHeader() / marshal() Messages UnMarshaling Marshaling Layer popPacket() calls headerSize() / unmarshal Dispatching
  20. 23. Loosely Coupled System <ul><li>CM helps with Efficient Memory Management </li></ul><ul><ul><li>CM is possible to query </li></ul></ul><ul><ul><li>each networking layer for the size of header. </li></ul></ul><ul><ul><li>CM will add, and allocate the memory all at once . </li></ul></ul><ul><ul><li>Reduces allocation overhead </li></ul></ul><ul><ul><li>as well as allocator heap fragmentation. </li></ul></ul>
  21. 24. Loosely Coupled System <ul><li>Message Composition/Decompositon </li></ul><ul><ul><li>Message Object can easy organize DataSets </li></ul></ul><ul><ul><ul><li>DataSets = header + packet(payload) </li></ul></ul></ul><ul><ul><li>It is easier to fragment the message </li></ul></ul><ul><ul><li>for transmission and to later reassemble it. </li></ul></ul>
  22. 25. Tightly Coupled System <ul><li>Problem </li></ul><ul><ul><li>Avoid many parameters passing </li></ul></ul><ul><ul><li>Complex Control Flow </li></ul></ul>
  23. 26. Tightly Coupled System <ul><li>Solution </li></ul><ul><ul><li>Reify the parameters passed into a single parameter object </li></ul></ul><ul><ul><ul><li>Object is a “Message” specific to the interaction. </li></ul></ul></ul><ul><ul><ul><li>Object provide simple methods </li></ul></ul></ul><ul><ul><ul><li> that support some parameter manipulation (e.g. RangeCheck). </li></ul></ul></ul><ul><ul><li>Simplifies Memory Management </li></ul></ul><ul><ul><ul><li>Just passing a single pointer to the object instead of the raw parameters </li></ul></ul></ul><ul><ul><li>Localizing the parameter passing into a single object </li></ul></ul><ul><ul><ul><li>Makes it easy to assess the effect of adding or removing components. </li></ul></ul></ul>
  24. 28. <ul><li>Static Participants </li></ul><ul><ul><li>Loosely Coupled System </li></ul></ul><ul><ul><li>Tightly Coupled System </li></ul></ul><ul><li>Dynamic Participants </li></ul>
  25. 29. Static Participants #1 - loosely Coupled System
  26. 30. Component <ul><li>Generate a Message </li></ul><ul><li>Add or Remove other DataSets. </li></ul><ul><li>Marshal and fragment a message </li></ul><ul><li>Unmarshal and reassemble message </li></ul>
  27. 31. Message <ul><li>Message is Composite that can contain other DataSets. </li></ul><ul><li>Defines standard method </li></ul><ul><ul><li>add and remove DataSets. </li></ul></ul><ul><ul><li>Determine the sizes. </li></ul></ul><ul><ul><li>Marshalling, unmarshalling, fragmentation, reassembly </li></ul></ul>
  28. 32. DataSet and DataSetInterface <ul><li>DataSet </li></ul><ul><ul><li>Each Layer generate DataSet. </li></ul></ul><ul><ul><li>Real Data DataSet may be added or removed from a Message. </li></ul></ul><ul><li>DataSetInterface </li></ul><ul><ul><li>A Common Interface for DataSet and Message </li></ul></ul>
  29. 33. Static Participants #2 - Tightly Coupled System
  30. 34. Component <ul><li>Generate Parameter Object </li></ul><ul><li>Get and Set Parameter Info through Get()/Set() </li></ul>
  31. 35. Parameter Object <ul><li>Encapsulates Parameters </li></ul><ul><ul><li>Provide high lever access to parameters </li></ul></ul><ul><ul><li>get() and set() functions. </li></ul></ul><ul><li>Know how to marshal() and unmarshal() </li></ul>
  32. 36. Dynamic Participants
  33. 38. Benefits <ul><li>Loosely Coupled System </li></ul><ul><ul><li>Decouples Components by abstraction the communication structure </li></ul></ul><ul><ul><li>Overcome Heterogeneous Environment. </li></ul></ul><ul><ul><ul><li>Define a meta-object protocol between the basic code of a system and the communication meta-system. </li></ul></ul></ul><ul><li>Tightly Coupled System </li></ul><ul><ul><li>ParameterObject localizes components interactions . </li></ul></ul>
  34. 39. Drawbacks <ul><li>The complete standard protocol for Message may be overly general for some application </li></ul><ul><li>ParameterObject entails </li></ul><ul><li>considerable bookkeeping overhead </li></ul>
  35. 40. <ul><li>CM pattern has several distinct implementation </li></ul><ul><ul><li>depending on Nature of System and </li></ul></ul><ul><ul><li>Expected System Performance </li></ul></ul>
  36. 41. Tightly Coupled Components <ul><li>Nature </li></ul><ul><ul><li>Usually interact by parameter passing . </li></ul></ul><ul><ul><li>To avoid long parameter lists, </li></ul></ul><ul><ul><li>We encapsulate the parameters into a ParameterObject. </li></ul></ul><ul><ul><li>Parameter Object may support methods </li></ul></ul><ul><ul><li>that provide a higher level access to parameters. </li></ul></ul><ul><ul><ul><li>Instead of Timestamp value, </li></ul></ul></ul><ul><ul><ul><li>Parameter object support operation such as isMessageOlder(); </li></ul></ul></ul>
  37. 42. Tightly Coupled Components <ul><li>Main Issue </li></ul>
  38. 43. Loosely Coupled Components <ul><li>Nature </li></ul><ul><ul><li>Loosely Coupled Components communicate </li></ul></ul><ul><ul><li>by constructing message that are composite of DataSets. </li></ul></ul><ul><ul><li>To Implement Composite Message Pattern </li></ul></ul><ul><ul><ul><li>Link all DataSet together </li></ul></ul></ul><ul><ul><ul><li>Create a layout suitable for network transmission </li></ul></ul></ul><ul><ul><ul><li>Network Transmission Involves </li></ul></ul></ul><ul><ul><ul><ul><li>Marshaling, Fragmentation, Unmarshaling and Reassembly. </li></ul></ul></ul></ul>
  39. 44. Loosely Coupled Components <ul><li>The following Steps </li></ul><ul><ul><li>Show how to construct a composite message </li></ul></ul><ul><ul><li>suitable for network transmission and reception. </li></ul></ul>
  40. 45. Loosely Coupled Components <ul><li>Step #1 - Composition using Linked List. </li></ul><ul><ul><li>The simple way to implement CM pattern </li></ul></ul><ul><ul><ul><li>links all the data set into a lined list </li></ul></ul></ul><ul><ul><li>If system supports Distributed Object , </li></ul></ul><ul><ul><ul><li>It would be possible to directly send and receive the linked list object. </li></ul></ul></ul>
  41. 46. Loosely Coupled Components <ul><li>Step #2 – Marshaling with minimal memory usage. </li></ul><ul><ul><li>To Allocate sufficient storage. </li></ul></ul><ul><ul><ul><li>Queries each recipient for the size of header and payload. </li></ul></ul></ul><ul><ul><li>Extend header </li></ul></ul><ul><ul><ul><li>Each header points to the header of the next DataSet </li></ul></ul></ul><ul><ul><ul><li>DataSet = Header + Payload. </li></ul></ul></ul>
  42. 47. Loosely Coupled Components <ul><li>Step #3 – Fragments and Headers </li></ul><ul><ul><li>Simple allocation based on total size may not be sufficient. </li></ul></ul><ul><ul><ul><li>In real world, Message is too big! </li></ul></ul></ul><ul><ul><ul><li>So We need Fragmentation, Reassembly </li></ul></ul></ul><ul><ul><li>For message fragmentation, Use Header and Payload. </li></ul></ul>
  43. 48. Loosely Coupled Components <ul><li>Step #3 – Fragment and Headers </li></ul><ul><ul><li>Purpose of Header. </li></ul></ul><ul><ul><ul><li>Payload may vary in size. </li></ul></ul></ul><ul><ul><ul><li>Don’t want to encode size limit in our code. </li></ul></ul></ul>
  44. 49. Loosely Coupled Components <ul><li>Step #3 – Fragment and Headers </li></ul><ul><ul><li>To use header, need following preconditions. </li></ul></ul><ul><ul><ul><li>Header that describes format are also required for packet send across the network . </li></ul></ul></ul><ul><ul><ul><li>Header have to be situated at the beginning of the packet . </li></ul></ul></ul><ul><ul><ul><li>The location of the header in the marshaled message may be mandated by networking protocol requirements. </li></ul></ul></ul><ul><ul><ul><li>Header format must be known to the receiver </li></ul></ul></ul><ul><ul><ul><ul><li>Receiver can determine the format of the payload simply by looking ah the header. </li></ul></ul></ul></ul>
  45. 50. Loosely Coupled Components <ul><li>Step #3 – Fragment and Headers </li></ul><ul><ul><li>Fragment </li></ul></ul>H/W Decomposed into Fragment.
  46. 51. Loosely Coupled Components <ul><li>Step #3 – Fragment and Headers </li></ul><ul><ul><li>Fragment ( Decide Message Sequence ) </li></ul></ul>Decomposed into Fragment. <ul><ul><li>Fragment Header have ID (identify location info). </li></ul></ul><ul><ul><li>Replicate Message (Protocol) Header for each Frag Header. </li></ul></ul><ul><ul><ul><li>Message Header is filled in by actual protocol. </li></ul></ul></ul><ul><ul><ul><ul><li>TCP, IP, raw ethernet, fddi and so on. </li></ul></ul></ul></ul>
  47. 52. Loosely Coupled Components <ul><li>Step #3 – Fragment and Headers </li></ul><ul><ul><li>Fragment ( Padding - Avoid splitting header info ) </li></ul></ul>Decomposed into Fragment. <ul><ul><li>Header define payload format (especially payload size) . </li></ul></ul><ul><ul><ul><li>It is prudent to avoid splitting any headers of DataSets. </li></ul></ul></ul><ul><ul><ul><li>We need decriptors for header. </li></ul></ul></ul><ul><ul><li>We pad the payload </li></ul></ul><ul><ul><li>so that fragmentation boundaries occur on payload rather than headers. </li></ul></ul>Splitting
  48. 53. Loosely Coupled Components <ul><li>Step # 4 – Reassembly </li></ul><ul><ul><li>Receive Message sent by sender </li></ul></ul><ul><ul><li>Just Extract Payload (ignoring headers and padding) </li></ul></ul><ul><ul><ul><li>Because the size of padding , Untill all fragments arrive </li></ul></ul></ul><ul><ul><li>Reassembly Message </li></ul></ul><ul><ul><ul><li>We Indicate the format of a fragment using auxiliary headers. </li></ul></ul></ul><ul><ul><ul><li>Auxiliary Header just have information about Splitting Information (Padding ..) </li></ul></ul></ul>
  49. 54. <ul><li>Choices (Distributed Virtual Memory) Operating System </li></ul><ul><ul><li>To Implement DVM, </li></ul></ul><ul><ul><ul><li>we have to take contents of pages and ship them across the network. </li></ul></ul></ul><ul><ul><ul><li>For efficiency , DVM system use its own network protocol stack </li></ul></ul></ul><ul><ul><ul><li>that supports fragmentations, reassembly, and reliable packet transfer. </li></ul></ul></ul><ul><ul><ul><li>The Stack can operate top of raw network like TCP/IP stack. </li></ul></ul></ul><ul><ul><ul><li>Since, Protocol Stack is customized for DVM </li></ul></ul></ul><ul><ul><ul><ul><li>Tightly Couple the stack layer . </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Communicate using Parameter Object . </li></ul></ul></ul></ul>
  50. 55. DVMMessage Code Class DVMMessage { /* Network Protocol Header */ //Leave space for Underlying Protocol Header // such as the Ethernet Header; unsigned _underlyingHeaderSize; unsigned _packetSize; //DVM Protocol using local identifiers DVMMessageType _type; unsigned _source; unsigned _destination; unsigned _timestamp; unsigned _checksum; /* Header and Auxiliary Header */ //Page List Header unsigned _totalNumberOfPages; unsigned _thisPageNumber; unsigned _offsetInPages; unsigned _pageFramentSize; } // List of pages to marshal from and unmarshal to. Page * _pageList; Page * _pageBeingProcessed /* Getter and Setter Functions */ //Expose using friend interfaces. … . public: //add pages to the list similar to add() //for the abstract message void addPages (Page * morePages); //marshal to buffer from page void marshal (unsigned *buffer, unsigned bufferSize); //unmarshal from buffer into page void unmarshal (unsigned* buffer, unsigned bufferSize); };
  51. 56. Fragmentation Message Code class Fragmenation { public : //calculate proper framents and ask message // to marshal parts of itself into the buffer. void makeFrags ( DVMMessage *m , unsigned * buffer, unsigned * bufferSize);
  52. 57. <ul><li>Chocies Distributed Virtual System </li></ul><ul><ul><li>Aamod Sane, Ken MacGregor, and Roy Campbell, </li></ul></ul><ul><ul><li>“ Distributed virtual memory consistency protocols: Design and performance” , </li></ul></ul><ul><ul><li>In Proceeding of the Second Workshop on Workstation Operating System, September 1989. </li></ul></ul><ul><li>X-Kernel networking System </li></ul><ul><ul><li>Norman C. Hutchinson and Larry L. Peterson. </li></ul></ul><ul><ul><li>“ The x-kernel: An architecture for implementing network protocols.” </li></ul></ul><ul><ul><li>IEEE Transactions on Software Engineering, 17(1): 64-76, January 1001. </li></ul></ul><ul><li>Parameter Object </li></ul><ul><ul><li>Kent Beck. </li></ul></ul><ul><ul><li>Parameter Object. </li></ul></ul><ul><ul><li>[email_address] , 1995 </li></ul></ul>
  53. 58. <ul><li>Active Message </li></ul><ul><ul><li>Is a message that knows how to process itself. </li></ul></ul><ul><ul><ul><li>When a message is received by the receiver </li></ul></ul></ul><ul><ul><ul><li>The message contains the identify to the function that will process the message. </li></ul></ul></ul><ul><ul><ul><li>The receiver may directly dispatch the message as –is to the Processor rather than spending time on the protocol stack. </li></ul></ul></ul><ul><ul><li>is applicable to homogeneous environment. </li></ul></ul><ul><ul><ul><li>Include pointers to functions within the message object. </li></ul></ul></ul><ul><ul><ul><li>Use COM+, Enterprise Service </li></ul></ul></ul>
  54. 59. <ul><li>Composite Message Pattern </li></ul><ul><li>Factory Method Pattern </li></ul>