/* FILE NAME * Written by A. Wehrman * Copyright (C) 1996 * by Structural Dynamics Research Corp. * all rights reserved * * DESCRPTION * This program is an simple example on how to the Command object and how * to use callbackes to obtain asynchronous notification of events to * obtain the errors and results of the command sent. This program merely * prompts user to enter prompt in terminal window. * * HISTORY * Modified by B. Bettig to make it simpler to read. Removed a lot of error handling code. */ #include #include #include #include #include #include #include #include #include #include #include #include #include // Think of a struct as a class without functions. // This struct is used to group our data, so we can pass it as one variable. struct mydata { OI_Event *events; // event handler OI_Server_ptr oiServer; // Open I-DEAS Server OI_CommandServer_ptr command; // Open I-DEAS Command Server OI_AccessControlServer_ptr accessControl; // Open I-DEAS Access Control }; // CORBA::... classes are from CORBA. OI_... classes are from the I-DEAS Open Architecture. // These functions are defined at the end. The latter 4 are "callback functions". // They are event handling functions that are called by I-DEAS. static void asySendCommand(mydata *); static void asyInitialize(mydata *); static void asyCleanup(mydata *); static void asyExit(OI_Base_ptr , void *, const OI_EventData &); static void asyErrorSent(OI_Base_ptr , void *, const OI_EventData &); static void asyMessageSent(OI_Base_ptr , void *, const OI_EventData &); static void asyCompleted(OI_Base_ptr , void *, const OI_EventData &); int main(int argc, char *argv[]) { OI_ErrorCode status; mydata asy; char *ans = NULL; asyInitialize(&asy); // connect to oi server and get GUI and Command objects etc. try // This is a C++ keyword for error handling. { // get the server object asy.oiServer = OI_Connect(argc, argv); // asy.connected = TRUE; // get a copy of I-DEAS command server object asy.command = asy.oiServer->GetCommandServer(); // only allow us to control I-DEAS asy.accessControl = asy.oiServer->GetAccessControlServer(); asy.accessControl->LockServer(); asy.command->DisableOutput(); // register callbacks for events in which interested asy.events = new OI_Event(asy.oiServer); asy.events->AddCallback(OIE_ServerExiting, (OI_EventCallback)asyExit, &asy, FALSE); asy.events->AddCallback(OIE_TaskSwitch, (OI_EventCallback)asyExit, &asy, FALSE); asy.events->AddCallback(OIE_ApplicationSwitch, (OI_EventCallback)asyExit, &asy, FALSE); asy.events->AddCallback(OIE_CommandCompleted, (OI_EventCallback)asyCompleted, &asy, TRUE); asy.events->AddCallback(OIE_SendList, (OI_EventCallback)asyMessageSent, &asy, FALSE); asy.events->AddCallback(OIE_SendError, (OI_EventCallback)asyErrorSent, &asy, TRUE); } // handle any exceptions that were thrown catch (CORBA::SystemException& se) { // asy.connected = FALSE; asyCleanup(&asy); cout << "CORBA Failure" << &se << endl; exit(EXIT_FAILURE); } catch(...) { asyCleanup(&asy); cout << "Unexpected exception in OI Server Conection and Setup" << endl; exit(EXIT_FAILURE); } // input command and sent it to I-DEAS asySendCommand(&asy); // Go into endless loop to process events. When previous command has been processed then the OIE_CommandCompleted event will occur. // This will result in asyCompleted() being called, which calls asySendCommand() again. OI_OrbixProcessEventLoop(); // This return is never reached. return 0; } /* NAME * asyInitialize * * DESCRIPTION * this utility initializes a mydata structure */ static void asyInitialize(mydata *asy) { asy->oiServer = OI_Server::_nil(); asy->command = OI_CommandServer::_nil(); asy->accessControl = OI_AccessControlServer::_nil(); } /* NAME * asyCleanup * * DESCRIPTION * this utility cleans up memory, disconects from OI_Server, etc. */ static void asyCleanup(mydata *asy) { try { if (!CORBA::is_nil(asy->accessControl)) CORBA::release(asy->accessControl); if (!CORBA::is_nil(asy->command)) CORBA::release(asy->command); } catch(CORBA::SystemException& se) { cout << "CORBA Failure" << &se << endl; } catch(...) { cout << "Unexpected exception in OI Server Conection and Setup" << endl; } asy->oiServer = NULL; asy->command = NULL; asy->accessControl = NULL; } /* NAME * asyExit * * DESCRIPTION * This function is called when server goes away (if only temporarily) */ static void asyExit(OI_Base_ptr baseObject, void *clientData, const OI_EventData &eventData) { mydata *asy = (mydata *)clientData; switch (eventData.event) { case OIE_ServerExiting: cout << "Server Exiting" << endl; break; case OIE_TaskSwitch: cout << "Task Switch" << endl; break; case OIE_ApplicationSwitch: cout << "Application Switch" << endl; break; } asyCleanup(asy); exit(EXIT_SUCCESS); } /* NAME * asyErrorSent * * DESCRIPTION * This function is called when Error was sent from ideas */ static void asyErrorSent(OI_Base_ptr baseObject, void *clientData, const OI_EventData &eventData) { mydata *asy = (mydata *)clientData; OI_ErrorCode status; try { eventData.anyData >>= status; cout << (char *)asy->oiServer->GetErrorMessage(status) << endl; } catch(CORBA::SystemException& se) { cout << "CORBA Failure" << &se << endl; } catch(...) { cout << "Unexpected exception in OI Server Conection and Setup" << endl; } } /* NAME * asyMessageSent * * DESCRIPTION * This function is called when message was sent from ideas */ static void asyMessageSent(OI_Base_ptr baseObject, void *clientData, const OI_EventData &eventData) { char *result; eventData.anyData >>= result; cout << result << endl; } /* NAME * asyCompleted * * DESCRIPTION * This function is called when last command sent has completed. It prompts * user to send another command */ static void asyCompleted(OI_Base_ptr baseObject, void *clientData, const OI_EventData &eventData) { mydata *asy = (mydata *)clientData; asySendCommand(asy); } /* NAME * asySendCommand * * DESCRIPTION * It obtains command from user and sends it to the OI_Server. */ static void asySendCommand(mydata *asy) { char command[1024]; char *ptr; cout << "Enter Command or 'quit' to exit" << endl; if (fgets(command, sizeof command, stdin) == NULL) { cout << "error reading input " << endl; asyCleanup(asy); #ifdef WINNT Sleep(2 * 1000); #else sleep(2); #endif exit(EXIT_FAILURE); } if ((ptr = strchr(command, '\n'))) *ptr = '\0'; if (strcmp(command, "quit") == 0) { cout << "Exiting example as requested" << endl; asyCleanup(asy); #ifdef WINNT Sleep(2 * 1000); #else sleep(2); #endif exit(EXIT_SUCCESS); } try { asy->command->SendCommand(command); } catch(CORBA::SystemException& se) { cout << "CORBA Failure" << &se << endl; asyCleanup(asy); #ifdef WINNT Sleep(2 * 1000); #else sleep(2); #endif exit(EXIT_FAILURE); } catch(...) { cout << "Unexpected exception in OI Server Conection and Setup" << endl; asyCleanup(asy); #ifdef WINNT Sleep(2 * 1000); #else sleep(2); #endif exit(EXIT_FAILURE); } }