Getting started with Ace dynamic service configuration in Visual C ++

Source: Internet
Author: User

Getting started with Ace dynamic service configuration in Visual C ++
Abstract:
Dynamic service configuration has obvious advantages in writing server applications. This article briefly introduces the use of visual C ++ (7.1)
Write the ace dynamic service step.
This article applies to beginners of ACE.

1. Main Process

1.1 create a main program
Use Viusal studio to create a Win32 console project named glivr86serviced. Note:
D Indicates daemon, not debug. This project will be transformed into nt_service in the future (subsequent steps will be introduced.
1.2 modify project attributes (configuation properties)
1.2.1 Add the main file glivr86serviced. cpp to the project. The objective is to add the C/C ++ option to the project properties.
1.2.1 General modify program output path $ (outdir)
1.2.2.debugging command arguments:-D, started in debug mode
1.2.3 C/C ++ settings
1.2.3.1 additional include directories/I [path]: $ (ace_root );
1.2.3.2 code generation:/MTD;/MTD for mt debugging and MT for release
1.2.3.3 Preprocessor: preprocesor definitions/D: Win32; _ debug; _ console;
This is the debug version. Change _ debug to ndebug.
1.2.4 link settings
1.2.4.1 input: additinal dependencise: ACE (d). Lib. for debugging, select aced. Lib,
Select Ace. Lib for the release.
1.2.4.2 system: subsystem/subsystem: Console; (/subsystem: Console)

Main program code

 

// @ File: glivr86serviced. cpp
// @ Description: IVR 86 main program entry of the business service
// @ Author: jiangtao
// @ Version: 2.0.0

# Include "stdafx. H"
# Include <memory> // use auto_ptr

# Include "ACE/OS _ns_unistd.h"
# Include "ACE/tp_reactor.h"
# Include "ACE/reactor. H"
# Include "ACE/service_config.h"
# Include "ACE/thread_manager.h"

// Thread Pool
Static ace_thr_func_return event_loop (void * Arg)
{
Ace_debug (lm_info, "(% p | % T), event_loop ()/n "));
Ace_reactor * reactor = static_cast <ace_reactor *> (ARG );

Reactor-> owner (ace_ OS: thr_self ());
Reactor-> run_reactor_event_loop ();
Return 0;
}

Int
Ace_tmain (INT argc, ace_tchar * argv [])
{

Const size_t n_threads = 4;
Ace_tp_reactor tp_reactor;
Ace_reactor Reactor (& tp_reactor );
Auto_ptr <ace_reactor> delete_instance (ace_reactor: instance (& reactor ));

If (ace_service_config: open (argc, argv) =-1)
Ace_error_return (lm_error,
Ace_text ("% P/N "),
Ace_text ("open ")),
1 );

Ace_thread_manager: instance ()-> spawn_n
(N_threads, event_loop, ace_reactor: instance ());

Ace_thread_manager: instance ()-> wait ();

Return 0;
}

2. Create a dynamic link library for the loaded Service
2.1 use Visual Studio to create a new project glivr86service. We still open it from the blank Win32 Console
Start
1.2.1 Add the main file glivr86service. cpp for the project. The objective is to add the C/C ++ option to the project properties.
1.2.1 General: configration type: changed to dynamic link library dynamic library (DLL)
1.2.3.1 additional include directories/I [path]: $ (ace_root );
1.2.3.2 code generation:/MTD;/MTD for mt debugging and MT for release
1.2.3.3 Preprocessor: preprocesor definitions/D:
Win32; _ debug; _ windows; ace_build_svc_dll
This is the debug version. Change _ debug to ndebug.
Note that the ace_build_svc_dll macro should be added here. If generate_export_file.pl is used
Generate a custom export header file. This macro can also be customized.

2.2.2 linker settings
2.2.2.1 input: additinal dependencise: ACE (d). Lib. Select aced. Lib for debugging,
Select Ace. Lib for the release.
2.2.2.2 system: subsystem/subsystem: Console; (/subsystem: Console)
2.2.2.3 General, output file:
../Glivr86serviced/glivr86serviced/glivr86serviced. dll

Enter the path or environment variable path in glivr86serviced
To facilitate debugging.

2.2.2.4 Adanced, import libaray: $ (outdir)/glivr86serviced. Lib
The above is the debug version. The release version can remove the suffix D, that is
$ (Outdir)/glivr86service. Lib

3. service dynamic link library implementation

3.1 add two files for the project to declare and implement Service Factory respectively
// @ File: servicefactory. h
// @ File: servicefactory. cpp
The Code is as follows:

 

// @ File: servicefactory. h
// @ Description: IVR 86 Business Service
// @ Author: jiangtao
// @ Data: 2006-7-3
// @ Version: 1.0.0

# Ifndef servicefactory_h
# Define servicefactory_h
# Include "ACE/svc_export.h"
# Include "ACE/service_config.h"
# Include "ACE/service_object.h"

// Declare the Service Factory
Ace_svc_factory_declare (servicefactory_t)

Class ace_svc_export servicefactory_t: Public ace_service_object
{
Public:
/// Initializes object when Dynamic Linking occurs.
Virtual int Init (INT argc, ace_tchar * argv []);

/// Terminates object when dynamic unlinking occurs.
Virtual int Fini (void );

/// Returns information on a service object.
Virtual int Info (ace_tchar ** info_string, size_t length = 0) const;

};

# Endif/* servicefactory_h */

/*************************************** ****************************/

// @ File: servicefactory. cpp

# Include "servicefactory. H"
# Include "ACE/log_msg.h"
// Implement the Service Factory
Ace_svc_factory_define (servicefactory_t)

Int servicefactory_t: Init (INT argc, ace_tchar * argv [])
{
Ace_debug (lm_info, "(% p | % t) Service initialization completed/N "));
Return 0;
}

Int servicefactory_t: Info (ace_tchar ** STRP, size_t length) const
{
Ace_debug (lm_info, "servicefactory_t: Info ()/n "));
Return 0;
}

Int servicefactory_t: Fini (void)
{
Return 0;
}

5. service configuration file SVC. conf
Dynamic ivr86service service_object * glivr86service: _ make_servicefactory_t () Active

6. Running result

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.