Ice entry instance -- ping and obtain remote time

Source: Internet
Author: User

Although traditional socket programming is used, we can be more clear aboutProgramPerformance, can more directly control the socket settings, such as sending timeout time, accept the buffer size, and perform their own protocol encryption. However, due to its high debugging cost and difficulty in distributed deployment

As a middleware technology, ice has been widely used. Since the epoll model was adopted after ice3.2, the performance of the communication layer was significantly improved. In addition, it is easier to debug than socket programming. This is undoubtedly a great temptation for programmers.

There are a lot of documents about the principle of ice on the Internet, so I will not talk nonsense. Start with a simple instance to get started with ice programming:

Create a testice. Ice File

# Ifndef _ h_test_wg_ice_h _
# DEFINE _ h_test_wg_ice_h _

Module WG
{
Interface wgtestice
{
Int Ping ();
Int gettime (out string strtime );
};
};

# Endif

We need to create a client and a server.

The server file is as follows:

Testiceimpl. h

# Include <iostream>
# Include <string>
# Include "testice. H"
# Include <sys/time. h>
Using namespace WG;
Class ctesticeimpl: Public wgtestice
{
Public:
Ctesticeimpl (){}
~ Ctesticeimpl (){}

Int Ping (const ice: Current & = ice: Current ())
{
STD: cout <"Ping successfully." <STD: Endl;
Return 0;
}

Int gettime (STD: string & strtime, const ice: Current & = ice: Current ())
{
Time_t TT = Time (null );
Struct TM TM1;
Struct TM * pnow;

Pnow = localtime_r (& TT, & TM1 );
If (! Pnow)
{
Strtime = "error ";
Return-1;
}
Char Buf [32] = {0 };
Sprintf (BUF, "% 04d-% 02d-% 02d % 02d: % 02d: % 02d ",
Pnow-> maid + 1900,
Pnow-> tm_mon + 1,
Pnow-> tm_mday,
Pnow-> tm_hour,
Pnow-> tm_min,
Pnow-> tm_sec );
Strtime = Buf;
Return 0;
}

};
# Endif

The above class derives an interface class, which is used for server implementation.

Create a main function file testserver. cpp'

# Include <ice/ice. h>
# Include "testice. H"
# Include "testiceimpl. H"

Int createsrv ()
Try
{
Int tmpargc = 0;
Char ** tmpargv = NULL;

Ice: communicatorptr IC;
Ic = ice: Initialize (tmpargc, tmpargv );

Ice: objectadapterptr adapter = IC-> createobjectadapterwithenderson points ("wgsrv", "default-P 7788 ″);
Ice: objectptr object = new ctesticeimpl ();
Adapter-> Add (object, IC-> stringtoidentity ("wginterfaceagentid "));

Adapter-> activate ();

STD: cout <"Now Server start successfully." <STD: Endl;

IC-> waitforshutdown ();

If (IC)
{< br> STD: cout <"Now ice server exit. " IC-> destroy ();
}

}
Catch (const ice: exception & Ex)
{
STD: cout <"catch ice exception," <ex <STD: Endl;
Return-1;
}
Catch (...)
{
STD: cout <"catch unknown exception." <STD: Endl;
Return-1;
}
Int main (INT argc, char ** argv)
{
STD: cout <"before to create SRV" <STD: Endl;
Return createsrv ();

}

Next, create the client test file testclient. cpp.
# Include <iostream>
# Include <ice/ice. h>
# Include "testice. H"

Ice: communicatorptr pcommunicator;
Ice: objectprx pobject;
WG: wgtesticeprx pproxy;

Const char * menu =
* Test ice client ******************* * *** \ n"
"\ T 1 Ping \ n"
"\ T 2 gettime \ n"
"\ T q quit \ n"
"************************************ * **************** \ N ";

Void showmenu ()
{
STD: cout <menu <STD: Endl;
Return;
}
Int domenu ()
{
Int ret = 0;
Char C;
STD: String strtime;

Showmenu ();
C = getchar ();

While (C! = 'Q ')
{
Switch (c)
{
Case '1 ′:
Ret = pproxy-> Ping ();
STD: cout <"Ping RET is" <RET <STD: Endl;
Break;
Case '2 ′:
Ret = pproxy-> gettime (strtime );
STD: cout <"gettime RET is" <
RET <"rettime is" <strtime <STD: Endl;
Break;
Case 'q ':
Return 0;
Break;
Default:
Break;

}

sleep (2);
showmenu ();
// system ("clear");
C = getchar ();
}< br> return 0;
}

Int main (INT argc, char ** argv)
Try
{
If (argc <4)
{
STD: cout <"argc less than 4. example is:
Testclient wginterfaceagentid 127.0.0.1 7788 "<STD: Endl;
Return-1;
}
Char Buf [128] = {0 };
Snprintf (BUF, sizeof (BUF)-1, "% s: default-H % s-p % s", argv [1], argv [2], argv [3]);

Int tmpargc = 0;
Char ** tmpargv = NULL;

Pcommunicator = ice: Initialize (tmpargc, tmpargv );
Pobject = pcommunicator-> stringtoproxy (BUF );

Pproxy = WG: wgtesticeprx: checkedcast (pobject );

Return domenu ();

}
Catch (const ice: exception & Ex)
{
STD: cout <"catch ice exception," <ex <STD: Endl;
Return-1;
}
Catch (...)
{
STD: cout <"catch unknown exception." <STD: Endl;
Return-1;
}

Then compile server_objs = testice. O \
Testserver. o

Client_objs = testice. O \
Testclient. o

You can test it.

Start the server first and then start the client. The effect is as follows:

* *************** Test ice client ******************** ***
1 Ping
2 gettime
Q quit
**************************************** ****************

2
Gettime RET is 0 rettime is 20:15:30

Okay, so the first step is complete.

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.