ebook img

The ACE programmer's guide: practical design patterns for network and systems programming PDF

537 Pages·2009·3.466 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview The ACE programmer's guide: practical design patterns for network and systems programming

The ACE Programmer’s Guide This page intentionally left blank The ACE Programmer’s Guide Practical Design Patterns for Network and Systems Programming Stephen D. Huston James CE Johnson Umar Syyid Boston • San Francisco • New York • Toronto • Montreal London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico City Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Addison-Wesley was aware of a trademark claim, the designations have been printed with initial capital letters or all capitals. The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. Figures 7.1, 7.2, 7.3, and 8.1 originally published in Schmidt/Huston, C++ Network Programming, Volume 2: Systematic Reuse with ACE and Frameworks, Copyright © 2003 by Pearson Education, Inc. Reprinted with permission of Pearson Education, Inc. The publisher offers discounts on this book when ordered in quantity for bulk purchases and special sales. For more information, please contact: U.S. Corporate and Government Sales (800) 382-3419 [email protected] For sales outside of the U.S., please contact: International Sales [email protected] Visit Addison-Wesley on the Web: www.awprofessional.com Library of Congress Cataloging-in-Publication Data Huston, Stephen D. The ACE programmer’s guide : practical design patterns for network and systems programming / Stephen D. Huston, James CE Johnson and Umar Syyid. p. cm. ISBN 0-201-69971-0 (pbk. : alk. paper) 1. Computer software—Development. 2. Object-oriented programming (Computer science) 3. Software patterns. I. Johnson, James C. E. II. Syyid, Umar. III. Title. QA76.76.D47H89 2003 005.1’17—dc21 2003014046 Copyright © 2004 by Pearson Education, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. For information on obtaining permission for use of material from this work, please submit a written request to: Pearson Education, Inc, Rights and Contracts Department 75 Arlington Street, Suite 300 Boston, MA 02116 Fax: (617) 848-7047 ISBN13:978-0-201-69975-5 ISBN 0-201-69971-0 Text printed in the United States at Offset Paperback Manufacturers in Laflin, Pennsylvania. 6th Printing March 2010 Contents Illustrations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xv Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Part I: ACE Basics 1 1. Introduction to ACE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 1.1 A History of ACE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 1.2 ACE’s Benefits. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5 1.3 ACE’s Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6 1.4 Patterns, Class Libraries, and Frameworks. . . . . . . . . . . . . . . . . . . . . . .7 1.5 Porting Your Code to Multiple Operating Systems . . . . . . . . . . . . . . . .8 1.6 Smoothing the Differences among C++ Compilers. . . . . . . . . . . . . . .11 1.7 Using Both Narrow and Wide Characters . . . . . . . . . . . . . . . . . . . . . .19 1.8 Where to Find More Information and Support. . . . . . . . . . . . . . . . . . .21 1.9 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23 2. How to Build and Use ACE in Your Programs . . . . . . . . . . . . . . .25 2.1 A Note about ACE Versions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25 2.2 Guide to the ACE Distribution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26 v vi Contents 2.3 How to Build ACE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27 2.4 How to Include ACE in Your Applications . . . . . . . . . . . . . . . . . . . . .30 2.5 How to Build Your Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31 2.6 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36 3. Using the ACE Logging Facility . . . . . . . . . . . . . . . . . . . . . . . . . .37 3.1 Basic Logging and Tracing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38 3.2 Enabling and Disabling Logging Severities. . . . . . . . . . . . . . . . . . . . .43 3.3 Customizing the ACE Logging Macros. . . . . . . . . . . . . . . . . . . . . . . .47 3.4 Redirecting Logging Output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55 3.5 Using Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .60 3.6 The Logging Client and Server Daemons . . . . . . . . . . . . . . . . . . . . . .64 3.7 The LogManager Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70 3.8 Runtime Configuration with the ACE Logging Strategy. . . . . . . . . . .73 3.9 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .75 4. Collecting Runtime Information . . . . . . . . . . . . . . . . . . . . . . . . . .77 4.1 Command Line Arguments and ACE_Get_Opt. . . . . . . . . . . . . . . . . .78 4.2 Accessing Configuration Information . . . . . . . . . . . . . . . . . . . . . . . . .83 4.3 Building Argument Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85 4.4 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86 5. ACE Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87 5.1 Container Concepts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88 5.2 Sequence Containers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .90 5.3 Associative Containers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103 5.4 Allocators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115 5.5 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .119 Part II: Interprocess Communication 121 6. Basic TCP/IP Socket Use. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123 6.1 A Simple Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .124 6.2 Adding Robustness to a Client. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129 6.3 Building a Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .135 6.4 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .140 7. Handling Events and Multiple I/O Streams . . . . . . . . . . . . . . . . .141 7.1 Overview of the Reactor Framework . . . . . . . . . . . . . . . . . . . . . . . . .142 7.2 Handling Multiple I/O Sources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142 Contents vii 7.3 Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .155 7.4 Notifications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .158 7.5 Timers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .160 7.6 Using the Acceptor-Connector Framework . . . . . . . . . . . . . . . . . . . .168 7.7 Reactor Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181 7.8 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .185 8. Asynchronous I/O and the ACE Proactor Framework. . . . . . . . .187 8.1 Why Use Asynchronous I/O?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .188 8.2 How to Send and Receive Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . .189 8.3 Establishing Connections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .197 8.4 The ACE_Proactor Completion Demultiplexer . . . . . . . . . . . . . . . . .201 8.5 Using Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202 8.6 Other I/O Factory Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202 8.7 Combining the Reactor and Proactor Frameworks. . . . . . . . . . . . . . .203 8.8 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205 9. Other IPC Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .207 9.1 Interhost IPC with UDP/IP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .207 9.2 Intrahost Communication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .213 9.3 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .215 Part III: Process and Thread Management 217 10. Process Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .219 10.1 Spawning a New Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .219 10.2 Using the ACE_Process_Manager . . . . . . . . . . . . . . . . . . . . . . . . . . .226 10.3 Synchronization Using ACE_Process_Mutex . . . . . . . . . . . . . . . . . .231 10.4 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .234 11. Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .235 11.1 Using Wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .236 11.2 Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .239 11.3 Guarding Critical Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .246 11.4 Signal Management with the Reactor. . . . . . . . . . . . . . . . . . . . . . . . .247 11.5 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .248 12. Basic Multithreaded Programming . . . . . . . . . . . . . . . . . . . . . .249 12.1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .250 12.2 Basic Thread Safety . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .251 viii Contents 12.3 Intertask Communication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .257 12.4 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .266 13. Thread Management. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .267 13.1 Types of Threads. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .267 13.2 Priorities and Scheduling Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . .271 13.3 Thread Pools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .275 13.4 Thread Management Using ACE_Thread_Manager . . . . . . . . . . . . .276 13.5 Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .279 13.6 Thread Start-Up Hooks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .283 13.7 Cancellation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .284 13.8 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .288 14. Thread Safety and Synchronization . . . . . . . . . . . . . . . . . . . . . .289 14.1 Protection Primitives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .289 14.2 Thread Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .301 14.3 Thread-Specific Storage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .309 14.4 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .311 15. Active Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .313 15.1 The Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .314 15.2 Using the Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .316 15.3 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .324 16. Thread Pools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .325 16.1 Understanding Thread Pools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .325 16.2 Half-Sync/Half-Async Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .326 16.3 Leader/Followers Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .338 16.4 Thread Pools and the Reactor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .343 16.5 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .346 Part IV: Advanced ACE 347 17. Shared Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .349 17.1 ACE_Malloc and ACE_Allocator . . . . . . . . . . . . . . . . . . . . . . . . . . .350 17.2 Persistence with ACE_Malloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .352 17.3 Position-Independent Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . .356 17.4 ACE_Malloc for Containers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .359 17.5 Wrappers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .374 17.6 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .376 Contents ix 18. ACE Streams Framework. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .377 18.1 Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .377 18.2 Using a One-Way Stream. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .378 18.3 A Bidirectional Stream. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .397 18.4 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .417 19. ACE Service Configurator Framework . . . . . . . . . . . . . . . . . . .419 19.1 Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .420 19.2 Configuring Static Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .420 19.3 Setting Up Dynamic Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .427 19.4 Setting Up Streams. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .430 19.5 Reconfiguring Services During Execution. . . . . . . . . . . . . . . . . . . . .431 19.6 Using XML to Configure Services and Streams . . . . . . . . . . . . . . . .433 19.7 Configuring Services without svc.conf. . . . . . . . . . . . . . . . . . . . . . . .434 19.8 Singletons and Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .434 19.9 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .435 20. Timers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .437 20.1 Timer Concepts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .437 20.2 Timer Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .439 20.3 Prebuilt Dispatchers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .447 20.4 Managing Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .450 20.5 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .455 21. ACE Naming Service. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .457 21.1 The ACE_Naming_Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .457 21.2 A Single-Process Naming Context: PROC_LOCAL. . . . . . . . . . . . .459 21.3 Sharing a Naming Context on One Node: NODE_LOCAL. . . . . . . .468 21.4 Sharing a Naming Context across the Network: NET_LOCAL . . . .476 21.5 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .478 Bibliography. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .479 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .481

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.