Docsity

CSE775 - Distributed Objects Midterm 1 Examination - Prof. James Fawcett, Exams of Engineering

Toggle sidebar
bg1
CSE775 – Distributed Objects Spring 2008
Midterm #1 Examination
Name:_______Instructor’s Solution________________ SUID:____________________
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 promptly at the end of
class. 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 CSE775 - Distributed Objects Midterm 1 Examination - Prof. James Fawcett and more Exams Engineering in PDF only on Docsity!

Midterm #1 Examination

Name:_______Instructor’s Solution________________ SUID:____________________ 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 promptly at the end of class. 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. Describe the Dispatch interface, in as much detail as you can^1. When would you use it? Answer: The IDispatch interface, derived from IUnknown provides four methods: GetTypeInfoCount returns 1 if TypeInfo interface is available, else 0 GetTypeInfo returns TypeInfo interface if available GetIDsOfNames maps single method name and its parameter names into DISPIDs Invoke call method or access property for an input DISPID Parameters are sent to Invoke in an array of variants, packed into a DISPPARMs structure, and a result is returned in an [out] variant parameter. This interface is designed to allow languages that do not support compilation of vtble-based interfaces to call any method that can be described in IDL. Javascript on web pages and VBscript on Asp server pages are current examples. Almost every time you define a COM component you provide it with dual interfaces, e.g., both a vtble-based interface and a dispatch interface so that the component can be used by the broadest practical set of clients. (^1) This question is not about the implementation of the interface, but about its syntax and the expected semantics of any implementation.
  1. Suppose that, in Project #2, you decided that each host should provide a thread to service client requests, and a separate thread to handle communication. Describe an apartment structure for this design. What are the threading models for each, and how will shared resources be synchronized. Answer: For simplicity of implementation you could define the primary STA, using the host’s main thread, to service client requests, like this: CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); To allow a child thread to perform COM work, we will then need to create a child thread and have it create a second STA, using the same call. In this case, all calls between these apartments are marshaled through a proxy. The receiving communication component will service host calls through a windows message loop, all running on the single STA thread, so no synchronization is necessary, except for global and static data in the component. For reasons of performance, we might choose, instead, to create in the host an MTA, like this: CoInitializeEx(NULL, COINIT_MULTITHREADED); Now, the communication component, if it has a registry marking of free or both, will join this MTA when created and all calls will be direct. Since it is possible that both host and component threads may concurrently call and service the same method, all member data of the communication component must be synchronized, along with its global and static data unless all calls result in messages placed in a thread-safe blocking queue, in which case no further synchronization is required. If the communication component has no registry threading model or single or APARTMENTTHREADED, then it will be loaded into an STA, created by COM, and all calls will be marshaled to its message loop, so the synchronization is the same as for the first case cited in this answer.
  1. Write code to respond to clicks on a button control on a Win32 window by popping up a message box, with a “been clicked” message. Answer: case WM_COMMAND: // Parse control messages: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); switch (wmId) { case ButtonChildID: // parse button messages switch(wmEvent) { case BN_CLICKED: MessageBox(NULL,L "been clicked",L"demo",MB_OK); break; // other cases ommitted
  1. Describe the COM interface policy. Discuss its symmetric, reflexive, and transitive properties. How does one establish the identity of an instance of a COM component? What does that have to do with COM interface policy? Answer: All COM interfaces must support at least the three IUnknown interface methods QueryInterface, AddRef, and Release. All calls to QueryInterface for a pointer to the IUnknown interface must return the same address. The set of interfaces accessible through QueryInterface must be fixed. Suppose that a component exposes three interfaces, say IA, IB, and IC. Symmetric property: If a component exposes the two interfaces, IA and IB, and if a client holds a pointer to IA and successfully queries for a pointer to IB, then a subsequent query, using IB, for the interface IA must succeed. Reflexive property: If a client holds a pointer to the IA interface, then a query using that pointer for the IA interface must succeed. Transitive property: If a client holds a pointer to IA and can successfully query for IB, and, using the pointer to IB, cab successfully query for interface IC, then, when holding a pointer to IA, a query for IC must succeed. All COM interfaces are immutable. That is, once published, they must not be changed. When a function returns an interface pointer it must call AddRef() before returning. When a client finishes using an interface pointer it should call Release().
  1. Write the IDL for an interface that passes a collection of file names. You may structure the collection in any way that seems effective. Write client-side code that calls this function and stores the results in an array of strings. Answer: [object,uuid(8030C6D6-2169-4F75-9D8A-C2AADE3B91E7),dual, nonextensible,helpstring("IFileHandler Interface"), pointer_default(unique) ] interface IFileHandler : IDispatch{ [ id(1), helpstring("method GetFileNames")] HRESULT GetFileNames([ out] LONG* pCount, [out] BSTR** pFileNames); }; /////////////////////////////////////////////////////////// // FilesClient.cpp - Demonstrate Solution to MT1-Q7 // // // // Jim Fawcett, CSE775 - Distributed Objects, Spring 08 // /////////////////////////////////////////////////////////// #include <atlbase.h> #include #include "../Files/Files_i.h" #include <atlbase.h> #include #include "../Files/Files_i.h" int main( int argc, char* argv[]) { CoInitialize(NULL); CComQIPtr pFileHandler; pFileHandler.CoCreateInstance(CLSID_FileHandler); if(pFileHandler) { long Count; BSTR FileNames;* HRESULT hr = pFileHandler->GetFileNames(&Count, &FileNames); if(SUCCEEDED(hr)) { for(int i=0; i<Count; ++i) { std::wcout << "\n " << FileNames[i]; SysFreeString(FileNames[i]); } CoTaskMemFree(FileNames); } } std::wcout << "\n\n"; return 0; }
  1. What is a GUID? Describe all the places you would find GUIDs in an implementation of a COM component. Answer: GUIDS are Globally Unique IDentifiers, used by COM to uniquely identify components, interfaces, and typelibraries. You find GUIDS defined in IDL files, declared in comp_i.h files, mapped to identifiers in comp_i.c files, and COM finds them in the registry, as keys to the paths from which to find typelibraries, load libraries, and start processes. A component’s typelibrary stores the equivalent of its GUIDs.