赞
踩
我们知道该模块下经常出现的任务概念,这里我们也可以理解为消息。这里有消息的处理函数。
任务或者消息通过iunknown接口发送,这个函数就是默认的处理该接口发送过来的消息。先根据参数identity来判断身份是否合法。如果合法调用相应的函数来处理消息。其中还需要判断是service还是feature,根据不同情况调用不同处理函数。
参数参数:
void DEFAULT_MessageHandle(ServiceImpl *serviceImpl, const Identity *identity, Request *msg) { //参数检查 if (serviceImpl->serviceId != identity->serviceId) { //身份标识identity中指示的serviceId与当前服务不匹配 return; } if (identity->featureId < 0) { //未指定feature,则调用service的消息处理函数 if (serviceImpl->service->MessageHandle != NULL) { //用于处理调用者发送的请求 serviceImpl->service->MessageHandle(serviceImpl->service, msg); } return; } //有指定featureid,则调用对应的feature处理消息 //判断featureid是否合法 if (VECTOR_Size(&serviceImpl->features) <= identity->featureId) { return; } //根据featureid返回对应的featureImpl对象,featureid对应于features中的feature的下标 FeatureImpl *featureImpl = (FeatureImpl *)VECTOR_At(&(serviceImpl->features), identity->featureId); if (featureImpl == NULL) { return; } //通过对应的feature处理消息,用于处理调用者发送的请求 featureImpl->feature->OnMessage(featureImpl->feature, msg); }
服务的停止
该函数就是为了停止一个服务,最后将服务状态置为INIT。
void DEFAULT_StopService(ServiceImpl *service) { if (service == NULL) { return; } //配置身份标识 Identity id = {service->serviceId, INVALID_INDEX, (service->taskPool != NULL) ? service->taskPool->queueId : NULL}; //获取features中feature的个数 int16 size = VECTOR_Size(&service->features); int16 i; //遍历features集合 for (i = 0; i < size; ++i) { //根据下标返回FeatureImpl对象 FeatureImpl *featureImpl = (FeatureImpl *)VECTOR_At(&(service->features), i); if (featureImpl == NULL) { continue; } //标识指定的feature id.featureId = i; //停止feature featureImpl->feature->OnStop(featureImpl->feature, id); } //更新状态为SVC_INIT service->inited = SVC_INIT; }
获取feature实例
这里的原理就是根据feature的name来匹配对应实例的下标pos,最后返回feature实例。
/*
函数功能:获取featureimpl对象
函数描述:查询并返回serviceimpl的features中指定name的featureimpl对象
*/
FeatureImpl *DEFAULT_GetFeature(ServiceImpl *serviceImpl, const char *featureName)
{
//参数检查
if (serviceImpl == NULL || featureName == NULL) {
return NULL;
}
//根据featureName查询,返回相应元素下标
short pos = VECTOR_FindByKey(&(serviceImpl->features), (void *)featureName);
//返回指定下标的FeatureImpl对象
return (FeatureImpl *)VECTOR_At(&(serviceImpl->features), pos);
}
feature的删除
//删除serviceImpl中名称为featureName的feature,删除的同时停止feature Feature *DEFAULT_DeleteFeature(ServiceImpl *serviceImpl, const char *featureName) { //参数检查 if (serviceImpl == NULL || featureName == NULL) { return NULL; } //获取指定featureName在features中的下标 int16 pos = VECTOR_FindByKey(&(serviceImpl->features), (void *)featureName); if (pos < 0 || !SAMGR_IsNoInterface((FeatureImpl *)VECTOR_At(&serviceImpl->features, pos))) { //下标不合法,或者featureImpl不为NULL且featureImpl的接口不为NULL return NULL; } //将features中指定下标的元素与NULL值交换,起到删除的作用。 //成功 返回被替换的元素,失败 返回NULL FeatureImpl *featureImpl = (FeatureImpl *)VECTOR_Swap(&(serviceImpl->features), pos, NULL); if (featureImpl == NULL) { return NULL; } //标识服务ID Identity id = {serviceImpl->serviceId, INVALID_INDEX, NULL}; //停止指定的feature featureImpl->feature->OnStop(featureImpl->feature, id); //返回feature,并释放featureImpl占用的空间 Feature *feature = featureImpl->feature; SAMGR_Free(featureImpl); return feature; }
获取featurename
这个和之前获取实例的相反,这个是根据feature实例获取featurename,之前是根据featurename来获取实例。刚好实现功能相反
static const char *GetFeatureName(const FeatureImpl *featureImpl)
{
if (featureImpl == NULL) {
return NULL;
}
//获取feature的name并返回
return featureImpl->feature->GetName(featureImpl->feature);
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。