Exemplo n.º 1
0
def message_process(message):
  name = message.get_addr()
  data = message.get_mess()
  if message.is_client():
    if communication.am_leader():
      propose.propose(message)
    else:
      communication.send_mess(Message(Addr = name, Wait = True, Client = True,
        Message = "%s" % communication.LEADER))
  elif len(data)>1:
    data_split = data.split(":")
    function = data_split[0]
    response = None
    if function == "SYN":
      communication.send_mess(Message(Addr = name, Message = "ACK"))
      print "[NODE %d: %s] is online" % (communication.NODE_ID[name], name)
      communication.set_online(name)
      heartbeat.reset(name)

    elif function == "H":
      heartbeat.heartbeat_process(name, data_split[1:])

    elif function == "P":
      response = accept.propose_process(data_split[1:])
      response = "PR:"+response
      communication.send_mess(Message(Addr = name, Wait = True, Message = response))

    elif function == "PR":
      propose.prepare_response(data_split[1:])

    elif function == "A":
      response = accept.accept_response(data_split[1:])
      response = "AR:"+response
      communication.send_mess(Message(Addr = name, Wait = True, Message = response))

    elif function == "AR":
      response = propose.accept_response(data_split[1:])
      if response:
        response = "S:"+response
        communication.send_mess(Message(Addr = name, Wait = True, Message = response))

    elif function == "S":
      response = accept.success(data_split[1:])
      if response:
        response = "SR:"+response
        communication.send_mess(Message(Addr = name, Wait = True, Message = response))

    elif function == "SR":
      response = propose.success_response(int(data_split[1]))
      if response:
        response = "S:"+response
        communication.send_mess(Message(Addr = name, Wait = True, Message = response))
Exemplo n.º 2
0
def local_message_process(message):
  data = message.get_mess()
  # print ("[%s] %s") %("Local", data)
  data_split = data.split(":")
  function = data_split[0]
  response = None
  if function == "H":
      heartbeat.heartbeat_process(communication.HOSTNAME, data_split[1:])
  elif function == "P":
    response = accept.propose_process(data_split[1:])
    response = "PR:"+response
    local_response = Message(Local = True, Message = response)
    communication.send_local_mess(local_response)

  elif function == "PR":
    propose.prepare_response(data_split[1:])

  elif function == "A":
    response = accept.accept_response(data_split[1:])
    response = "AR:"+response
    local_response = Message(Local = True, Message = response)
    communication.send_local_mess(local_response)

  elif function == "AR":
    reponse = propose.accept_response(data_split[1:])
    if response:
      response = "S:"+response
      local_response = Message(Local = True, Message = response)
      communication.send_local_mess(local_response)

  elif function == "S":
    response = accept.success(data_split[1:])
    if response:
      response = "SR:"+response
      local_response = Message(Local = True, Message = response)
      communication.send_local_mess(local_response)

  elif function == "SR":
    response = propose.success_response(int(data[1]))
    if response:
      response = "S:"+response
      local_response = Message(Local = True, Message = response)
      communication.send_local_mess(local_response)