Ejemplo n.º 1
0
def prepare_response(args):
  global ClientsAddr, ClientsId
  global P_Proposals, P_Values, NumAccepted, P_Accepted, NumPrepared
  [index, prepared_ProposalId, prepared_clientid, prepared_value] = args
  index = int(index)

  if not P_Accepted[index]:
    NumPrepared[index] += 1
    if float(P_Proposals[index]) < float(prepared_ProposalId):
      if ClientsId[index] != "P":
        propose(message.Message(Addr = ClientsAddr[index],
          Message="%s:%s"%(ClientsId[index].split(".")[1], P_Proposals[index])))
        del ClientsAddr[index]
      P_Proposals[index] = prepared_ProposalId
      P_Values[index] = prepared_value
      ClientsId[index] = prepared_clientid

    if NumPrepared[index] == communication.MAJORITY and P_Values[index] != "PI":
      P_Accepted[index] = True

      response = "A:%d:%s:%s:%s"%(index, P_Proposals[index],  ClientsId[index], P_Values[index])
      response_mess = message.Message(Wait = True, Message = response)
      communication.send_mess(response_mess)
      local_response_mess = message.Message(Local = True, Message = response)
      communication.send_local_mess(local_response_mess)
Ejemplo n.º 2
0
def prepare_response(args):
    global ClientsAddr, ClientsId
    global P_Proposals, P_Values, NumAccepted, P_Accepted, NumPrepared
    [index, prepared_ProposalId, prepared_clientid, prepared_value] = args
    index = int(index)

    if not P_Accepted[index]:
        NumPrepared[index] += 1
        if float(P_Proposals[index]) < float(prepared_ProposalId):
            if ClientsId[index] != "P":
                propose(
                    message.Message(
                        Addr=ClientsAddr[index],
                        Message="%s:%s" %
                        (ClientsId[index].split(".")[1], P_Proposals[index])))
                del ClientsAddr[index]
            P_Proposals[index] = prepared_ProposalId
            P_Values[index] = prepared_value
            ClientsId[index] = prepared_clientid

        if NumPrepared[
                index] == communication.MAJORITY and P_Values[index] != "PI":
            P_Accepted[index] = True

            response = "A:%d:%s:%s:%s" % (index, P_Proposals[index],
                                          ClientsId[index], P_Values[index])
            response_mess = message.Message(Wait=True, Message=response)
            communication.send_mess(response_mess)
            local_response_mess = message.Message(Local=True, Message=response)
            communication.send_local_mess(local_response_mess)
Ejemplo n.º 3
0
def client_respond(addr, index, dbResponse):
  if addr:
    value = P_Values[index]
    client_reply_mess = "R:%s:%s"%(value, dbResponse)
    client_reply = message.Message(Addr = addr, Wait = True, Client = True,
      Message = client_reply_mess)
    communication.send_mess(client_reply)
Ejemplo n.º 4
0
def client_respond(addr, index, dbResponse):
    if addr:
        value = P_Values[index]
        client_reply_mess = "R:%s:%s" % (value, dbResponse)
        client_reply = message.Message(Addr=addr,
                                       Wait=True,
                                       Client=True,
                                       Message=client_reply_mess)
        communication.send_mess(client_reply)
Ejemplo n.º 5
0
def heartbeat_send():
  global LAST_BEAT
  if (time.time()-LAST_BEAT) > BEAT_DELAY:
    lead = communication.LEADER
    if not lead or not communication.STATUS[lead]:
      communication.choose_new_leader()
      lead = communication.NEW_LEADER
    heartbeat_mess = "H:%s:%d:%d"%(lead, communication.ROUND, accept.get_largest_index())
    communication.send_mess(message.Message(Message = heartbeat_mess))
    communication.send_local_mess(message.Message(Local = True, Message = heartbeat_mess))
    LAST_BEAT = time.time()
Ejemplo n.º 6
0
def heartbeat_send():
    global LAST_BEAT
    if (time.time() - LAST_BEAT) > BEAT_DELAY:
        lead = communication.LEADER
        if not lead or not communication.STATUS[lead]:
            communication.choose_new_leader()
            lead = communication.NEW_LEADER
        heartbeat_mess = "H:%s:%d:%d" % (lead, communication.ROUND,
                                         accept.get_largest_index())
        communication.send_mess(message.Message(Message=heartbeat_mess))
        communication.send_local_mess(
            message.Message(Local=True, Message=heartbeat_mess))
        LAST_BEAT = time.time()
Ejemplo n.º 7
0
def propose(mess):
    global ClientsAddr, ClientsId
    global P_Proposals, P_Values, NumAccepted, P_Accepted, NumPrepared, Chosen
    global firstUnchosenIndex, EmptyIndex

    client_addr = mess.get_addr()
    data = mess.get_mess()
    [message_id, value] = data.split(":")

    if client_addr:
        clientip = client_addr[0].split(".")[-1]
        client_id = "%s.%s" % (clientip, message_id)
    else:
        client_id = "P"

    next_index = EmptyIndex
    if client_id != "P":
        for index, cid in ClientsId.items():
            if cid == client_id:
                if Chosen[index]:
                    if index in DB_Response.keys():
                        client_respond(client_addr, index, DB_Response[index])
                    return
                else:
                    next_index = index
                    break

    ClientsAddr[next_index] = client_addr
    P_Proposals[next_index] = ProposalId
    P_Values[next_index] = value
    ClientsId[next_index] = client_id
    NumPrepared[next_index] = 0
    P_Accepted[next_index] = False
    NumAccepted[next_index] = 0
    Chosen[next_index] = False

    proposal = "P:%s:%s:%s:%d:%d" % (ProposalId, value, client_id, next_index,
                                     firstUnchosenIndex)
    proposal_mess = message.Message(Wait=True, Message=proposal)
    communication.send_mess(proposal_mess)
    local_proposal_mess = message.Message(Local=True, Message=proposal)
    communication.send_local_mess(local_proposal_mess)

    EmptyIndex += 1
Ejemplo n.º 8
0
def propose(mess):
  global ClientsAddr, ClientsId
  global P_Proposals, P_Values, NumAccepted, P_Accepted, NumPrepared, Chosen
  global firstUnchosenIndex, EmptyIndex

  client_addr = mess.get_addr()
  data = mess.get_mess()
  [message_id, value] = data.split(":")

  if client_addr:
    clientip = client_addr[0].split(".")[-1]
    client_id = "%s.%s"%(clientip,message_id)
  else:
    client_id = "P"

  next_index = EmptyIndex
  if client_id != "P":
    for index, cid in ClientsId.items():
      if cid == client_id:
        if Chosen[index]:
          if index in DB_Response.keys():
            client_respond(client_addr, index, DB_Response[index])
          return
        else:
          next_index = index
          break

  ClientsAddr[next_index] = client_addr
  P_Proposals[next_index] = ProposalId
  P_Values[next_index] = value
  ClientsId[next_index] = client_id
  NumPrepared[next_index] = 0
  P_Accepted[next_index] = False
  NumAccepted[next_index] = 0
  Chosen[next_index] = False

  proposal = "P:%s:%s:%s:%d:%d"%(ProposalId, value, client_id,
    next_index, firstUnchosenIndex)
  proposal_mess = message.Message(Wait = True, Message = proposal)
  communication.send_mess(proposal_mess)
  local_proposal_mess = message.Message(Local = True, Message = proposal)
  communication.send_local_mess(local_proposal_mess)

  EmptyIndex+=1
Ejemplo n.º 9
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))