Comet an ErlangtoCOM Port n n Comet is

  • Slides: 20
Download presentation
Comet - an Erlang-to-COM Port n n Comet is a port and a gen_server

Comet - an Erlang-to-COM Port n n Comet is a port and a gen_server module that enables Erlang programs to deploy COM components Comet is under development, an early version is part of OTP release 7 B

COM n n n Common Object Model A standard for component development from Microsoft

COM n n n Common Object Model A standard for component development from Microsoft Windows-only (although a third-party version exist on Solaris) Rival to CORBA on the Windows platform All Microsoft programs use COM Support for distribution, DCOM and COM+

COM Model n n Classes presents interfaces Interfaces are a bunch of related functions

COM Model n n Classes presents interfaces Interfaces are a bunch of related functions or methods No data are exposed, only interfaces Properties of objects accessible through access-functions

COM Model continued n n IDL describes classes and interfaces IDL compiles into a

COM Model continued n n IDL describes classes and interfaces IDL compiles into a type-library that can be browsed with a tool Two ways to use a class – Dispatch - a special interface for interpreted languages – Virtual Interface - faster, for compiled languages Comet can use both (although dispatch is safer)

COM Memory Handling n n n Reference-counting Language support in Visual Basic, “Java” (and

COM Memory Handling n n n Reference-counting Language support in Visual Basic, “Java” (and C#) Erlang programs must (currently) explicitly free interfaces

Erlang Ports n n n A way to call external code from Erlang Implemented

Erlang Ports n n n A way to call external code from Erlang Implemented as a linked in driver (DLL) or a port program Comet offers both – port driver is considerably faster – port program is safer, if the COM server crashes, it won’t bring the emulator down A gen_server module interfaces to the port program or driver The Comet port driver and program are multithreaded

Comet as a Port Driver n An application calling a COM Object – Comet

Comet as a Port Driver n An application calling a COM Object – Comet as a driver Erlang process some_application erl_com Erl. Com. Drv. dll thread IWhatever

Comet as a Port Program n An application calling a COM Object – Comet

Comet as a Port Program n An application calling a COM Object – Comet as a port program Erlang process some_application erl_com Erl. Com. Drv. exe thread IWhatever

Calling COM from Erlang n n n All calls through the gen_server module “erl_com”

Calling COM from Erlang n n n All calls through the gen_server module “erl_com” erl_com provides methods for calling erl_com has functions for: – creating objects – fetching interfaces – releasing interfaces and objects – retrieving type information of objects and types – creating threads for calling COM objects asynchronously

A Simple Example n An interface that implements some utilities interface ISome. Utilities :

A Simple Example n An interface that implements some utilities interface ISome. Utilities : IDispatch { [id(1)] HRESULT Days. Between([in] DATE date 1, [in] DATE date 2, [out, retval] double* days. Between); [id(2)] HRESULT Replace. All([in] BSTR in. Str, [in] BSTR key. Str, [in] BSTR new. Str, [out, retval] BSTR* out. Str); . . . }; n Calling it from Erlang . . . S= “It was a dark and stormy night. . . ”, I= erl_com: create_dispatch(T, “{class id for Some. Utilities}”), S 2= erl_com: invoke(I, “Replace. All”, [S, “stormy”, “still”]), D= erl_com: invoke(I, “Days. Between”, [{vt_date, {{2000, 1, 1}, {0, 0, 0}}}, {vt_date, erlang: now()}]) erl_com: release(I), . . .

Mapping COM Types to Erlang n n COM uses a small set of types

Mapping COM Types to Erlang n n COM uses a small set of types Comet mapps Erlang types to COM types through the use of tuples – Basic types are converted properly: integers, floats, strings and booleans – Other types are prefixed in a tuple, e. g. {vt_date {1999, 12}, {}} n n n Constants in COM are enumerations Strings currently 8 -bits in Comet Complex types as structures, are currently not supported

Invoke (dispatch interface) n n The invoke method in the dispatch interface is used

Invoke (dispatch interface) n n The invoke method in the dispatch interface is used to latebind to interfaces Comet provides the methods invoke, property_put and property_get . . . Obj= erl_com: create_object(T, “{class id}”), I= erl_com: query_interface(Obj, “{a dispatch interface id}”), Value= erl_com: invoke(I, “Method”, [parameters]), erl_com: property_put(I, “Property”, [parameters], Value 2), Value 3= erl_com: property_get(I, “Another. Property”), . . . n n Errors returned as {com_error, Code} Can have named parameters (not support in Comet yet)

Calling a Virtual Interface n n n A virtual interface is an array of

Calling a Virtual Interface n n n A virtual interface is an array of function pointers – Virtual Method Table used for C++ objects Called in Comet using assembler glue – Every parameter, including return value, must be explicitly typed – Address of virtual function must be specified – Only practical when code is generated Wrong parameters cause Comet to crash . . . [Outstr]= erl_com: com_call(I_, 36, [{ vt_str, In. Str}, {vt_str, out}]), . . .

Browser Example n n The Internet Explorer browser presents COM interfaces Example: creating an

Browser Example n n The Internet Explorer browser presents COM interfaces Example: creating an Internet Explorer and navigating to a URL 1 opens a Comet process and a thread in it 2 creates an object, retrieves its default interface 3 invokes the methods “navigate” and the “visible” {ok, Pid}= erl_com: start_process(), 1 T= erl_com: new_thread(Pid), Obj= erl_com: create_dispatch(T, " Internet. Explorer. Application", ? CLSCTX_LOCAL_SERVER), erl_com: invoke(Obj, "Navigate", ["www. erlang. org"]), erl_com: property_put(Obj, "Visible", true), 3 Obj. 2

Excel Example n n n Excel is also accessible through COM Easiest way is

Excel Example n n n Excel is also accessible through COM Easiest way is to start with a Visual Basic-program – The Excel macro recorder can generate these Example: adding a graph Visual Basic: Charts. Add Active. Chart. Type = xl. Pie. Exploded Erlang: Charts = erl_com: package_interface(E, erl_com: property_get(E, "Charts")), erl_com: invoke(Charts, "Add"), C= erl_com: package_interface(E, erl_com: property_get(E, "Active. Chart")), erl_com: property_put(C, "Chart. Type", ? Xl. Pie. Exploded),

Generating Glue Code n n n Can be used for both virtual- and dispatch-interfaces

Generating Glue Code n n n Can be used for both virtual- and dispatch-interfaces Type libraries, compiled from COM IDL, describes COM classes and interfaces Comet reads information from Type Libraries Erlang modules are generated with glue code Each interface generates a module Each enum (set of constants) generates a module and a header-file with macros

Excel Example with Generated Code – (Code is generated from the Excel type-library) Visual

Excel Example with Generated Code – (Code is generated from the Excel type-library) Visual Basic: Charts. Add Active. Chart. Type = xl. Pie. Exploded Active. Chart. Set. Source. Data _ Source: =Sheets("Sheet 1"). Range("B 2: C 4"), _ Plot. By: =xl. Columns Active. Chart. Location Where: =xl. Location. As. Object, Name: ="Sheet 1" Erlang: charts: add(xc_Application: charts(E)), Active. Chart= xc_Application: active. Chart(E), chart: chart. Type(Active. Chart, ? Xl. Pie. Exploded), R= sheets: range(xc_Application. sheets(E, “Sheet 1”), “B 2: C 4”), chart: set. Source. Data(Active. Chart, R, ? xl. Columns), chart: location(Active. Chart, ? xl. Location. As. Object, "Sheet 1"),

Problems n n n n Combining an object-oriented approach with Erlang’s semi -functional Handling

Problems n n n n Combining an object-oriented approach with Erlang’s semi -functional Handling state Memory management Type conversions between Erlang and other system Asynchronous operations Performance considerations Robustness

Future Improvements n n Feedback needed Improvements considered – Full Unicode support – Calling

Future Improvements n n Feedback needed Improvements considered – Full Unicode support – Calling Erlang from COM – Event Sinks – Erlang COM Servers – COM+ Distribution – Complex types – Other API’s on other platforms – Combining COM’s ref-counting with Erlang’s GC

References n n n Comet documentation from OTP Don Box: Essential COM (Addison Wesley)

References n n n Comet documentation from OTP Don Box: Essential COM (Addison Wesley) Box, Brown, Ewald and Sells: Effective COM (Addison Wesley) Oberg: Understanding & Programming COM+ (Prentice Hall) Jason Pritchard: COM and Corba Side by Side (Addison Wesley)