示例#1
0
//===========================================================================
bool cPrecisionClock::timeoutOccurred()
{
    // check if timeout has occurred
    if (getCurrentTimeSeconds() > m_timeoutPeriod)
    {
        return true;
    }
    else
    {
        return false;
    }
}
示例#2
0
//===========================================================================
double cPrecisionClock::stop()
{

    // How much time has now elapsed in total running "sessions"?
    m_timeAccumulated += getCPUTimeSeconds() - m_timeStart;

    // stop timer
    m_on = false;

    // return time when timer was stopped
    return getCurrentTimeSeconds();
}
示例#3
0
void User::processUpdatePacket(CS6Packet newData)
{
	m_lastReceivedPacketTime = getCurrentTimeSeconds();
	switch(newData.packetType)
	{
	case 0:
		{
			//BAD
			break;
		}
	case TYPE_Update:
		{
			//if this packet is applicable for processing
			if (m_lastReceivedPacketNum < newData.packetNumber)
			{

				Vector2f nextTarget = Vector2f(newData.data.updated.xPosition, newData.data.updated.yPosition);
				//prepare the unit for autonomous movement
				m_unit.m_deadReckoningVelocity = nextTarget - m_unit.m_target;
				m_unit.m_deadReckoningVelocity = m_unit.m_deadReckoningVelocity.normalized();
				//if (!(m_unit.m_deadReckoningVelocity == Vector2f(0,0)))
				//{
					m_unit.m_hasReachedCurrentTarget = false;
				//}
				m_unit.m_target = nextTarget;
				m_unit.m_velocity.x = newData.data.updated.xVelocity;
				m_unit.m_velocity.y = newData.data.updated.yVelocity;
				m_unit.m_orientationDegrees = newData.data.updated.yawDegrees;
				Color3b tempColor;
				memcpy(&tempColor, newData.playerColorAndID, sizeof(tempColor));
				m_unit.m_color = Color4f(tempColor);
				//jump the player if the distance is too far
				if (m_unit.m_position == Vector2f(0,0) || m_unit.m_position.distanceSquared(m_unit.m_target) > 400)
				{
					m_unit.m_position.x = newData.data.updated.xPosition;
					m_unit.m_position.y = newData.data.updated.yPosition;

				}
				//and decide which packet should be processed next
				m_lastReceivedPacketNum = newData.packetNumber;
			}
			break;
		}
	case TYPE_Victory:
		{
			m_score++;
			break;
		}
	}
}
//轮询列表(查看是否有节点超时未收到数据),线程函数
void *pollDiedNode()
{
	while(1)
	{
		sleep(PollDiedTime);

		uint64 time = getCurrentTimeSeconds();
		//检查第一级液位节点
		LevelFirstNode *lf = LevelFirstList;
		while(lf != NULL)
		{
			if(my_mutex_lock(&(lf->lock)))
			{
				lf = lf->next;
				continue;
			}
			if((lf->active == 1) && ((time - lf->time) >= ActiveDiff))  //节点已经died
			{
				lf->active = 0;
				/**向客户端发送信息通知该节点已经died**/
				sendNodeOnOffBroadInfo(&time,REAL_LEVEL_NODE_OFF,lf->drum);
			}
			pthread_mutex_unlock(&(lf->lock));
			lf = lf->next;
		}
		//检查第一级温度节点
		TempFirstNode *tf = TempFirstList;
		while(tf != NULL)
		{
			if(my_mutex_lock(&(tf->lock)))
			{
				tf = tf->next;
				continue;
			}
			if((tf->active == 1) && ((time - tf->time) >= ActiveDiff))  //节点已经died
			{
				tf->active = 0;
				/**向客户端发送信息通知该节点已经died**/
				sendNodeOnOffBroadInfo(&time,REAL_TEMP_NODE_OFF,tf->drum);
			}
			pthread_mutex_unlock(&(tf->lock));
			tf = tf->next;
		}
	}
}
//收到新数据信息,数据取地址,type只有两种first类型
int addNewLTInfo(enum LTLabel type,void *info,uint8 drum)
{
	if((info == NULL) || (drum == (uint8)0) || (drum >= NumofDrums))
		return -1;
	
	switch(type)
	{
		case L_FIRST: //收到液位信息
		{	LevelFirstNode *lf = LevelFirstList;
			int step = 0;  //用于直接到达二级、三级节点结构
			while(lf != NULL)
			{
				if(lf->drum == drum)  //找到添加的节点,这些节点都不会被删除,并且drum不会改变,所以,锁lock可以不用包含这句话
				{	
					if(my_mutex_lock(&lf->lock)) //这里就需要加锁,未获取到锁
						return -1;
					uint64 curtime = getCurrentTimeSeconds();//获取当前时间
					uint16 level = *(uint16 *)info;  //获取液位信息
					LTInfo *ltinfo = NULL;
					int alarm = 0;
					uint16 first_leveldiff = 0;
					if(lf->time != (uint64)0)  //如果不为系统第一次接收数据
					{
						//判断是否需要报警
						if((former_first_level[drum - 1] > level) && ((first_leveldiff=(former_first_level[drum - 1] - level)) >= LevelFirstDiff))
						{
							//debug("first level alarm\n");
							//报警
							sendAlarmBroadInfo(&curtime,REAL_ALARM_FISRT,drum,first_leveldiff);
							alarm = 1;
						}
						
						//不是系统第一次接受数据的话需要将原来的值存入数据库中
						ltinfo = (LTInfo *)malloc(sizeof(LTInfo));
						memset(ltinfo,0,sizeof(LTInfo));
						ltinfo->time = lf->time;
						ltinfo->drum = lf->drum;
						ltinfo->lt.level = lf->lt.level;
						if(addLTInfo(ltinfo,L_FIRST))
						{
							debug("add ltinfo first level error\n");
							free(ltinfo);
							pthread_mutex_unlock(&lf->lock);
							return -1;
						}
					}
					former_first_level[drum - 1] = level;
					//开始修改第一级节点信息
					lf->time = curtime;
					lf->lt.level = level;
					if(lf->active == 0)
					{
						lf->active = 1;
						/******向客户端发送信息通知节点成功接入协议,轮询实时发送列表,然后发送*******/
						//debug("******node add in success*********\n");
						sendNodeOnOffBroadInfo(&curtime,REAL_LEVEL_NODE_ON,drum);
					}
					
					pthread_mutex_unlock(&(lf->lock));
					lf = NULL;  //以防出错
					
					/**接着找到第二级节点并修改信息**/
					LevelSecondNode *ls = LevelSecondList;
					for(int i = 0; i < step; i++)  //根据初始化时的结构可知,各级的桶号是一一对应的
						ls = ls->next;
					if((ls == NULL) || (ls->drum != drum))
					{
						debug("can not find second level by first\n");
						free(ltinfo);
						return -1;
					}
					//找到第二级节点,比较时间,添加节点信息
					if(my_mutex_lock(&(ls->lock)))
					{
						free(ltinfo);
						return -1;
					}
					if((curtime / FSTTOSEC) == (ls->time / FSTTOSEC))  //在同一个时间段内,time和ls->time除以每小时的秒数值肯定相同
					{
						//时间time不能修改
						ls->lt.sum_level += level;
						ls->num++;
						pthread_mutex_unlock(&(ls->lock));
					}
					else				//不在同一个时间段内
					{
						if(ls->num != 0)   //非第一次存入值,第一次的时候num也为0
						{
							uint16 second_leveldiff = 0;
							//判断是否需要报警
							if((!alarm) && (former_second_level[drum-1] > level) && ((second_leveldiff=(former_second_level[drum-1] - level)) >= LevelSecondDiff))
							{
								debug("second level alarm\n");
								//报警
								sendAlarmBroadInfo(&curtime,REAL_ALARM_SECOND,drum,second_leveldiff);
								alarm = 1;
							}
							
							//先将原时间段存入数据库中
							ltinfo->time = ls->time;					//drum前面已经赋值过了
							ltinfo->lt.level = (uint16)((ls->lt.sum_level) / (ls->num));    //存入数据库的是平均值
							ltinfo->num = ls->num;
							if(addLTInfo(ltinfo,L_SECOND))
							{
								pthread_mutex_unlock(&(ls->lock));
								debug("add ltinfo error");
								free(ltinfo);
								return -1;
							}
						}
						former_second_level[drum-1] = level;
						//然后给该二级节点赋新值
						uint32 sum_level = ls->lt.sum_level;
						uint16 num = ls->num;
						ls->time = curtime;
						ls->lt.sum_level = level;
						ls->num = 1;
						pthread_mutex_unlock(&(ls->lock));
						ls = NULL;
						
						/** 当二级节点被存入数据库后,同时将该节点加入第三级节点中 **/
						LevelThirdNode *lt = LevelThirdList;
						for(int i = 0; i < step; i++)  //找到三级节点
							lt = lt->next;
						if((lt == NULL) || (lt->drum != drum))
						{
							debug("can not find third level by second\n");
							free(ltinfo);
							return -1;
						}
						if(my_mutex_lock(&(lt->lock)))
						{
							free(ltinfo);
							return -1;
						}
						if((curtime / SECTOTHD) == (lt->time / SECTOTHD)) //与第三级节点在同一个设定的时间段内
						{
							lt->lt.sum_level += sum_level;
							lt->num += num;
							pthread_mutex_unlock(&(lt->lock));
						}
						else    //不在一个时间段内
						{
							if(lt->num != 0)
							{
								uint16 third_leveldiff = 0;
								//判断是否需要报警
								if((!alarm) && (former_third_level[drum-1] > level) && ((third_leveldiff=(former_third_level[drum-1] - level)) >= LevelThirdDiff))
								{//报警
									debug("third level alarm\n");
									sendAlarmBroadInfo(&curtime,REAL_ALARM_THIRD,drum,third_leveldiff);
								}
								ltinfo->time = lt->time;					//drum前面已经赋值过了
								ltinfo->lt.level = (uint16)((lt->lt.sum_level) / (lt->num));    //存入数据库的是平均值
								ltinfo->num = lt->num;
								if(addLTInfo(ltinfo,L_THIRD))
								{
									pthread_mutex_unlock(&(lt->lock));
									debug("add ltinfo error");
									free(ltinfo);
									return -1;
								}
							}
							former_third_level[drum-1] = level;
							//给三级节点赋新值
							lt->time = curtime;
							lt->lt.sum_level = sum_level;
							lt->num = num;
							pthread_mutex_unlock(&(lt->lock));
						}
					}
					if(ltinfo)
						free(ltinfo);
					return 0;
				}
				lf = lf->next;
				step++;
			}
		}
			break;
		case T_FIRST: //收到温度信息
		{//	debug("in temp first add\n");
			TempFirstNode *tf = TempFirstList;
			int step = 0;  //用于直接到达二级、三级节点结构
			while(tf != NULL)
			{
				if(tf->drum == drum)  //找到添加的节点,这些节点都不会被删除,并且drum不会改变,所以,锁lock可以不用包含这句话
				{
					if(my_mutex_lock(&(tf->lock)))
						return -1;
					uint64 curtime = getCurrentTimeSeconds(); //获取当前时间
					float temp = *(float *)info;  //获取温度信息
					LTInfo *ltinfo = NULL;
					if(tf->time != (uint64)0)  //如果不为系统第一次接收数据
					{
						//温度不需要报警,将原来的值存入数据库中
						ltinfo = (LTInfo *)malloc(sizeof(LTInfo));
						memset(ltinfo,0,sizeof(LTInfo));
						ltinfo->time = tf->time;
						ltinfo->drum = tf->drum;
						ltinfo->lt.temp = tf->lt.temp;
						if(addLTInfo(ltinfo,T_FIRST))
						{
							debug("add ltinfo error");
							free(ltinfo);
							return -1;
						}
					}
					//开始修改第一级节点信息
					tf->time = curtime;
					tf->lt.temp = temp;
					if(tf->active == 0)
					{
						tf->active = 1;
						/******向客户端发送信息通知节点成功接入协议,轮询实时发送列表,然后发送*******/
						sendNodeOnOffBroadInfo(&curtime,REAL_TEMP_NODE_ON,drum);
					}
					pthread_mutex_unlock(&(tf->lock));
					tf = NULL;  //以防出错
					
					/**接着找到第二级节点并修改信息**/
					TempSecondNode *ts = TempSecondList;
					for(int i = 0; i < step; i++)  //根据初始化时的结构可知,各级的桶号是一一对应的
						ts = ts->next;
					if((ts == NULL) || (ts->drum != drum))
					{
						debug("can not find second level by first\n");
						free(ltinfo);
						return -1;
					}
					//找到第二级节点,比较时间,添加节点信息
					if(my_mutex_lock(&(ts->lock)))
					{
						free(ltinfo);
						return -1;
					}
					if((curtime / FSTTOSEC) ==(ts->time / FSTTOSEC))  //在同一个时间段内,time一定比ts->time大,第一次的话ts->time 为0,差值一定大于FSTTOSEC,所以这几句代码不执行
					{
						//时间time不能修改
						ts->lt.sum_temp += temp;
						ts->num++;
						pthread_mutex_unlock(&(ts->lock));
					}
					else				//不在同一个时间段内
					{
						if(ts->num != 0)
						{
							//先将原时间段存入数据库中
							ltinfo->time = ts->time;					//drum前面已经赋值过了
							ltinfo->lt.temp = (ts->lt.sum_temp) / (ts->num);    //存入数据库的是平均值
							ltinfo->num = ts->num;
							if(addLTInfo(ltinfo,T_SECOND))
							{
								pthread_mutex_unlock(&(ts->lock));
								debug("add ltinfo error");
								free(ltinfo);
								return -1;
							}
						}
						//然后给该二级节点赋新值
						float sum_temp = ts->lt.sum_temp;
						uint16 num = ts->num;
						ts->time = curtime;
						ts->lt.sum_temp = temp;
						ts->num = 1;
						pthread_mutex_unlock(&(ts->lock));
						ts = NULL;
						
						/** 当二级节点被存入数据库后,同时将该节点加入第三级节点中 **/
						TempThirdNode *tt = TempThirdList;
						for(int i = 0; i < step; i++)  //找到三级节点
							tt = tt->next;
						if((tt == NULL) || (tt->drum != drum))
						{
							debug("can not find third level by second\n");
							free(ltinfo);
							return -1;
						}
						if(my_mutex_lock(&(tt->lock)))
						{
							free(ltinfo);
							return -1;
						}
						if((curtime / SECTOTHD) ==(tt->time / SECTOTHD)) //与第三级节点在同一个设定的时间段内
						{
							tt->lt.sum_temp += sum_temp;
							tt->num += num;
							pthread_mutex_unlock(&(tt->lock));
						}
						else    //不在一个时间段内
						{
							if(tt->num != 0)
							{
								ltinfo->time = tt->time;					//drum前面已经赋值过了
								ltinfo->lt.temp = (tt->lt.sum_temp) / (tt->num);    //存入数据库的是平均值
								ltinfo->num = tt->num;
								if(addLTInfo(ltinfo,T_THIRD))
								{
									pthread_mutex_unlock(&(tt->lock));
									debug("add ltinfo error");
									free(ltinfo);
									return -1;
								}
							}
							//给三级节点赋新值
							tt->time = curtime;
							tt->lt.sum_temp = sum_temp;
							tt->num = num;
							pthread_mutex_unlock(&(tt->lock));
						}
					}
					if(ltinfo)  //不为空则释放
						free(ltinfo);
					return 0;
				}
				tf = tf->next;
				step++;
			}
		}
			break;
		default:
			return -1;
	}
	
	return -1;
}