Exemplo n.º 1
0
void ClusterMembership::decodePacket(const char *buf, int len, const IpAddress & src)
{
	// Check length acceptable
	if (len < 20) return;
	// Check magic number
	unsigned int magic = ntohl(* (unsigned int *) buf);
	if (magic != MagicNumber) return;
	CommandType cmd = (CommandType) ntohl(* (unsigned int *) (buf + 12));
	// See if it's a "set weight" packet
	if (cmd == CommandSetWeight) {
		handleSetWeight(buf, len, src);
		return;
	}
	// Otherwise, it's a normal multicast pkt.
	// Check cluster IP
	IpAddress decode_clusteraddr;
	decode_clusteraddr.copyFromInAddr((struct in_addr *) (buf+4));
	if (decode_clusteraddr != clusteraddr) return;
	// Get source addr in packet, ensure it's the same as
	// the sender.
	IpAddress decode_src;
	decode_src.copyFromInAddr((struct in_addr *) (buf+8));
	if (decode_src != src) return;
	unsigned int weight = ntohl(*(int *) (buf + 16));
	// Ignore messages from my own node.
	if (src == localaddr) return;
	switch (cmd) {
		case CommandStatus:
		case CommandMaster:
			// ok;
			break;
		default:
			// unknown cmd
			return;
	}
	// std::cout << "From " << src << " Command:" << cmd << " node weight:" << weight << std::endl;
	// Check if it's already in nodemap.
	if (nodes.find(src) == nodes.end())
	{
		nodes[src] = NodeInfo();
	}
	// Update weight and timestamp.
	nodes[src].weight = weight;
	if (! nodes[src].isup) {
		nodes[src].isup = true;
		std::cout << "Node up:" << src << std::endl;
	}
	// Timestamp 
	gettimeofday(& (nodes[src].lastheard), 0);
	if (cmd == CommandMaster) {
		decodeMasterPacket(buf,len,src);
	}
}
Exemplo n.º 2
0
// Handle messages for this resource.
UtlBoolean MprMixer::handleMessage(MpFlowGraphMsg& rMsg)
{
   UtlBoolean boolRes;
   int       msgType;
   int*      weights;

   msgType = rMsg.getMsg();
   switch (msgType)
   {
   case SET_WEIGHT:
      return handleSetWeight(rMsg.getInt1(), rMsg.getInt2());
      break;
   case SET_WEIGHTS:
      weights = (int*) rMsg.getPtr1();
      boolRes = handleSetWeights(weights, rMsg.getInt1());
      delete[] weights; // delete storage allocated in the setWeights() method
      return boolRes;
      break;
   default:
      return MpResource::handleMessage(rMsg);
      break;
   }
}