jni_result jni_nameServiceFree() { u_result r; jni_mapping mapping; r = U_RESULT_OK; if(ns != NULL){ ns->refCount--; if(ns->refCount == 0){ mapping = jni_mapping(c_iterTakeFirst(mappings)); while(mapping != NULL){ os_free(mapping->uri); os_free(mapping); mapping = jni_mapping(c_iterTakeFirst(mappings)); } c_iterFree(mappings); os_free(ns); ns = NULL; r = u_userDetach(); } else{ r = U_RESULT_OK; } } else{ r = U_RESULT_NOT_INITIALISED; } return jni_convertResult(r); }
int main (int argc, char *argv[]) { int opt; int interval = 3000; int sampleCount = 0; c_bool extended = FALSE; c_bool raw = FALSE; c_bool delta = FALSE; c_bool preallocated = FALSE; char *uri = ""; u_result ur; u_participant participant; v_participantQos pqos; int no_break = TRUE; char c; int lost; const char* sddsURI = NULL; monitorMode selectedAction = memoryStats; monitor_ms msData = NULL; monitor_trc trcData = NULL; monitor_orc orcData = NULL; int delay = 0; int trigger = 0; int sample = 0; c_long objectCountLimit = 0; char *filterExpression = NULL; HANDLE handle = GetStdHandle(STD_INPUT_HANDLE); INPUT_RECORD buffer[1]; DWORD events; orderKind selectedOrdering = NO_ORDERING; int orderCount = INT_MAX; while (((opt = getopt (argc, argv, optflags)) != -1) && !errno) { switch (opt) { case 'i': if (!(sscanf (optarg, "%d", &interval)) > 0) { fprintf(stderr, "mmstat: Not a valid interval.\n"); print_usage(); exit(-1); } break; case 's': if (!(sscanf (optarg, "%d", &sampleCount)) > 0) { fprintf(stderr, "mmstat: Not a valid sample count.\n"); print_usage(); exit(-1); } break; case 'l': if (!(sscanf (optarg, "%d", &objectCountLimit)) > 0) { fprintf(stderr, "mmstat: Not a valid limit.\n"); print_usage(); exit(-1); } break; case 'f': filterExpression = optarg; break; case 'e': extended = TRUE; break; case 'r': raw = TRUE; break; case 'a': preallocated = TRUE; break; case 'm': selectedAction = memoryStats; break; case 'M': selectedAction = memoryStats; delta = TRUE; break; case 't': selectedAction = typeRefCount; break; case 'T': selectedAction = typeRefCount; delta = TRUE; break; case 'o': if (selectedOrdering == NO_ORDERING && strlen(optarg) == 1) { switch (optarg[0]) { case 'C': selectedOrdering = ORDER_BY_COUNT; break; case 'S': selectedOrdering = ORDER_BY_SIZE; break; case 'T': selectedOrdering = ORDER_BY_TOTAL; break; default: fprintf(stderr, "mmstat: Unknown ordering kind.\n"); print_usage(); exit(-1); } } else { fprintf(stderr, "mmstat: Cannot specify multiple orderings at the same time.\n"); print_usage(); exit(-1); } break; case 'n': if (!(sscanf (optarg, "%d", &orderCount)) > 0) { fprintf(stderr, "mmstat: Not a valid ordering nrEntries.\n"); print_usage(); exit(-1); } break; #ifdef OBJECT_WALK case 'r': selectedAction = objectRefCount; break; case 'R': selectedAction = objectRefCount; delta = TRUE; break; #endif case 'h': case '?': default: print_usage (); exit(0); break; } } if (errno) { fprintf(stderr, strerror(errno)); fprintf(stderr, "\n"); print_usage(); exit (-1); } if ((argc - optind) > 1) { fprintf(stderr, "Too many arguments"); print_usage (); exit (-1); } if (selectedAction == memoryStats) { if (objectCountLimit > 0) { fprintf(stderr, "Can't use object limit in memory stats mode.\n"); print_usage(); exit(-1); } if (filterExpression != NULL) { fprintf(stderr, "Can't use filter expression in memory stats mode.\n"); print_usage(); exit(-1); } } else { if (extended) { fprintf(stderr, "Extended mode can only be used in memory stats mode.\n"); print_usage(); exit(-1); } if (preallocated) { fprintf(stderr, "Preallocated memory can only be shown in memory stats mode.\n"); print_usage(); exit(-1); } } if ((argc - optind) == 1) { uri = argv[optind]; } if( !raw) { if(strlen(uri) > 0) { sddsURI = uri; } else { sddsURI = os_getenv ("OSPL_URI"); if(!sddsURI) { sddsURI = DOMAIN_NAME; } } printf("Trying to open connection with the OpenSplice system using URI:\n" \ "'%s'...\n", sddsURI); } else { sddsURI = uri; } ur = u_userInitialise(); if(ur == U_RESULT_OK) { pqos = u_participantQosNew(NULL); participant = u_participantNew(sddsURI, 30, "mmstat", (v_qos)pqos, TRUE); u_participantQosFree(pqos); if(participant) { if( !raw ) { printf("Connection established.\n\n"); } lost = 0; switch (selectedAction) { case memoryStats: msData = monitor_msNew (extended, raw, delta, preallocated); break; case typeRefCount: trcData = monitor_trcNew (objectCountLimit, filterExpression, selectedOrdering, orderCount, delta); break; case objectRefCount: orcData = monitor_orcNew (objectCountLimit, filterExpression, delta); break; } while (no_break && !lost) { if (delay <= 0 || trigger) { switch (selectedAction) { case memoryStats: ur = u_entityAction(u_entity(participant), monitor_msAction, msData); break; case typeRefCount: ur = u_entityAction(u_entity(participant), monitor_trcAction, trcData); break; case objectRefCount: ur = u_entityAction(u_entity(participant), monitor_orcAction, orcData); break; } fflush (stdout); sample++; if (trigger) { trigger = 0; } else { delay = interval; } } if(ur == U_RESULT_OK) { if (_isatty(_fileno(stdin)) && !raw) { c = '\0'; PeekConsoleInput( handle, buffer, 1, &events ); if(events > 0) { ReadConsoleInput(handle, buffer, 1, &events); if(buffer[0].EventType == KEY_EVENT && buffer[0].Event.KeyEvent.bKeyDown) { c = buffer[0].Event.KeyEvent.uChar.AsciiChar; } } if (c == 'q' || c == '\03' /* ^C */) { no_break = FALSE; } else if (c == 't') { trigger = 1; } PeekConsoleInput( handle, buffer, 1, &events ); if(events > 0) { ReadConsoleInput(handle, buffer, 1, &events); if(buffer[0].EventType == KEY_EVENT && buffer[0].Event.KeyEvent.bKeyDown) { c = buffer[0].Event.KeyEvent.uChar.AsciiChar; } } } if (no_break && interval) { delay -= 100; os_nanoSleep (SLEEP_INTERVAL); } } else { /* Participant is no longer accessible, terminate now... */ no_break = 0; lost = TRUE; } if (sampleCount && (sample == sampleCount)) { printf ("\nsample_count limit reached\n"); no_break = 0; } } PeekConsoleInput( handle, buffer, 1, &events ); if(events > 0) { ReadConsoleInput(handle, buffer, 1, &events); if(buffer[0].EventType == KEY_EVENT && buffer[0].Event.KeyEvent.bKeyDown) { c = buffer[0].Event.KeyEvent.uChar.AsciiChar; } } if(lost) { printf("\nConnection with domain lost. The OpenSplice system has\n" \ "probably been shut down.\n"); } u_participantFree(participant); } else { printf("Connection could NOT be established (creation of participant failed).\n"); printf("Is the OpenSplice system running?\n"); OS_REPORT(OS_ERROR,"mmstat", 0, "Creation of participant failed."); } u_userDetach(); switch (selectedAction) { case memoryStats: monitor_msFree (msData); break; case typeRefCount: monitor_trcFree (trcData); break; case objectRefCount: monitor_orcFree (orcData); break; } } else { printf("Connection could NOT be established (could not initialise).\n"); printf("Is the OpenSplice system running?\n"); OS_REPORT(OS_ERROR,"mmstat", 0, "Failed to initialise."); } printf("\nExiting now...\n"); return 0; }
int main( int argc, char* argv[]) { int result = 0; v_participantQos participantQos; u_result uresult; os_boolean success; v_subscriberQos subscriberQos; c_time resolution; c_base base; v_kernel kernel; /* Necessary to initialize the user layer. Do this just once per process.*/ mlv_init (); uresult = u_userInitialise(); mlv_setforreal (1); if(uresult == U_RESULT_OK){ /* Allocate default participant qos*/ participantQos = u_participantQosNew(NULL); { os_mutexAttr mattr; os_mutexAttrInit (&mattr); mattr.scopeAttr = OS_SCOPE_PRIVATE; os_mutexInit (&gluelock, &mattr); } if(participantQos){ if(argc > 1){ SERVICE_NAME = argv[1]; } if(argc > 2){ SERVICE_URI = argv[2]; } /*create participant*/ participant = u_participant(u_serviceNew( SERVICE_URI, 0, SERVICE_NAME, NULL, U_SERVICE_NETWORKING, (v_qos)participantQos)); if(participant){ struct cfgst *cfgst; ddsi2_participant_gid = u_entityGid (u_entity (participant)); /*Notify kernel that I am initializing. */ u_serviceChangeState(u_service(participant),STATE_INITIALISING); /*Start monitoring the splicedaemon state. I need to terminate if he says so*/ u_serviceWatchSpliceDaemon( u_service(participant), in_discoveryWatchSpliced, &terminate); if ((cfgst = config_init (participant, SERVICE_NAME)) != NULL) { unsigned rtps_flags = 0; struct nn_servicelease *servicelease; open_tracing_file (); /* Dependencies between default values is not handled automatically by the config processing (yet) */ if (config.many_sockets_mode) { if (config.max_participants == 0) config.max_participants = 100; } if (NN_STRICT_P) { /* Should not be sending invalid messages when strict */ config.respond_to_rti_init_zero_ack_with_invalid_heartbeat = 0; config.acknack_numbits_emptyset = 1; } config_print_and_free_cfgst (cfgst); servicelease = nn_servicelease_new (participant); nn_servicelease_start_renewing (servicelease); myNetworkId = getNetworkId (); u_entityAction(u_entity(participant), resolveKernelService, NULL); base = c_getBase(service); kernel = v_object(service)->kernel; rtps_init (base, kernel, config.domainId, config.participantIndex, rtps_flags, config.networkAddressString, config.peers); /* Initialize entity administration. */ success = in_entityAdminInit(participant); if(success){ /*Create subscriber to receive client writers' messages.*/ subscriberQos = u_subscriberQosNew(NULL); os_free(subscriberQos->partition); subscriberQos->partition = NULL; clientSubscriber = u_subscriberNew( participant, "clientSubscriber", subscriberQos, TRUE); if(clientSubscriber){ /*Create networkReader to be able to receive client writers' messages.*/ clientReader = u_networkReaderNew( clientSubscriber, "clientReader", NULL, TRUE); if(clientReader){ resolution.seconds = 0; resolution.nanoseconds = 10 * 1000 * 1000; /*10 ms*/ /*Create network queue*/ uresult = u_networkReaderCreateQueue( clientReader, 1000, 0, FALSE, FALSE, resolution, TRUE, &queueId, "DDSi"); if(uresult == U_RESULT_OK){ struct builtin_datareader_set drset; u_entityAction(u_entity(clientReader), resolveKernelReader, NULL); uresult = create_builtin_readers (&drset, participant); if (uresult == U_RESULT_OK) { u_serviceChangeState(u_service(participant),STATE_OPERATIONAL); uresult = attachAndMonitor(participant, &drset); if((uresult != U_RESULT_OK) && (uresult != U_RESULT_DETACHING)) { nn_log (LC_ERROR, "Abnormal termination...\n"); result = -1; } else { nn_log (LC_INFO, "Deleting entities...\n"); } destroy_builtin_readers (&drset); } else { nn_log (LC_FATAL, "Could not create subscription + readers for builtin topics.\n"); result = -1; } terminate = TRUE; v_networkReaderTrigger(vclientReader, queueId); os_threadWaitExit(clientWriterThread, NULL); } else { nn_log (LC_FATAL, "Could not create networkQueue.\n"); result = -1; } /*Clean up networkReader*/ os_mutexLock (&gluelock); u_networkReaderFree(clientReader); clientReader = NULL; vclientReader = NULL; os_mutexUnlock (&gluelock); } else { nn_log (LC_FATAL, "Could not create networkReader.\n"); result = -1; } /*Clean up subscriber*/ u_subscriberFree(clientSubscriber); } else { nn_log (LC_FATAL, "Could not create subscriber.\n"); result = -1; } /*Clean up entity administration*/ in_entityAdminDestroy(); } else { nn_log (LC_FATAL, "Could not initialize entity adminstration.\n"); result = -1; } /* RTPS layer now defines types, cleanup before detaching */ rtps_term(); /*Notify kernel that I've terminated*/ u_serviceChangeState(u_service(participant),STATE_TERMINATED); nn_servicelease_free (servicelease); /*Delete participant*/ uresult = u_serviceFree(u_service(participant)); if(uresult != U_RESULT_OK){ nn_log (LC_FATAL, "Deletion of participant failed.\n"); result = -1; } } else { nn_log (LC_FATAL, "Initialization of configuration failed.\n"); result = -1; } } else { nn_log (LC_FATAL, "Could not create participant.\n"); result = -1; } u_participantQosFree (participantQos); } else { nn_log (LC_FATAL, "Could not allocate participantQos.\n"); result = -1; } os_mutexDestroy (&gluelock); /* Detach user layer */ mlv_setforreal (0); uresult = u_userDetach(); mlv_fini (); if(uresult != U_RESULT_OK){ nn_log (LC_FATAL, "Detachment of user layer failed.\n"); result = -1; } } else { nn_log (LC_FATAL, "Initialization of user layer failed.\n"); result = -1; } nn_log (LC_INFO, "Finis.\n"); /* Must be really late, or nn_log becomes unhappy -- but it should be before os_osExit (which appears to be called from u_userExit(), which is not called by u_userDetach but by an exit handler, it appears.) */ config_fini (); return result; }
int ospl_main( int argc, char *argv[]) { u_result retVal; char *name; char *config; #ifdef INTEGRITY Error err; Semaphore networkSvcStartSem = SemaphoreObjectNumber(13); name = "networking"; config = "file:///ospl.xml"; err = WaitForSemaphore(networkSvcStartSem); assert ( err == Success ); argc = 3; #else #ifdef NW_DEBUGGING /* Stop and wait for debugger */ if (argc > 3) { if (strcmp(argv[3], "wait") == 0) { int p = 0; while (p==0) { os_time delay = {1, 0}; printf("Networking sleeping, waiting for debugger\n"); os_nanoSleep(delay); } } argc--; } if (argc > 3) { int i; printf("Ignoring superfluous parameters: "); for (i=3; i<argc; i++) { printf(" %s", argv[i]); } printf("\n"); } #endif #endif /* First check command line arguments */ if (argc == 3) { #ifndef INTEGRITY name = argv[1]; config = argv[2]; #endif /* Initialize user API */ retVal = u_userInitialise(); if (retVal == U_RESULT_OK) { /* The actual service actions */ nw_serviceMain(name, config); u_userDetach(); } else { NW_REPORT_ERROR("networking main loop", "Error attaching to kernel, bailing out"); } } else { NW_REPORT_ERROR("networking main loop", "Usage: networking name <configuration-URI>"); } return 0; }