Server-Side C++ Mapping Copyright © 2005-2010 ZeroC, Inc. Ice Programming with C++ 6. Server-Side C++ Mapping.

Slides:



Advertisements
Similar presentations
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
Advertisements

CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
CS 4800 By Brandon Andrews.  Specifications  Goals  Applications  Design Steps  Testing.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
CORBA Case Study By Jeffrey Oliver March March 17, 2003CORBA Case Study by J. T. Oliver2 History The CORBA (Common Object Request Broker Architecture)
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
OCT 1 Master of Information System Management Organizational Communications and Distributed Object Technologies Lecture 5: Distributed Objects.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
II. Middleware for Distributed Systems
How Does Remote Method Invocation Work? –Systems that use RMI for communication typically are divided into two categories: clients and servers. A server.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Communication in Distributed Systems –Part 2
Netprog RPC Overview1 Distributed Program Design n Communication-Oriented Design –Design protocol first. –Build programs that adhere to the protocol.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
1 Aniruddha Gokhale Vanderbilt University, Spring 2003 Intro to TAO Programming Study the Hello example in ACE_wrappers/TAO/tests –Show IDL definition,
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Copyright © 2003 ProsoftTraining. All rights reserved. Distributed Object Computing Using Java and CORBA.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class.
Introduction to Ice Copyright © ZeroC, Inc. Ice Programming with Java 1. Introduction to Ice.
Inheritance. Recall the plant that we defined earlier… class Plant { public: Plant( double theHeight ) : hasLeaves( true ), height (theHeight) { } Plant(
Understanding the CORBA Model. What is CORBA?  The Common Object Request Broker Architecture (CORBA) allows distributed applications to interoperate.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Defining New Types Lecture 21 Hartmut Kaiser
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 RMI.
Client-Side Slice-to-Java Mapping Copyright © ZeroC, Inc. Ice Programming with Java 4. Client-Side Slice-to-Java Mapping.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Update on CORBA Support for Babel RMI Nanbor Wang and Roopa Pundaleeka Tech-X Corporation Boulder, CO Funded by DOE OASCR SBIR.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Server-Side Java Mapping Copyright © ZeroC, Inc. Ice Programming with Java 6. Server-Side Java Mapping.
Copyright © 2015 Curt Hill Java for Minecraft Those things you should know.
Digging into the GAT API Comparing C, C++ and Python API‘s Hartmut Kaiser
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
EIToolkit stub doc. main.cpp file int main(int argc, char* argv[]) { EIProperties::UseStandards(); // create the stub ExampleStub stub; stub.Start();
Overview of C++ Polymorphism
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
CEN6502, Spring Understanding the ORB: Client Side Structure of ORB (fig 4.1) Client requests may be passed to ORB via either SII or DII SII decide.
 2000 Deitel & Associates, Inc. All rights reserved. Optional Case Study - Chapter 6 Outline 6.1 Introduction 6.2 Implementation: Visibility 6.3 Implementation:
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Copyright © Jim Fawcett Spring 2017
Jim Fawcett CSE775 – Distributed Objects Spring 2009
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Review: Two Programming Paradigms
Programmazione I a.a. 2017/2018.
Programming Models for Distributed Application
Overview of C++ Polymorphism
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Final Exam Review Inheritance Template Functions and Classes
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

Server-Side C++ Mapping Copyright © ZeroC, Inc. Ice Programming with C++ 6. Server-Side C++ Mapping

Server-Side C++ Mapping Copyright © ZeroC, Inc. 6-1 Lesson Overview This lesson presents: –the mapping from Slice to C++ relevant to the server side –the relevant APIs that are necessary to initialize and finalize the Ice run time –how to implement and register object implementations By the end of this lesson, you will be able to write a working Ice server.

Server-Side C++ Mapping Copyright © ZeroC, Inc. 6-2 Server Side C++ Mapping All of the client-side C++ mapping also applies to the server side. Additional server-side functionality you must know about: how to initialize and finalize the server-side run time how to implement servants how to pass parameters and throw exceptions how to create servants and register them with the run time

Server-Side C++ Mapping Copyright © ZeroC, Inc. 6-3 Initializing the Ice Run Time #include int main(int argc, char* argv[]) { int status = 1; Ice::CommunicatorPtr ic; try { ic = Ice::initialize(argc, argv); // server code here... status = 0; } catch (...) { } if (ic) { try { ic->destroy(); } catch (...) { } return status; }

Server-Side C++ Mapping Copyright © ZeroC, Inc. 6-4 Server-Side Initialization Servers must create at least one object adapter, activate that adapter, and then wait for the Ice run time to shut down: ic = Ice::initialize(argc, argv); Ice::ObjectAdapterPtr adapter = ic->createObjectAdapterWithEndpoints( "MyAdapter", "tcp -p 10000"); // Instantiate and register one or more servants here... adapter->activate(); ic->waitForShutdown(); An object adapter provides one or more endpoints at which the server listens for incoming requests. An adapter has a name that must be unique within its communicator. Adapters must be activated before they start accepting requests. You must call waitForShutdown from the main thread to wait for the server to shut down (or otherwise prevent the main thread from exiting).

Server-Side C++ Mapping Copyright © ZeroC, Inc. 6-5 Mapping for Interfaces Interfaces map to skeleton classes with a pure virtual member function for each Slice operation: module M { interface Simple { void op(); }; This generates into the header file: namespace M { public: class Simple : public virtual ::Ice::Object { virtual void op(const ::Ice::Current& = ::Ice::Current()) = 0; // Other methods here... }; } To implement an interface (that is, implement a servant), you derive a concrete servant class from the abstract skeleton class.

Server-Side C++ Mapping Copyright © ZeroC, Inc. 6-6 Mapping for Interfaces (1) You must implement all pure virtual function that are inherited from the skeleton class. You can add whatever else you need to support your implementation: constructors and destructor public, protected, or private member functions public, protected, or private data members other base classes Slice exception specifications are ignored by the C++ mapping. (No corresponding C++ exception specification is generated.)

Server-Side C++ Mapping Copyright © ZeroC, Inc. 6-7 Mapping for Operations As we saw, Slice operations map to pure virtual functions with the same name. idempotent Slice operations are mapped the same way as ordinary operations. interface Example { int op1(); idempotent int op2(); }; The signatures in the skeleton class are: virtual ::Ice::Int op1(const ::Ice::Current& = ::Ice::Current()) = 0; virtual ::Ice::Int op2(const ::Ice::Current& = ::Ice::Current()) = 0;

Server-Side C++ Mapping Copyright © ZeroC, Inc. 6-8 Mapping for Parameters Server-side operation signatures are identical to the client-side operation signatures: In-parameters are passed by value or by const reference. Out-parameters are passed by non-constant reference. Return values are passed by value. Every operation has a single trailing parameter of type Ice::Current. string op(int a, string b, out float c, out string d); Maps to: virtual ::std::string op( ::Ice::Int, const ::std::string&, ::Ice::Float&, ::std::string&, const ::Ice::Current& = ::Ice::Current()) = 0;

Server-Side C++ Mapping Copyright © ZeroC, Inc. 6-9 Throwing Exceptions exception GenericError { string reason; }; interface Example { void op() throws GenericError; }; You could implement op as: void ExampleI::op(const Current&) { throw GenericError("something failed"); } Do not throw Ice run-time exceptions. You can throw ObjectNotExistException, OperationNotExistException, or FacetNotExistException, which are returned to the client unchanged. But these have specific meaning and should not be used for anything else. If you throw any other run-time exception, the client will get an UnknownLocalException.

Server-Side C++ Mapping Copyright © ZeroC, Inc Creating an ObjectAdapter Each server must have at least one object adapter. You create an adapter with: local interface ObjectAdapter; local interface Communicator { ObjectAdapter createObjectAdapter(string name); ObjectAdapter createObjectAdapterWithEndpoints( string name, string endpoints); //... }; The endpoints at which the adapter listens are taken from configuration (first version), or from the supplied argument (second version). Example endpoint specification: tcp -p 10000:udp -p 10000:ssl -p Endpoints have the general form: [-h ] [-p ] [-t timeout] [-z]

Server-Side C++ Mapping Copyright © ZeroC, Inc Object Adapter States An object adapter is in one of three possible states: Holding (initial state after creation) The adapter does not read incoming requests off the wire (for TCP and SSL) and throws incoming UDP requests away. Active The adapter processes incoming requests. You can transition freely between the Holding and Active state. Inactive This is the final state, entered when you initiate destruction of the adapter: –Requests in progress are allowed to complete. –New incoming requests are rejected with a ConnectionRefusedException.

Server-Side C++ Mapping Copyright © ZeroC, Inc Controlling Adapter State The following operations on the adapter relate to its state: local interface ObjectAdapter { void activate(); void hold(); void deactivate(); void waitForHold(); void waitForDeactivate(); void destroy(); //... }; The operations to change state are non-blocking. If you want to know when a state transition is complete, call waitForHold or waitForDeactivate as appropriate. destroy blocks until deactivation completes. You can re-create an adapter with the same name once destroy completes.

Server-Side C++ Mapping Copyright © ZeroC, Inc Object Identity Each Ice object has an associated object identity. Object identity is defined as: struct Identity { string name; string category; }; The name member gives each Ice object a unique name. The category member is primarily used in conjunction with default servants and servant locators. If you do not use these features, the category is usually left as the empty string. The identity must be unique within the object adapter: no two servants that incarnate an Ice object can have the same identity. The combination of name and category must be unique. An identity with an empty name denotes a null proxy.

Server-Side C++ Mapping Copyright © ZeroC, Inc Stringified Object Identity Two helper functions on the communicator allow you to convert between identities and strings: class Communicator : /*... */ { public: Identity stringToIdentity(const std::string&); std::string identityToString(Identity id); //... }; Stringified identities have the form /, for example: person/fred If no slash is present, the string is used as the name, with the category assumed to be empty. The object adapter has a getCommunicator method that returns the communicator. You need the communicator to convert between strings and object identities.

Server-Side C++ Mapping Copyright © ZeroC, Inc The Active Servant Map (ASM) Each adapter maintains a map that maps object identities to servants: Incoming requests carry the object identity of the Ice object that is the target. The ASM allows the server-side run time to locate the correct servant for the request. Object identities must be unique per ASM.

Server-Side C++ Mapping Copyright © ZeroC, Inc Instantiating Servants Servants must be instantiated on the heap: SimplePtr sp = new SimpleI("Hello"); Servants are reference-counted. As for classes, you use smart pointers to manage the servant’s memory: once the last smart pointer to a servant goes out of scope, the servant is destroyed. If you want a smart pointer to the servant class (as opposed to the skeleton base class), declare it as: typedef IceUtil::Handle SimpleIPtr;

Server-Side C++ Mapping Copyright © ZeroC, Inc Activating Servants To make a servant available to the Ice run time, you must activate it. This adds an identity–servant pair to the ASM: local interface ObjectAdapter { Object* add(Object servant, Identity id); Object* addWithUUID(Object servant); //... }; Both operations return the proxy for the servant, for example: SimplePrx sp = SimplePrx::uncheckedCast( adapter->add(new SimpleI("hello"), adapter->getCommunicator()-> stringToIdentity("fred"))); As soon as a servant is added to the ASM, the run time will dispatch requests to it (assuming that the adapter is activated). addWithUUID adds the servant to the ASM with a UUID as the name, and an empty category.

Server-Side C++ Mapping Copyright © ZeroC, Inc The Ice::Application Class Ice::Application is a utility class to start and stop the Ice run time. namespace Ice { class Application /*... */ { public: virtual ~Application(); int main(int, char* [], const char* configFile); int main(int, char* [], const InitializationData& = InitializationData()); virtual int run(int, char* []) = 0; static const char* appName(); static CommunicatorPtr communicator(); //... }; } You call Application::main from the real main, and implement the body of your client or server in the run method.

Server-Side C++ Mapping Copyright © ZeroC, Inc Signal Handling Ice::Application provides platform-independent signal handling: enum SignalPolicy { HandleSignals, NoSignalHandling }; class Application : /*... */ { public: Application(SignalPolicy = HandleSignals); //... static void destroyOnInterrupt(); static void shutdownOnInterrupt(); static void ignoreInterrupt(); static void holdInterrupt(); static void releaseInterrupt(); static bool interrupted(); }; The default behavior on receipt of a signal is to destroy the communicator, allowing all currently running operation invocations to complete first. Signal handling is thread-safe. (The handler is invoked from a separate thread.)

Server-Side C++ Mapping Copyright © ZeroC, Inc Compiling and Linking a Server The exact compiler options vary from platform to platform. But, regardless of differences, you must: compile the Slice-generated source files(s) compile your application code link the object files with Ice and IceUtil libraries For Linux and a Slice definition file Foo.ice : $ c++ -D_REENTRANT -I. -I$ICE_HOME/include -c Foo.cpp Server.cpp $ c++ -o server Foo.o Server.o -L$ICE_HOME/lib -lIce -lIceUtil Note that these commands are the same as for the client side—you need not supply server-specific options or link against a server-specific object file or library.