Пример #1
0
//-------------------------------------------------------------------------------------
bool Componentbridge::process()
{
	if(state_ == 0)
	{
		// 如果是cellappmgr或者baseapmgrp则向machine请求获得dbmgr的地址
		Mercury::BundleBroadcast bhandler(networkInterface_, KBE_PORT_BROADCAST_DISCOVERY);

		bhandler.newMessage(MachineInterface::onBroadcastInterface);
		MachineInterface::onBroadcastInterfaceArgs8::staticAddToBundle(bhandler, getUserUID(), getUsername(), 
			componentType_, componentID_, 
			networkInterface_.intaddr().ip, networkInterface_.intaddr().port,
			networkInterface_.extaddr().ip, networkInterface_.extaddr().port);
		
		bhandler.broadcast();

		bhandler.close();
		state_ = 1;
		return true;
	}
	else
	{
		if(componentType_ != MACHINE_TYPE)
			if(!findInterfaces())
				return true;
	}

	return false;
}
Пример #2
0
//-------------------------------------------------------------------------------------
bool Machine::findBroadcastInterface()
{

	std::map<u_int32_t, std::string> interfaces;
	Mercury::BundleBroadcast bhandler(networkInterface_, KBE_PORT_BROADCAST_DISCOVERY);

	if (!bhandler.epListen().getInterfaces(interfaces))
	{
		ERROR_MSG("Machine::findBroadcastInterface: Failed to discover network interfaces\n");
		return false;
	}

	uint8 data = 1;
	bhandler << data;
	if (!bhandler.broadcast(KBE_PORT_BROADCAST_DISCOVERY))
	{
		ERROR_MSG(boost::format("Machine::findBroadcastInterface:Failed to send broadcast discovery message. error:%1%\n") %
			kbe_strerror());
		return false;
	}
	
	sockaddr_in	sin;

	if(bhandler.receive(NULL, &sin))
	{
		INFO_MSG(boost::format("Machine::findBroadcastInterface:Machine::findBroadcastInterface: Broadcast discovery receipt from %1%.\n") %
					inet_ntoa((struct in_addr&)sin.sin_addr.s_addr) );

		std::map< u_int32_t, std::string >::iterator iter;

		iter = interfaces.find( (u_int32_t &)sin.sin_addr.s_addr );
		if (iter != interfaces.end())
		{
			INFO_MSG(boost::format("Machine::findBroadcastInterface: Confirmed %1% (%2%) as default broadcast route interface.\n") %
				inet_ntoa((struct in_addr&)sin.sin_addr.s_addr) %
				iter->second.c_str());

			broadcastAddr_ = sin.sin_addr.s_addr;
			return true;
		}
	}
	
	std::string sinterface = "\t[";
	std::map< u_int32_t, std::string >::iterator iter = interfaces.begin();
	for(; iter != interfaces.end(); iter++)
	{
		sinterface += inet_ntoa((struct in_addr&)iter->first);
		sinterface += ", ";
	}

	sinterface += "]";

	ERROR_MSG(boost::format("Machine::findBroadcastInterface: Broadcast discovery [%1%] "
		"not a valid interface. available interfaces:%2%\n") %
		inet_ntoa((struct in_addr&)sin.sin_addr.s_addr) % sinterface.c_str());

	return false;
}
Пример #3
0
int 
imWindow::handle(int e) 
{
  int key, mod, ret;
  int x, y;
  if (Fl_Window::handle(e)) return 1;
  mod = Fl::event_state();	// modifiers (shift, alt, etc)
  key = Fl::event_key();
  int b = (mod & 0x0F000000);
  x = Fl::event_x();
  y = Fl::event_y();

  if (bhandler && (e == FL_PUSH || e == FL_DRAG || e == FL_RELEASE))
    {
      if (Fl::focus() != this)
        Fl::focus(this);
      ret = bhandler(e, x, y, b, (mod & 0x00FF0000), this);
      if (ret != 0)
        return ret;
    }

  // right button, set up interest rectangle
  if (b == FL_BUTTON3 && (e == FL_PUSH || e == FL_DRAG || e == FL_RELEASE))	
    {
      if (e == FL_PUSH)
        {
          moving = true;
          intX = x;
          intY = y;
          intW = intH = 0;
          redraw();
        }

      if (e == FL_RELEASE || e == FL_DRAG)
        {
          if (e == FL_RELEASE) moving = false;
          if (x != intX && y != intY) // ok, have a rectangle
            {
              if (x < intX) { int xx = x; x = intX; intX = xx; }
              if (y < intY) { int yy = y; y = intY; intY = yy; }
              intW = x - intX;
              intH = y - intY;
            }
          else
            intW = intH = 0;
          redraw();
        }

      return 1;
    }


  if (e == FL_KEYBOARD && key < 256 && khandler)
    {
      ret = khandler(key, (mod >> 16) & 0xF, this);
      return ret;
    }
Пример #4
0
//-------------------------------------------------------------------------------------
bool Componentbridge::findInterfaces()
{
	int8 findComponentTypes[] = {UNKNOWN_COMPONENT_TYPE, UNKNOWN_COMPONENT_TYPE, UNKNOWN_COMPONENT_TYPE, UNKNOWN_COMPONENT_TYPE, 
								UNKNOWN_COMPONENT_TYPE, UNKNOWN_COMPONENT_TYPE, UNKNOWN_COMPONENT_TYPE, UNKNOWN_COMPONENT_TYPE};

	switch(componentType_)
	{
	case CELLAPP_TYPE:
		findComponentTypes[0] = MESSAGELOG_TYPE;
		findComponentTypes[1] = RESOURCEMGR_TYPE;
		findComponentTypes[2] = DBMGR_TYPE;
		findComponentTypes[3] = CELLAPPMGR_TYPE;
		findComponentTypes[4] = BASEAPPMGR_TYPE;
		break;
	case BASEAPP_TYPE:
		findComponentTypes[0] = MESSAGELOG_TYPE;
		findComponentTypes[1] = RESOURCEMGR_TYPE;
		findComponentTypes[2] = DBMGR_TYPE;
		findComponentTypes[3] = BASEAPPMGR_TYPE;
		findComponentTypes[4] = CELLAPPMGR_TYPE;
		break;
	case BASEAPPMGR_TYPE:
		findComponentTypes[0] = MESSAGELOG_TYPE;
		findComponentTypes[1] = DBMGR_TYPE;
		findComponentTypes[2] = CELLAPPMGR_TYPE;
		break;
	case CELLAPPMGR_TYPE:
		findComponentTypes[0] = MESSAGELOG_TYPE;
		findComponentTypes[1] = DBMGR_TYPE;
		findComponentTypes[2] = BASEAPPMGR_TYPE;
		break;
	case LOGINAPP_TYPE:
		findComponentTypes[0] = MESSAGELOG_TYPE;
		findComponentTypes[1] = DBMGR_TYPE;
		findComponentTypes[2] = BASEAPPMGR_TYPE;
		break;
	case DBMGR_TYPE:
		findComponentTypes[0] = MESSAGELOG_TYPE;
		break;
	default:
		if(componentType_ != MESSAGELOG_TYPE && componentType_ != MACHINE_TYPE)
			findComponentTypes[0] = MESSAGELOG_TYPE;
		break;
	};

	int ifind = 0;
	srand(KBEngine::getSystemTime());
	uint16 nport = KBE_PORT_START + (rand() % 1000);

	while(findComponentTypes[ifind] != UNKNOWN_COMPONENT_TYPE)
	{
		if(dispatcher().isBreakProcessing())
			return false;

		int8 findComponentType = findComponentTypes[ifind];

		INFO_MSG("Componentbridge::process: finding %s...\n",
			COMPONENT_NAME_EX((COMPONENT_TYPE)findComponentType));
		
		Mercury::BundleBroadcast bhandler(networkInterface_, nport);
		if(!bhandler.good())
		{
			KBEngine::sleep(10);
			nport = KBE_PORT_START + (rand() % 1000);
			continue;
		}

		if(bhandler.pCurrPacket() != NULL)
		{
			bhandler.pCurrPacket()->resetPacket();
		}

		bhandler.newMessage(MachineInterface::onFindInterfaceAddr);
		MachineInterface::onFindInterfaceAddrArgs6::staticAddToBundle(bhandler, getUserUID(), getUsername(), 
			componentType_, findComponentType, networkInterface_.intaddr().ip, bhandler.epListen().addr().port);

		if(!bhandler.broadcast())
		{
			ERROR_MSG("Componentbridge::process: broadcast error!\n");
			return false;
		}
	
		MachineInterface::onBroadcastInterfaceArgs8 args;
		if(bhandler.receive(&args, 0, 1000000))
		{
			if(args.componentType == UNKNOWN_COMPONENT_TYPE)
			{
				//INFO_MSG("Componentbridge::process: not found %s, try again...\n",
				//	COMPONENT_NAME_EX(findComponentType));
				
				KBEngine::sleep(1000);
				
				// 如果是这些辅助组件没找到则跳过
				if(findComponentType == MESSAGELOG_TYPE || findComponentType == RESOURCEMGR_TYPE)
				{
					WARNING_MSG("Componentbridge::process: not found %s!\n",
						COMPONENT_NAME_EX((COMPONENT_TYPE)findComponentType));

					findComponentTypes[ifind] = -1; // 跳过标志

					ifind++;
				}

				continue;
			}

			INFO_MSG("Componentbridge::process: found %s, addr:%s:%u\n",
				COMPONENT_NAME_EX((COMPONENT_TYPE)args.componentType), inet_ntoa((struct in_addr&)args.intaddr), ntohs(args.intaddr));

			Componentbridge::getComponents().addComponent(args.uid, args.username.c_str(), 
				(KBEngine::COMPONENT_TYPE)args.componentType, args.componentID, args.intaddr, args.intport, args.extaddr, args.extport);
			
			// 防止接收到的数据不是想要的数据
			if(findComponentType == args.componentType)
			{
				ifind++;
			}
			else
			{
				ERROR_MSG("Componentbridge::process: %s not found. receive data is error!\n", COMPONENT_NAME_EX((COMPONENT_TYPE)findComponentType));
			}
		}
		else
		{
			ERROR_MSG("Componentbridge::process: receive error!\n");
			return false;
		}
	}
	
	ifind = 0;

	// 开始注册到所有的组件
	while(findComponentTypes[ifind] != UNKNOWN_COMPONENT_TYPE)
	{
		if(dispatcher().isBreakProcessing())
			return false;

		int8 findComponentType = findComponentTypes[ifind++];
		
		if(findComponentType == -1)
			continue;

		INFO_MSG("Componentbridge::process: register self to %s...\n",
			COMPONENT_NAME_EX((COMPONENT_TYPE)findComponentType));

		if(getComponents().connectComponent(static_cast<COMPONENT_TYPE>(findComponentType), getUserUID(), 0) != 0)
		{
			ERROR_MSG("Componentbridge::register self to %s is error!\n",
			COMPONENT_NAME_EX((COMPONENT_TYPE)findComponentType));

			dispatcher().breakProcessing();
			return false;
		}
	}

	return true;
}
Пример #5
0
//-------------------------------------------------------------------------------------
bool Componentbridge::findInterfaces()
{
	if(state_ == 1)
	{
		srand(KBEngine::getSystemTime());
		uint16 nport = KBE_PORT_START + (rand() % 1000);

		while(findComponentTypes_[findIdx_] != UNKNOWN_COMPONENT_TYPE)
		{
			if(dispatcher().isBreakProcessing())
				return false;

			int8 findComponentType = findComponentTypes_[findIdx_];

			INFO_MSG("Componentbridge::process: finding %s...\n",
				COMPONENT_NAME_EX((COMPONENT_TYPE)findComponentType));
			
			Mercury::BundleBroadcast bhandler(networkInterface_, nport);
			if(!bhandler.good())
			{
				return false;
			}

			bhandler.itry(0);
			if(bhandler.pCurrPacket() != NULL)
			{
				bhandler.pCurrPacket()->resetPacket();
			}

			bhandler.newMessage(MachineInterface::onFindInterfaceAddr);
			MachineInterface::onFindInterfaceAddrArgs6::staticAddToBundle(bhandler, getUserUID(), getUsername(), 
				componentType_, findComponentType, networkInterface_.intaddr().ip, bhandler.epListen().addr().port);

			if(!bhandler.broadcast())
			{
				ERROR_MSG("Componentbridge::process: broadcast error!\n");
				return false;
			}
		
			MachineInterface::onBroadcastInterfaceArgs8 args;
			if(bhandler.receive(&args, 0, 10000000))
			{
				if(args.componentType == UNKNOWN_COMPONENT_TYPE)
				{
					//INFO_MSG("Componentbridge::process: not found %s, try again...\n",
					//	COMPONENT_NAME_EX(findComponentType));
					
					// 如果是这些辅助组件没找到则跳过
					if(findComponentType == MESSAGELOG_TYPE || findComponentType == RESOURCEMGR_TYPE)
					{
						WARNING_MSG("Componentbridge::process: not found %s!\n",
							COMPONENT_NAME_EX((COMPONENT_TYPE)findComponentType));

						findComponentTypes_[findIdx_] = -1; // 跳过标志

						findIdx_++;
					}

					return false;
				}

				INFO_MSG("Componentbridge::process: found %s, addr:%s:%u\n",
					COMPONENT_NAME_EX((COMPONENT_TYPE)args.componentType), 
					inet_ntoa((struct in_addr&)args.intaddr), ntohs(args.intaddr));

				Componentbridge::getComponents().addComponent(args.uid, args.username.c_str(), 
					(KBEngine::COMPONENT_TYPE)args.componentType, args.componentID, 
					args.intaddr, args.intport, args.extaddr, args.extport);
				
				// 防止接收到的数据不是想要的数据
				if(findComponentType == args.componentType)
				{
					findIdx_++;
				}
				else
				{
					ERROR_MSG("Componentbridge::process: %s not found. receive data is error!\n", 
						COMPONENT_NAME_EX((COMPONENT_TYPE)findComponentType));
				}
			}
			else
			{
				ERROR_MSG("Componentbridge::process: receive error!\n");

				// 如果是这些辅助组件没找到则跳过
				if(findComponentType == MESSAGELOG_TYPE || findComponentType == RESOURCEMGR_TYPE)
				{
					WARNING_MSG("Componentbridge::process: not found %s!\n",
						COMPONENT_NAME_EX((COMPONENT_TYPE)findComponentType));

					findComponentTypes_[findIdx_] = -1; // 跳过标志

					findIdx_++;
				}

				return false;
			}
		}

		state_ = 2;
		findIdx_ = 0;
		return false;
	}

	if(state_ == 2)
	{
		// 开始注册到所有的组件
		while(findComponentTypes_[findIdx_] != UNKNOWN_COMPONENT_TYPE)
		{
			if(dispatcher().isBreakProcessing())
				return false;

			int8 findComponentType = findComponentTypes_[findIdx_];
			
			if(findComponentType == -1)
			{
				findIdx_++;
				return false;
			}

			INFO_MSG("Componentbridge::process: register self to %s...\n",
				COMPONENT_NAME_EX((COMPONENT_TYPE)findComponentType));

			if(getComponents().connectComponent(static_cast<COMPONENT_TYPE>(findComponentType), getUserUID(), 0) != 0)
			{
				ERROR_MSG("Componentbridge::register self to %s is error!\n",
				COMPONENT_NAME_EX((COMPONENT_TYPE)findComponentType));
				//dispatcher().breakProcessing();
				return false;
			}

			findIdx_++;
			return false;
		}
	}

	return true;
}