Go Back   ZeroC Forums > Help Center

Reply
 
LinkBack Thread Tools Rate Thread Display Modes
  #1 (permalink)  
Old 11-01-2004
wwwtiger wwwtiger is offline
Registered User
 
 
Join Date: Oct 2004
Posts: 10
Is there a AMD demo project?

I just change printer example to use AMI/AMD,

interface Printer
{
["ami","amd"] void printString(string s,out string reply);
};

When use AMI it works, but use AMD following the guide from ICE manual, client program report :
printer failed: .\Connection.cpp:208: Ice::CommunicatorDestroyedException:
communicator object destroyed

server side code:
class Job : public IceUtil::Shared
{
public:
Job(const AMD_Printer_printStringPtr &,const ::std::string& );
void execute();
private:
bool setReply();
AMD_Printer_printStringPtr _cb;
string _s;
};
typedef IceUtil::Handle<Job> JobPtr;


Job::Job(const AMD_Printer_printStringPtr & cb,const ::std::string& s):_cb(cb), _s(s)
{
}

bool Job::setReply()
{
_s.append(" welcome to AMD!");
return true;
}

void Job::execute()
{
if(!setReply())
{
return;
}
_cb->ice_response(_s);
}


class PrinterI : virtual public Printer,virtual public IceUtil::Mutex
{
public:
virtual void printString_async(const AMD_Printer_printStringPtr &, const ::std::string&, const Ice::Current &);
private:
::std::list<JobPtr> _jobs;
};

void PrinterI:rintString_async(const AMD_Printer_printStringPtr & cb, const ::std::string& s, const Ice::Current &)
{
IceUtil::Mutex::Lock sync(*this);
JobPtr job = new Job(cb, s);
_jobs.push_back(job);
}

And I think AMD do not affect client code, so the client is the same code using AMI.

client code:
ic = Ice::initialize(argc, argv);
Ice::ObjectPrx base = ic->stringToProxy("SimplePrinter:default -p 10000");
PrinterPrx printer = PrinterPrx::checkedCast(base);
if (!printer)
throw "Invalid proxy";

AMI_Printer_printStringPtr cb = new AMI_Printer_printStringI;
printer->printString_async(cb, "Scott");



Is there something wrong in the code? Maybe I can fix the problem by myself if I can get a AMD demo project, but I can't find it in demos shifted with ICE.
Reply With Quote
  #2 (permalink)  
Old 11-02-2004
mes's Avatar
mes mes is online now
ZeroC Staff
 
Name: Mark Spruiell
Organization: ZeroC, Inc.
Project: Ice Developer
 
Join Date: Feb 2003
Location: California
Posts: 971
Hi,

Sorry, we don't currently provide an example that demonstrates AMD, although several of the Ice tests make use of AMD. For example, see the code in test/Ice/operations.

I suspect the reason your client receives a CommunicatorDestroyedException is because your client is not waiting for the AMI request to complete before it destroys its communicator. However, without seeing the complete source code, it's impossible for us to know for sure.

Take care,
- Mark
Reply With Quote
  #3 (permalink)  
Old 11-02-2004
wwwtiger wwwtiger is offline
Registered User
 
 
Join Date: Oct 2004
Posts: 10
Thank you, Mark

So whether server side use AMD or not, it do not affect client side(sychronize call or AMI), right? That means after deploy the client product, I can change the way server works without update the client side.
Reply With Quote
  #4 (permalink)  
Old 11-02-2004
marc's Avatar
marc marc is offline
ZeroC Staff
 
Name: Marc Laukien
Organization: ZeroC, Inc.
Project: The Internet Communications Engine
 
Join Date: Feb 2003
Location: Florida
Posts: 1,781
This is correct, it doesn't matter to the client if the server uses AMD or regular dispatch, and it dosn't matter to the server if the client uses AMI or regular invocations.
Reply With Quote
  #5 (permalink)  
Old 11-02-2004
wwwtiger wwwtiger is offline
Registered User
 
 
Join Date: Oct 2004
Posts: 10
I know the reason cause AMD client failed. In the ICE manual it mentioned "the response thread" but have no code about it, just push "job" into a job list, so I added a work thread read "job" out and execute, and client side wait for the reply before exit main thread. Finally it works.

By the way, would you please add response thread code in the ICE manual in future, or provide a AMD demo? I think many beginner may make same mistake like me.

Thanks for you help!
Reply With Quote
  #6 (permalink)  
Old 02-03-2005
chenhong_sz chenhong_sz is offline
Registered User
 
 
Join Date: Nov 2003
Location: China.Shenzhen
Posts: 35
Hello wwwtiger,Can you show me the sample code about the question you fix the error. I raise same error with you in this example.
We hope the manual next version can provide an example code for AMD
Reply With Quote
  #7 (permalink)  
Old 02-03-2005
matthew's Avatar
matthew matthew is offline
ZeroC Staff
 
Name: Matthew Newhook
Organization: ZeroC, Inc.
Project: Internet Communications Engine
 
Join Date: Feb 2003
Location: NL, Canada
Posts: 1,060
Quote:
Originally Posted by chenhong_sz
Hello wwwtiger,Can you show me the sample code about the question you fix the error. I raise same error with you in this example.
We hope the manual next version can provide an example code for AMD
What you are likely doing is calling an AMI method, and not waiting for a response before your client terminates. For example:

Code:
//slice
interface Foo
{
   ["ami"] void bar();
}

//C++
int
main(int argc, char** argv)
{
    ...
    FooPrx foo = ...;

    AMI_Foo_barPtr cb = new AMI_Foo_barI;
    foo->bar_async(cb);
    communicator->destroy();
    return EXIT_SUCCESS;
}
This example would cause the above problem in most cases because main doesn't wait for the callback to be actually called before destroying the communicator.

Regards, Matthew
Reply With Quote
  #8 (permalink)  
Old 02-03-2005
chenhong_sz chenhong_sz is offline
Registered User
 
 
Join Date: Nov 2003
Location: China.Shenzhen
Posts: 35
Sorry for my not clear description.
I had test the ami sample as manual , It is work fine. my question is about the amd. i do a sample it use amd ,like wwwtiger's code ,it build ok but when i call the amd method in client , client and server will no response. and when i press ctrl+c in client side, it show these message:\Connection.cpp:202: Ice::CommunicatorDestroyedException:communicator object destroyed.as wwwtiger said that maybe program no thread read "job" out and execute. as follow code:
...
IceUtil::Mutex::Lock sync(*this);
JobPtr job = new Job(cb, s);
_jobs.push_back(job);

it add a new job into a queue,but i can't find some code that read the job in manual.

if we must add some code that will new some thread to read out the job and run it? if yes, can you show me these code ? thank you.
Reply With Quote
  #9 (permalink)  
Old 02-03-2005
mes's Avatar
mes mes is online now
ZeroC Staff
 
Name: Mark Spruiell
Organization: ZeroC, Inc.
Project: Ice Developer
 
Join Date: Feb 2003
Location: California
Posts: 971
The example in demo/IceUtil/workqueue shows one way of structuring a work queue using the threading and synchronization classes provided with Ice.

Take care,
- Mark
Reply With Quote
  #10 (permalink)  
Old 02-03-2005
wwwtiger wwwtiger is offline
Registered User
 
 
Join Date: Oct 2004
Posts: 10
I just add a Job thread, and start it on main function

class JobThread : public IceUtil::Thread
{
virtual void run()
{
while(1)
{
_jobMutex.lock();

if(_jobs.size()!=0)
{
JobPtr firstJob=(JobPtr)_jobs.front();
_jobs.pop_front();
firstJob->execute();
}
_jobMutex.unlock();
Sleep(100);
}
}
};

====
//Start job
IceUtil::ThreadPtr t = new JobThread;
IceUtil::ThreadControl tc = t->start(); // Start it



Have a nice day!
Reply With Quote
  #11 (permalink)  
Old 02-03-2005
chenhong_sz chenhong_sz is offline
Registered User
 
 
Join Date: Nov 2003
Location: China.Shenzhen
Posts: 35
Hi,wwwtiger,Thanks for your help,.

but i am confused that why ice run time not to implement like AMI?(AMI will auto new a thread to wait for the response when call the ami asyn call). Because i think if done it and it will can make good use of the server's threadpool advantage to finish these job . but now if these code is must been added manual ,I think I maybe will make mistake and is not effective if i don't take care .if AMD only provide the routine of asyn call? for example: callback class and asyn function. but the job queue and executed thread we must add it manual?

Last edited by chenhong_sz : 02-03-2005 at 11:44 PM.
Reply With Quote
  #12 (permalink)  
Old 02-04-2005
benoit's Avatar
benoit benoit is online now
ZeroC Staff
 
Name: Benoit Foucher
Organization: ZeroC, Inc.
Project: Ice
 
Join Date: Feb 2003
Location: Rennes, France
Posts: 1,540
If you want the Ice server thread pool to do all the job, don't use AMD It's important to understand that AMD and AMI are different concepts and therefore their implementation is totally different.

With AMI, you make asynchronous invocations. You invoke on an object and the invocation will return as soon as the request has been sent over the wire. The calling thread doesn't have to wait for the answer, instead, when the answer is available you'll get a callback through the AMI callback object. Note also that AMI doesn't create any new threads to wait for the answer (it's the Ice client thread pool which is taking care of reading answers from client requests and eventually call on AMI callbacks).

With AMD, you asynchronously dispatch method invocations. The implementation of a method can release the dispatch thread whenever it wants and send the answer of the method invocation latter from another thread. It's up to you to decide when to send the answer:
  • You might want to do what wwwtiger is doing, i.e.: queue job items that are executed by another thread and send the answer of the invocation when the job item execution completes.
  • You might also want to make an AMI call on an object and and send the answer for the AMD invocation when you receive the answer of the AMI call.
  • You might want to wait for a resource to become available without holding the dispatch thread.
And I'm sure there's many more use cases!

Benoit.
Reply With Quote
  #13 (permalink)  
Old 06-10-2005
OrNot OrNot is offline
Registered User
 
Name: Bin.Li
Organization: GE Healthcare
Project: Enterprise solution
 
Join Date: Jun 2005
Location: Shanghai
Posts: 157
Send a message via MSN to OrNot
-->
Recently,I devot all my time to learn ICE and like it more and more. However, I am often baffled by some concepts, such as AMI\AMD. I just feel strange why not give plenty of UML diagrams in the doucument,which can definitely explain the details more clearly .

So far as this thread, I will feel life easier as a ICE beginner if you can give us a campact and whole printer example to demonstrate the usage of AMI\AMD. It must be easy to you, but It can give me, a newbie , a comprehensive concepts about it. I read those examples in Test/Operations, but, frankly speaking, not like them and lost in them.

But anyway, thank your execellent work. I just wish to master it as soon as possible.


Best regards
Reply With Quote
  #14 (permalink)  
Old 06-10-2005
marc's Avatar
marc marc is offline
ZeroC Staff
 
Name: Marc Laukien
Organization: ZeroC, Inc.
Project: The Internet Communications Engine
 
Join Date: Feb 2003
Location: Florida
Posts: 1,781
The next issue of Connections will have an article about the use of AMI/AMD.
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
A problem of using ICE in a C# project. rano Help Center 1 07-12-2006 03:49 AM
Is there any open source project based on ICE? ffmpeg4nmm Comments 3 05-28-2006 11:23 PM
Wish project dropped alfredt Comments 0 01-09-2005 11:23 PM
This is my executable project of 'Hello World'!^_^ kane Help Center 0 10-08-2004 02:28 AM
Visual Studio 6 project files for Ice gmueckl Help Center 2 09-02-2003 04:18 AM


All times are GMT -4. The time now is 04:33 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
(c) 2008 ZeroC, Inc.