Docsity

Practice Midterm Exam - Spring 2006 - Distributed Objects | CSE 775, Exams of Engineering

Toggle sidebar
bg1
CSE775 – Distributed Objects Spring 2006
Instructor’s Solution
Midterm Examination
This is a closed book examination. Please place all your books on the floor
beside you. You may keep one page of notes on your desktop in addition to
this exam package. All examinations will be collected at 8:20. Please be
prepared to quickly hand in your examination at that time.
If you have any questions, please do not leave your seat. Raise your hand
and I will come to your desk to discuss your question. I will answer all
questions about the meaning of the wording of any question. I may choose
not to answer other questions.
You will find it helpful to review all questions before beginning. All questions
are given equal weight for grading, but not all questions have the same
difficulty. Therefore, it is very much to your advantage to answer first those
questions you believe to be easiest.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Practice Midterm Exam - Spring 2006 - Distributed Objects | CSE 775 and more Exams Engineering in PDF only on Docsity!

Instructor’s Solution

Midterm Examination

This is a closed book examination. Please place all your books on the floor beside you. You may keep one page of notes on your desktop in addition to this exam package. All examinations will be collected at 8:20. Please be prepared to quickly hand in your examination at that time. If you have any questions, please do not leave your seat. Raise your hand and I will come to your desk to discuss your question. I will answer all questions about the meaning of the wording of any question. I may choose not to answer other questions. You will find it helpful to review all questions before beginning. All questions are given equal weight for grading, but not all questions have the same difficulty. Therefore, it is very much to your advantage to answer first those questions you believe to be easiest.

  1. In Project #2^1 you are asked to start one or more out-of-process COM wrappers for legacy tools and collect their results. Write the IDL, Client code and Wrapper Component code that starts one of the tools. Assume that you provide a series of command line arguments that may number from zero to five. Your client code shows how to package the arguments and send them to the wrapper. Your component code shows how they are unpacked for delivery to the legacy program, which you may represent by a function startTool(args) that you do not have to supply. The IDL you need to provide here just describes the interface that allows you to do this and collect the results, assuming a text output from the legacy tool. Answer: See Handouts/cse775/midterm/CodeSp06-Q (^1) Statement attached at the end.

Note that the communication component’s send and receive threads are doing no COM work, so they don’t need a pointer to the component’s interface and no marshaling is carried out as part of their activities.

Synchronizer

Secondary STA Primary STA Communication Component socket Executive Pops up user interface form if client, otherwise simply manages component and worker thread. Worker Thread Gathers directory and file information, copies files Form.Invoke With this structure, only two threads, on each machine, need to know about the communication component interface. Send thread Recv thread Primary STA thread Primary STA thread Secondary STA Primary STA thread thread Secondary STA thread

  1. Describe the COM memory allocation management rules. Suppose that the results returned to the client in Project #2 are represented by one long string. Where and when, on the wrapper machine, is the memory allocated and deallocated for the result. Where and when, on the client machine, is the memory allocated and deallocated for the result. Answer: For [in] parameters, the client Allocates and de-allocates storage. For [out] parameters the server allocates storage and the client de-allocates. For [in, out] parameters, the client allocates, the server may re-allocate, and the client de-allocates. For arrays you use CoTaskMemAllocate and CoTaskMemFree. For BSTRs, use SysAllocString and SysFreeString. Supposing that the wrapper collects legacy tool output as one long string, it must allocate storage for that. Doing that efficiently is a problem since we don’t know how long the output is going to be, but must use the explicit COM allocation rules, so let’s simply collect the tool output as a std::wstring, then when all of the tool output has been collected, allocate a BSTR using SysAllocStringLen, and return its address, using the BSTR::Detach() function^4. After the string has been marshaled to the client, the stub will deallocate the detached storage. When the client receives the string, the proxy creates storage for the string, using SysAllocStringLen, with the BSTR’s length parameter, and passes the address to the client, via a parameter of the invoking function. When the client is finished with the string the string is destroyed, by the client, with SysFreeString. In view of the inefficiency of these operations, it might make more sense to chunk the legacy tool output into fixed size blocks, so that we could avoid the copy, described above. (^4) See AtlDemo
  1. What is the importance of the IDispatch interface. Name all of its methods and state their purpose. Answer: a. IDispatch is a “universal” interface that is accessible by COM-aware scripting languages, like Javascript and Pearl. b. The Win32 API provides functions to implement IDispatch interfaces, sourced by COM objects. This makes it straight-forward for containers to support events provided by ActiveX controls. c. Its methods are: i. GetIDsOfNames – maps single method name and argument names to DISPID structure. ii. Invoke – executes a method based on its DISPID iii. GetTypeInfoCount – returns zero or one, depending on whether type information is available. iv. GetTypeInfo – returns pointer to TypeInfo object.
  1. Describe the complete activation sequence for the client and server and legacy tool for Project #2. Be as specific as you can about the interaction between the client, the COM run-time, and the out-of-process wrapper. Please specify which apartment you will use for the wrapper, and what does that have to do with activation? Answer: a. Server is initially run with /RegServer as command line argument to set key registry values. Server process terminates after registration. b. Client calls Coinitialize() to create an apartment – let’s assume an STA c. Client calls CoCreateInstance() to activate an out-of-process wrapper. d. COM finds the component in the registry, checks the ROT to see if it is currently running, and if not, calls CreateProcess() to start it, supplying /Embedding on the command line. e. Server calls Coinitialize() to create an apartment – again assume STA. f. Server creates class factory and calls CoRegisterClassObject(). g. Server enters windows message loop. h. After the server has registered the class factory (step f.) COM calls CreateInstance(), using IClassFactory pointer it gets from ROT, and marshals requested pointer, obtained from call, back to the client. i. Client uses pointer to call wrapper’s start function, passing any options that the client wants and wrapper accepts. j. Wrapper calls CreateProcess() to start the legacy tool, passing it option arguments on the (child process’s) command line. k. While the legacy tool runs, its standard output is redirected to the wrapper and is captured as a string. l. When the child process finishes, the wrapper sends the client a (connection point) notification, and the client retrieves the output, by calling a function on the wrapper interface. m. The client calls release on the wrapper interface and the wrapper decrements its reference count and shuts down. n. Since the wrapper resides in an STA, all of the COM to server and client to server calls are deposited by RPC threads in the windows message loop of the wrapper STA. The single thread of the wrapper STA reads the messages and dispatches them to the appropriate instance methods.
  1. What is the purpose of the Connection Point technology? Describe its three principle parts and how the client interacts with them. Can you cite one distinguishing characteristic of the IDL for a project that uses connection points? Answer: Connection points support event notification. They allow a COM component to make outgoing calls to clients when certain specified internal events occur. The three principle parts of a connection point application are: a. ConnectionPointContainer – a COM object that contains one or more ConnectionPoints. b. ConnectionPoint – a COM component that makes IDispatch calls on a sink component implemented by the client. c. Sink – a COM component implemented by the client that receives notifications. Clients interact with these parts as follows: a. The client enumerates the ConnectionPointContainer to discover if a specific connection point is implemented. b. If so, the client advises the ConnectionPoint of its interest in receiving notifications by marshaling a pointer to its sink interface to the ConnectionPoint. c. The client implements a handler for notifications as a method on its sink interface. The method body does whatever the client needs when a notification occurs. d. When the client no longer is interested in receiving notifcations, it can call Unadvise on the ConnectionPoint interface. A COM object that wants to expose event(s), using ConnectionPoints, expresses that through an IDispatch interface, marked with the IDL attribute “source”. This declares a sink interface a client is required to implement.
  1. How does the out-of-process wrapper component in Project #2 join an apartment? Describe the interaction between RPC channel threads and the wrapper for each of the possible apartment configurations you might use for Project #2. Answer: Since the wrapper is out-of-process, it must call Coinitialize or CoinitializeEx(…), to establish an apartment. There are three apartment configurations, Single Threaded Apartment (STA), Multiple Threaded Apartment (MTA), and Thread Neutral Apartment (TNA). Of these, we have discussed primarily STAs and MTAs in class, so we’ll restrict our attention to these two. All calls come from other processes, and so are from outside the wrapper’s apartment. So each call must be marshaled, using the RPC channel, as follows: a. STA – RPC threads deposit windows messages for each proxied call into the apartment, in the windows message loop of the apartment. The single thread of the apartment extracts each message and dispatches it to the appropriate instance method. b. MTA – The main thread of the wrapper is blocked on an event, holding in memory, and individual RPC threads directly access each instance method of the component. c. TNA - A threading model is exposed by a COM object with one of these four legal entries: a. Apartment b. Free c. Both d. Neutral