Server.cpp

00001 
00002 #include <IceE/IceE.h>
00003 #include <IRobotI.h>
00004 
00005 #include <unistd.h>
00006 #include <fcntl.h>
00007 #include <sys/types.h>
00008 #include <sys/stat.h>
00009 
00010 using namespace std;
00011 using namespace Robots;
00012 
00013 
00014 int run(int argc, char* argv[], const Ice::CommunicatorPtr& communicator)
00015 {
00016   Ice::ObjectAdapterPtr adapter =
00017     communicator->createObjectAdapterWithEndpoints("IRobotAdapter", "tcp -p 10000");
00018   Ice::ObjectPtr object = new IRobotI(1);
00019   adapter->add(object, communicator->stringToIdentity("IRobotService"));
00020   adapter->activate();
00021   communicator->waitForShutdown();
00022   return EXIT_SUCCESS;
00023 }
00024 
00025   int
00026 main(int argc, char* argv[])
00027 {
00028   int status;
00029   bool daemon = false;
00030   bool noclose = false;
00031   Ice::CommunicatorPtr communicator;
00032 
00033 
00034   try
00035   {
00036     Ice::InitializationData initData;
00037     //initData.properties = Ice::createProperties();
00038     //initData.properties->load("config");
00039     //initData.properties->setProperty("Ice.Override.Timeout", "100");
00040     communicator = Ice::initialize(argc, argv, initData);
00041 
00042     if (daemon)
00043     {
00044       printf("Running as a daemon\n");
00045       //Become a daemon
00046       // fork off the parent process
00047       pid_t pid = fork();
00048       if (pid < 0)
00049       {
00050         printf("Can not fork\n");
00051         exit(1);
00052       }
00053 
00054       if (pid > 0)
00055         exit(0); //exit the parent process
00056 
00057       // Change the file mask
00058       umask(0);
00059 
00060       //Create a new system id so that the kernel wont think we are an orphan.
00061       pid_t sid = setsid();
00062       if (sid < 0)
00063       {
00064         printf("Can not become independent\n");
00065         exit(1);
00066       }
00067 
00068       if (!noclose)
00069       {
00070         fclose(stdin);
00071         fclose(stdout);
00072         fclose(stderr);
00073       }
00074 
00075     }
00076 
00077     status = run(argc, argv, communicator);
00078   }
00079   catch(const Ice::Exception& ex)
00080   {
00081     fprintf(stderr, "%s\n", ex.toString().c_str());
00082     status = EXIT_FAILURE;
00083   }
00084 
00085   if(communicator)
00086   {
00087     try
00088     {
00089       communicator->destroy();
00090     }
00091     catch(const Ice::Exception& ex)
00092     {
00093       fprintf(stderr, "%s\n", ex.toString().c_str());
00094       status = EXIT_FAILURE;
00095     }
00096   }
00097 
00098   return status;
00099 }
00100 
Generated on Sun May 8 08:41:21 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3