static void createSubscriber (void) { mama_status status; memset(&gCb, 0, sizeof(gCb)); gCb.onCreate = createCb; gCb.onError = errorCb; gCb.onMsg = msgCb; gCb.onQuality = NULL; /* not used by basic subscriptions */ gCb.onGap = NULL; /* not used by basic subscriptions */ gCb.onRecapRequest = NULL; /* not used by basic subscriptions */ if (MAMA_STATUS_OK!=(status=mamaSubscription_allocate (&gSubscription))) { printf ("Error allocating subscription: %s\n", mamaStatus_stringForStatus (status)); exit (status); } mamaSubscription_setDebugLevel (gSubscription, gSubscLogLevel); if (MAMA_STATUS_OK!=(status=mamaSubscription_createBasic (gSubscription, gTransport, gMamaDefaultQueue, &gCb, gTopic, NULL))) { printf ("Error creating subscription: %s\n", mamaStatus_stringForStatus (status)); exit (status); } }
/* * Subscribe to specified message topic */ void subscribeToTopic(const char * topicName) { global.subscription = NULL; mama_status status; if ((status = mamaSubscription_allocate(&global.subscription)) == MAMA_STATUS_OK) { // initialize functions called by MAMA on different subscription events memset(&global.receiver, 0, sizeof(global.receiver)); global.receiver.onCreate = onCreate; // when subscription created global.receiver.onError = onError; // when error occurs global.receiver.onMsg = onMessage; // when a message arrives on subscribed topic // subscribe to messages on specified topic mamaQueue defaultQueue; if (((status = mama_getDefaultEventQueue(global.bridge, &defaultQueue)) == MAMA_STATUS_OK) && ((status = mamaSubscription_createBasic(global.subscription, global.transport, defaultQueue, &global.receiver, topicName, NULL)) == MAMA_STATUS_OK)) { // normal exit return; } } // error exit printf("Error subscribing to topic %s, error code: %s\n", topicName, mamaStatus_stringForStatus(status)); mama_close(); exit(status); }
mama_status avisBridgeMamaInbox_createByIndex (inboxBridge* bridge, mamaTransport transport, int tportIndex, mamaQueue queue, mamaInboxMsgCallback msgCB, mamaInboxErrorCallback errorCB, mamaInboxDestroyCallback onInboxDestroyed, void* closure, mamaInbox parent) { wUuid t_uuid; char t_str[37]; mamaMsgCallbacks cb; avisInboxImpl* impl = NULL; mama_status status = MAMA_STATUS_OK; if (!bridge || !transport || !queue || !msgCB) return MAMA_STATUS_NULL_ARG; impl = (avisInboxImpl*)calloc(1, sizeof(avisInboxImpl)); if (!impl) return MAMA_STATUS_NOMEM; if (MAMA_STATUS_OK != (status = mamaSubscription_allocate(&impl->mSubscription))) { mama_log (MAMA_LOG_LEVEL_ERROR, "avisBridgeMamaInbox_createByIndex(): Failed to allocate subscription "); free(impl); return status; } // NOTE: uuid_generate is very expensive, so we use cheaper uuid_generate_time wUuid_generate_time(t_uuid); wUuid_unparse(t_uuid, t_str); snprintf(impl->mInbox, sizeof(impl->mInbox) -1, "_INBOX.%s", t_str); cb.onCreate = &avisInbox_onCreate; cb.onError = &avisInbox_onError; cb.onMsg = &avisInbox_onMsg; cb.onDestroy = &avisInbox_onDestroy; // not used by basic subscriptions cb.onQuality = NULL; cb.onGap = NULL; cb.onRecapRequest = NULL; if (MAMA_STATUS_OK != (status = mamaSubscription_createBasic(impl->mSubscription, transport, queue, &cb, impl->mInbox, impl))) { mama_log (MAMA_LOG_LEVEL_ERROR, "avisBridgeMamaInbox_createByIndex(): Failed to create subscription "); mamaSubscription_deallocate(impl->mSubscription); free(impl); return status; } impl->mClosure = closure; impl->mMsgCB = msgCB; impl->mErrCB = errorCB; impl->mParent = parent; impl->mOnInboxDestroyed = onInboxDestroyed; *bridge = (inboxBridge) impl; return MAMA_STATUS_OK; }
/********************************************************** * mc_createSubscriber * **********************************************************/ static void mc_createSubscriber ( mamaMsgCallbacks * pCb, MamaLogLevel logLevel, mamaTransport transport, mamaQueue queue ) { int i=0; const char* prefix = NULL; pCb->onCreate = mc_createCb; pCb->onError = mc_errorCb; if (gTimeInterval) { pCb->onMsg = mc_msgCb2; } else { pCb->onMsg = mc_msgCb; } pCb->onQuality = NULL; /* not used by basic subscriptions */ pCb->onGap = NULL; /* not used by basic subscriptions */ pCb->onRecapRequest = NULL; /* not used by basic subscriptions */ for (i=0; i<gNumSymbols; i++) { if (NULL !=gSymbolNamespace) { prefix = strdup(gSymbolNamespace); strcat((char*)prefix,"."); strcat((char*)prefix,gSymbolList[i]); } else { prefix = gSymbolList[i]; } MAMA_CHECK(mamaSubscription_allocate (&gSubscriptionList[i])); mamaSubscription_setDebugLevel (gSubscriptionList[i], logLevel); MAMA_CHECK(mamaSubscription_createBasic (gSubscriptionList[i], transport, queue, pCb, prefix, NULL)); if (gVcount) { printf("Created subscription for %s\n", prefix); } free((void*)prefix); } }
mama_status mamaDQPublisherManager_create ( mamaDQPublisherManager manager, mamaTransport transport, mamaQueue queue, const mamaDQPublisherManagerCallbacks* callback, const char* sourcename, const char* root, void * closure) { char* topic; int length = 0; mamaDQPublisherManagerImpl* impl = (mamaDQPublisherManagerImpl*) manager; static mamaMsgCallbacks basicSubscCbs = { dqPublisherImplCreateCb, dqPublisherImplErrorCb, dqPublisherImplMsgCb, NULL }; impl->mUserCallbacks = *callback; impl->mTransport = transport; impl->mQueue = queue; impl->mClosure = closure; length = strlen(root) + 1 + (strlen(sourcename) + 1); topic = calloc(length, sizeof(char)); strcpy(topic, root); impl->mNameSpace = strdup(sourcename); strcat(topic, "."); strcat(topic, sourcename); impl->mPublisherMap = wtable_create (topic, NUM_BUCKETS); mamaSubscription_allocate (&impl->mSubscription); mamaPublisher_create (&impl->mPublisher, transport, MAMA_CM_TOPIC, NULL, NULL); mamaSubscription_createBasic (impl->mSubscription, transport, queue, &basicSubscCbs, topic, impl); free(topic); return MAMA_STATUS_OK; }
static void prepareDictionaryListener (void) { mama_status status = MAMA_STATUS_OK; mamaMsgCallbacks dictionarySubCallbacks; dictionarySubCallbacks.onCreate = dictionarySubOnCreate; dictionarySubCallbacks.onDestroy = dictionarySubOnDestroy; dictionarySubCallbacks.onError = dictionarySubOnError; dictionarySubCallbacks.onMsg = dictionarySubOnMsg; /* Basic subscription so these are set to NULL */ dictionarySubCallbacks.onQuality = NULL; dictionarySubCallbacks.onGap = NULL; dictionarySubCallbacks.onRecapRequest = NULL; status = mamaSubscription_allocate (&gDictionarySubscription); if (MAMA_STATUS_OK != status) { mama_log (MAMA_LOG_LEVEL_ERROR, "Could not allocate dictionary subscription."); exit (1); } status = mamaSubscription_createBasic (gDictionarySubscription, gPubTransport, gPubDefaultQueue, &dictionarySubCallbacks, "_MDDD.WOMBAT.DATA_DICT", NULL); if (status != MAMA_STATUS_OK) { mama_log (MAMA_LOG_LEVEL_ERROR, "Could not create dictionary subscription"); exit (1); } /* Create the Dictionary Object */ mamaDictionary_create (&gDictionary); mamaDictionary_populateFromFile (gDictionary, gDictionaryFile); /* Create Dictionary Response Message */ status = mamaDictionary_getDictionaryMessage (gDictionary, &gDictionaryMessage); if (MAMA_STATUS_OK != status) { mama_log (MAMA_LOG_LEVEL_ERROR, "Failed to allocate dictionary message."); exit (1); } /* Build dictionary publisher */ status = mamaPublisher_create (&gDictionaryPublisher, gPubTransport, "WOMBAT.DATA_DICT", NULL, NULL); if (MAMA_STATUS_OK != status) { mama_log (MAMA_LOG_LEVEL_ERROR, "Failed to create dictionary publisher."); exit (1); } status = mamaMsg_updateU8 (gDictionaryMessage, MamaFieldMsgType.mName, MamaFieldMsgType.mFid, MAMA_MSG_TYPE_INITIAL); if (MAMA_STATUS_OK != status) { mama_log (MAMA_LOG_LEVEL_ERROR, "Failed to update dictionary message."); exit (1); } }
/* Description : This function will populate the responder impl object by creating the arrays of publishers * and subscribers. Note that one of each of these objects must be created for each of the sub * transport bridges that are contained in the main transport. There will be multiple sub * transports used when load balancing. * Arguments : impl [I] The responder to populate. * Returns : MAMA_STATUS_NO_BRIDGE_IMPL * MAMA_STATUS_OK */ static mama_status populateCmResponder(mamaCmResponderImpl *impl) { /* Returns */ mama_status ret = MAMA_STATUS_NO_BRIDGE_IMPL; mamaQueue internalQueue = NULL; /* Get the default event queue from the bridgeImpl. */ mamaBridgeImpl *bridgeImpl = mamaTransportImpl_getBridgeImpl(impl->mTransport); if(bridgeImpl != NULL) { /* The same callbacks will be sent to each subscriber regardless of the bridge * transport being used. */ mamaMsgCallbacks callbacks; memset(&callbacks, 0, sizeof(callbacks)); callbacks.onCreate = createCB; callbacks.onError = errorCB; callbacks.onMsg = msgCB; callbacks.onDestroy = destroyCB; ret = mamaBridgeImpl_getInternalEventQueue((mamaBridge)bridgeImpl, &internalQueue); if (ret == MAMA_STATUS_OK) { /* Enumerate all the sub transport bridges in the transport. */ int nextTransportIndex = 0; mamaPublisher *nextPublisher = impl->publishers; mamaSubscription *nextSubscription = impl->subscriptions; while((ret == MAMA_STATUS_OK) && (nextTransportIndex < impl->numberTransportBridges)) { /* The publisher allows commands to respond to point to point requests * without creating a new publisher, this must be created using the correct * transport bridge. */ ret = mamaPublisher_createByIndex( nextPublisher, impl->mTransport, nextTransportIndex, MAMA_CM_PUB_TOPIC, NULL, NULL); if(ret == MAMA_STATUS_OK) { /* Allocate the subscription */ ret = mamaSubscription_allocate(nextSubscription); if(ret == MAMA_STATUS_OK) { /* Set the subscription's transport index to ensure that the right transport is used. */ mamaSubscription_setTransportIndex(*nextSubscription, nextTransportIndex); /* Create the subscription */ ret = mamaSubscription_createBasic( *nextSubscription, impl->mTransport, internalQueue, &callbacks, MAMA_CM_TOPIC, impl); if(ret == MAMA_STATUS_OK) { /* We don't want the CM subs to show up on the stats logger */ mamaSubscription_setLogStats(*nextSubscription, 0); } } } /* Increment the counts for the next iteration */ nextTransportIndex ++; nextPublisher ++; nextSubscription ++; } } } return ret; }