Example #1
0
def receive():
	print("****receiving*****")
	mlen = s.recv(1)
	print("mlen: ",mlen)
	m_len = int.from_bytes(mlen, byteorder='big', signed=False)

	if m_len == 0:
		mlen =s.recv(1)
		print("mlen: ",mlen)
		m_len = int.from_bytes(mlen, byteorder='big', signed=False)

	print("after m_len:", m_len)
	data = s.recv(m_len)
	print("data: ",data)
	tmp = len(data)
	print("len: ", tmp)
	while tmp != m_len:
		print("incomplete")
		data+=s.recv(m_len-tmp)
		print("data: ", data)
		tmp = len(data)
		print("new tmp: ", tmp)
	msg = Message()
	msg.ParseFromString(data)
	if msg.type == 9:
		msg = Message4()
		msg.ParseFromString(data)
	print("")
	return msg
Example #2
0
    def reader_loop(self):
        while True:
            msg = self.recv()
            parsed_message = Message.FromString(msg[0])
            if parsed_message.type == Message.DECISION:
                #Ignore the past
                if parsed_message.instance <= self.last_received_instance:
                    continue
                self.maximum_instance = max(self.maximum_instance, parsed_message.instance)
                debug('{} {}'.format(self.last_received_instance, self.maximum_instance))
                debug(parsed_message)

                self.non_printed_instances[parsed_message.instance] = parsed_message.msg
                while (self.last_received_instance + 1) in self.non_printed_instances:
                    self.last_received_instance+=1
                    if parsed_message.msg != '':
                        sys.stdout.write("%s\n" % self.non_printed_instances[self.last_received_instance])
                        sys.stdout.flush()
                    self.decided[self.last_received_instance] = self.non_printed_instances[self.last_received_instance]
                if self.last_received_instance < self.maximum_instance:
                    message = Message(instance = self.last_received_instance+1,
                                      id = self._id,
                                      msg = '',
                                      type = Message.PROPOSAL)
                    debug('Catching up with message {}'.format(self.last_received_instance+1))
                    self.send(message, 'proposers')
Example #3
0
    def mainLoop(self):
        while True:
            for i in range(len(self.events)):
                handled = False
                for a in self.abstractions:
                    if not handled:
                        handled = a.handle(self.events[i])
                        x = a.getId()
                if handled:
                    del self.events[i]
                    break

            m = None
            self.lockNetworkMessages.acquire()
            if len(self.networkMessages):
                m = self.networkMessages.pop()
            self.lockNetworkMessages.release()
            if m:
                if m.type == Message.Type.NETWORK_MESSAGE:
                    x = m.networkMessage
                    sender = getProcessForKey(self.processes, x.senderHost,
                                              x.senderListeningPort)
                    self.systemId = m.systemId
                    if x.message.type == Message.Type.APP_PROPOSE:
                        self.events.append(x.message)
                    elif sender:
                        message = Message()
                        message.type = Message.Type.PL_DELIVER
                        message.abstractionId = m.abstractionId
                        message.plDeliver.message.CopyFrom(x.message)
                        message.plDeliver.sender.CopyFrom(sender)
                        self.events.append(message)
Example #4
0
    def reader_loop(self):
        while True:
            msg = self.recv()
            msg = Message.FromString(msg[0])
            if msg.type == Message.PHASE1A:
                if not msg.instance in self.state:
                    #Init with null
                    self.state[msg.instance] = InstanceState(msg.ballot)
                if msg.ballot >= self.state[msg.instance].ballot:
                    self.state[msg.instance] = self.state[msg.instance]._replace(ballot=msg.ballot)                   
                    message = Message(type = Message.PHASE1B,
                                      id = self._id,
                                      instance = msg.instance,
                                      **self.state[msg.instance]._asdict())
                    self.send(message, 'proposers')

            elif msg.type == Message.PHASE2A:
                debug('Received decide')
                debug(msg)
                if not msg.instance  in self.state:
                    continue
                debug(self.state[msg.instance])
                if (msg.ballot >= self.state[msg.instance].ballot and
                    msg.ballot != self.state[msg.instance].vballot):
                    self.state[msg.instance] = InstanceState(msg.ballot, 
                                                                           msg.ballot,
                                                                           msg.msg)
                    message = Message(instance = msg.instance,
                                      ballot = msg.ballot,
                                      type = Message.PHASE2B,
                                      id = self._id,
                                      msg = msg.msg)
                    self.send(message, 'proposers')
Example #5
0
def send(host: str, port: int, m: Message):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(1)
    s.connect((host, port))

    s.send(struct.pack('>I', m.ByteSize()))
    s.send(m.SerializeToString())
    s.close()
Example #6
0
    def bebDeliverDecided(self, l: ProcessId, v: Value):
        message = Message()
        message.epDecide.ets = self.ets
        message.epDecide.value.CopyFrom(v)
        message.abstractionId = self.getId()
        message.type = Message.Type.EP_DECIDE

        self.system.events.append(message)
Example #7
0
 def abort(self):
     self.halted = True
     message = Message()
     message.abstractionId = self.getId()
     message.type = Message.Type.EP_ABORTED
     message.epAborted.value.CopyFrom(self.state.val)
     message.epAborted.valueTimestamp = self.state.valts
     self.system.events.append(message)
Example #8
0
    def plDeliverHeartbeatRequest(self, p: ProcessId):
        message = Message()
        message.plSend.destination.CopyFrom(p)
        message.type = Message.Type.PL_SEND
        message.plSend.message.abstractionId = 'epfd'
        message.plSend.message.type = Message.Type.EPFD_HEARTBEAT_REPLY

        self.system.events.append(message)
Example #9
0
    def ecStartEpoch(self, newts, newl):
        self.newts = newts
        self.newl = newl
        message = Message()
        message.type = Message.Type.EP_ABORT
        message.abstractionId = 'ep' + self.ets.__str__()

        self.system.events.append(message)
        self.check()
Example #10
0
    def plDeliver(self, p: ProcessId, m):
        message = Message()

        message.bebDeliver.sender.CopyFrom(p)
        message.bebDeliver.message.CopyFrom(m)
        message.abstractionId = m.abstractionId
        message.type = Message.Type.BEB_DELIVER
        
        self.system.events.append(message)
Example #11
0
    def propose(self, v: Value):
        self.tmpval = v
        message = Message()

        message.abstractionId = 'beb'
        message.type = Message.Type.BEB_BROADCAST
        message.bebBroadcast.message.abstractionId = self.getId()
        message.bebBroadcast.message.type = Message.Type.EP_READ_

        self.system.events.append(message)
Example #12
0
 def registerApp(self):
     message = Message()
     networkMessage = NetworkMessage()
     networkMessage.senderHost = self.me.host
     networkMessage.senderListeningPort = self.me.port
     networkMessage.message.type = Message.Type.APP_REGISTRATION
     networkMessage.message.appRegistration.owner = self.me.owner
     networkMessage.message.appRegistration.index = self.me.index
     message.type = Message.Type.NETWORK_MESSAGE
     message.networkMessage.CopyFrom(networkMessage)
     send(self.hub.host, self.hub.port, message)
Example #13
0
    def bebDeliverWrite(self, l: ProcessId, v: Value):
        self.state.valts = self.ets
        self.state.val = v
        message = Message()

        message.plSend.destination.CopyFrom(l)
        message.type = Message.Type.PL_SEND
        message.plSend.message.abstractionId = self.getId()
        message.plSend.message.type = Message.Type.EP_ACCEPT_

        self.system.events.append(message)
Example #14
0
    def bebDeliverRead(self, l: ProcessId):
        message = Message()

        message.plSend.destination.CopyFrom(l)
        message.type = Message.Type.PL_SEND
        message.plSend.message.abstractionId = self.getId()
        message.plSend.message.type = Message.Type.EP_STATE_
        message.plSend.message.epState_.value.CopyFrom(self.state.val)
        message.plSend.message.epState_.valueTimestamp = self.state.valts

        self.system.events.append(message)
Example #15
0
    def check(self):
        if self.l == self.system.me and self.val.defined and self.proposed == False:
            self.proposed = True
            message = Message()

            epPropose = EpPropose()
            message.abstractionId = 'ep' + self.ets.__str__()
            epPropose.value.CopyFrom(self.val)
            message.epPropose.CopyFrom(epPropose)
            message.type = Message.Type.EP_PROPOSE

            self.system.events.append(message)
Example #16
0
    def bebBroadcast(self, m):
        for p in self.system.processes.values():
            message = Message()
            plSend = PlSend()
            aId = m.message.abstractionId

            plSend.destination.CopyFrom(p)
            plSend.message.CopyFrom(m.message)

            message.abstractionId = aId
            message.plSend.CopyFrom(plSend)
            message.type = Message.Type.PL_SEND
            self.system.events.append(message)
Example #17
0
    def epDecide(self, ts, v: Value):
        if self.ets == ts:
            if self.decided == False:
                self.decided = True
                message = Message()
                ucDecide = UcDecide()

                ucDecide.value.CopyFrom(v)
                message.ucDecide.CopyFrom(ucDecide)
                message.type = Message.Type.UC_DECIDE

                self.system.events.append(message)
            self.check()
Example #18
0
    def plSend(self, pS, aId, sId):
        m = Message()
        nm = NetworkMessage()

        nm.message.CopyFrom(pS.message)
        nm.senderHost = self.system.me.host
        nm.senderListeningPort = self.system.me.port

        m.abstractionId = aId
        m.networkMessage.CopyFrom(nm)
        m.systemId = self.system.systemId
        m.type = Message.Type.NETWORK_MESSAGE

        send(pS.destination.host, pS.destination.port, m)
Example #19
0
    def plDeliver(self, p: ProcessId):
        if getKeyForProcess(self.trusted) == getKeyForProcess(self.system.me):
            self.ts += len(self.system.processes)
            message = Message()
            bebBroadcast = BebBroadcast()

            bebBroadcast.message.abstractionId = 'ec'
            bebBroadcast.message.ecNewEpoch_.timestamp = self.ts
            bebBroadcast.message.type = Message.Type.EC_NEW_EPOCH_

            message.abstractionId = 'beb'
            message.bebBroadcast.CopyFrom(bebBroadcast)
            message.type = Message.Type.BEB_BROADCAST

            self.system.events.append(message)
Example #20
0
    def checkAccepted(self):
        if self.accepted > len(self.system.processes) / 2:
            self.accepted = 0
            message = Message()
            bB = BebBroadcast()

            bB.message.abstractionId = self.getId()
            bB.message.type = Message.Type.EP_DECIDED_
            bB.message.epDecided_.value.CopyFrom(self.tmpval)

            message.abstractionId = 'beb'
            message.bebBroadcast.CopyFrom(bB)
            message.type = Message.Type.BEB_BROADCAST

            self.system.events.append(message)
Example #21
0
    def checkStates(self):
        if len(self.states) > len(self.system.processes) / 2:
            st = self.highest()
            if st.val.defined:
                self.tmpval = st.val
            for k in self.states.keys():
                del self.states[k]

            message = Message()

            message.abstractionId = 'beb'
            message.type = Message.Type.BEB_BROADCAST
            message.bebBroadcast.message.abstractionId = self.getId()
            message.bebBroadcast.message.type = Message.Type.EP_WRITE_
            message.bebBroadcast.message.epWrite_.value.CopyFrom(self.tmpval)

            self.system.events.append(message)
Example #22
0
    def check(self):
        notSuspected = dict()
        for key in self.system.processes.keys():
            if key not in self.suspected.keys():
                notSuspected[key] = self.system.processes[key]

        maximum = getMax(notSuspected)
        if not maximum:
            return

        if not self.leader or (not self.leader.rank == maximum.rank):
            self.leader = maximum

            message = Message()
            message.abstractionId = 'ec'
            message.type = Message.Type.ELD_TRUST
            message.eldTrust.process.CopyFrom(maximum)
            self.system.events.append(message)
Example #23
0
 def run(self):
     while True:
         msg = Message()
         try:
             data = self.van.receiveSocket.recv()
             msg.ParseFromString(data)
         except:
             if self.van.closed:
                 return
             logger.error("receive message error: " +
                          traceback.format_exc(0) +
                          " messgae length {}".format(len(data)))
         else:
             if msg.Command != PING_ACK and msg.Command != PING:
                 pass
                 # logger.debug("receive command {} for msg {} from {}, data length {}".format(msg.Command,msg.RespondMsgId,msg.Sender.Host,len(data)))
             if msg.RespondMsgId > 0:
                 self.van.waitedMsgCache[msg.RespondMsgId] = msg
             self.van.msgChannel.append(msg)
Example #24
0
    def server(self):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        s.bind((self.me.host, self.me.port))
        s.listen(100)

        while True:
            conn, addr = s.accept()
            n = struct.unpack('>I', conn.recv(4))[0]
            raw = struct.unpack(">%ds" % n, conn.recv(n))[0]

            if raw:
                message = Message()
                message.ParseFromString(raw)

                self.lockNetworkMessages.acquire()
                self.networkMessages.append(message)
                self.lockNetworkMessages.release()
            conn.close()
        s.close()
Example #25
0
    def appPropose(self, v: Value, processes: []):
        for p in processes:
            key = getKeyForProcess(p)
            self.system.processes[key] = p
            if key == getKeyForProcess(self.system.me):
                self.system.me.rank = p.rank

        if len(processes):
            self.system.abstractions.append(Pl(self.system))
            self.system.abstractions.append(Ec(self.system))
            self.system.abstractions.append(Uc(self.system))
            self.system.abstractions.append(Eld(self.system))
            self.system.abstractions.append(Beb(self.system))
            self.system.abstractions.append(Epfd(self.system))
        
        m = Message()
        m.abstractionId = 'uc'
        m.ucPropose.value.CopyFrom(v)
        m.type = Message.Type.UC_PROPOSE
        self.system.events.append(m)
Example #26
0
def request(x):
	print("****requesting*****")
	msg = Message()
	msg.type = x
	m_len = len(msg.SerializeToString())
	s.send(struct.pack('>H', m_len))
	s.send(msg.SerializeToString())
	print("send: ",struct.pack('>H', m_len))
	print("send: ",msg.SerializeToString())
	print("")
Example #27
0
    def ucDecide(self, v: Value):
        m = Message()
        appDecide = Message()

        appDecide.appDecide.value.CopyFrom(v)
        appDecide.type = Message.Type.APP_DECIDE

        m.type = Message.Type.PL_SEND
        m.plSend.message.CopyFrom(appDecide)
        m.plSend.destination.CopyFrom(self.system.hub)

        self.system.events.append(m)
        self.system.abstractions = self.system.abstractions[0:2]
Example #28
0
    def bebDeliver(self, p: ProcessId, newTs: int):
        if getKeyForProcess(p) == getKeyForProcess(
                self.trusted) and newTs > self.lastts:
            message = Message()

            message.abstractionId = 'uc'
            message.ecStartEpoch.newLeader.CopyFrom(p)
            message.ecStartEpoch.newTimestamp = newTs
            message.type = Message.Type.EC_START_EPOCH
            self.system.events.append(message)
        else:
            self.lastts = newTs
            message = Message()

            message.plSend.destination.CopyFrom(p)
            message.type = Message.Type.PL_SEND
            message.plSend.message.abstractionId = 'ec'
            message.plSend.message.type = Message.Type.EC_NACK_

            self.system.events.append(message)
Example #29
0
	host_ip = socket.gethostbyname(HOST) 
except socket.gaierror: 
  
	# this means could not resolve the host 
	print ("there was an error resolving the host")
	sys.exit() 
  
# connecting to the server 
s.connect((host_ip, PORT)) 
  
print ("the socket has successfully connected to cs177 on port == %s" %(host_ip)) 

# m_len = 1
# s.send(struct.pack("H",m_len))
# s.send(m_len)
msg = Message()
msg.type = 0


m_len = len(msg.SerializeToString())

# s.send(str(m_len).encode("utf-8"))
s.send(struct.pack('>H', m_len))
s.send(msg.SerializeToString())
# print("send: ",str(m_len).encode("utf-8"))
print("send: ",struct.pack('>H', m_len))
print("send: ",msg.SerializeToString())
print("m_len: ",m_len)

print("*************recv state***********")
mlen = s.recv(m_len)
Example #30
0
    def reader_loop(self):
        while True:
            msg = self.recv()
            parsed_message = Message.FromString(msg[0])
            #See if it's time to change the leader
            if parsed_message.type == Message.LEADER:
                #See if we are in sync with instances
                if parsed_message.instance > self.instance:
                    self.instance = parsed_message.instance
                self.leaders[parsed_message.id] = time.time()
                k = sorted(self.leaders.keys())[0]
                if time.time() - self.leaders[k] > 2*self.get_timeout_msgs():
                    #Not the leader anymore
                    del self.leaders[k]
                    if len(self.leaders) > 0:
                        self.leader = sorted(self.leaders.keys())[0]
                    else:
                        self.leader = self._id
                    info('Switchig to next leader {}'.format(self.leader))
                else:
                    self.leader = k

            #Im not the leader
            if self.leader != self._id:
                continue
            #Got a Proposal from client
            if parsed_message.type == Message.PROPOSAL:
                debug(parsed_message)
                self.state[self.instance] = InstanceState(ballot = self._id,
                                                          timestamp = time.time(),
                                                          phase = Message.PHASE1A,
                                                          msg = parsed_message.msg)
                #Build Phase1A
                message = Message(type = Message.PHASE1A,
                                              id = self._id)
                #learner catch up msg
                if parsed_message.instance != -1:
                    message.instance = parsed_message.instance
                    if not parsed_message.instance in self.state:
                        self.state[parsed_message.instance] = InstanceState(ballot = self._id,
                                                                            timestamp = time.time(),
                                                                            phase = Message.PHASE1A,
                                                                            msg = parsed_message.msg)
                    message.ballot = self.state[parsed_message.instance].ballot +100
                    self.state[parsed_message.instance] = self.state[parsed_message.instance]._replace(ballot = message.ballot)
                #client instance
                else:
                    message.ballot = self.state[self.instance].ballot
                    message.instance = self.instance
                    self.instance+=1
                self.send(message, 'acceptors')
            
            #Got a Phase 1B
            elif (parsed_message.type == Message.PHASE1B and
            parsed_message.instance in self.state):
                if not parsed_message.instance in self.acceptor_messages:
                    self.acceptor_messages[parsed_message.instance] = [parsed_message]
                else:
                    self.acceptor_messages[parsed_message.instance].append(parsed_message)
                #Already sent 2A (had a quorum)
                if self.state[parsed_message.instance].phase == Message.PHASE2A:
                    continue
                #See if quorum is reached
                n_msgs = set([])
                current_propose = (-1, self.state[parsed_message.instance].msg)
                debug(self.acceptor_messages)
                for msg in self.acceptor_messages[parsed_message.instance]:
                    debug(msg)
                    if msg.ballot == self.state[parsed_message.instance].ballot:
                        n_msgs.add(msg.id)
                        current_propose = max(current_propose, 
                        (parsed_message.vballot, parsed_message.vmsg))
                    debug(n_msgs)

                if len(n_msgs) >= self.get_number_of_acceptors()//2+1:
                    debug('Quorum reached, initiating 2A')
                    self.state[parsed_message.instance] = self.state[parsed_message.instance]._replace(phase = Message.PHASE2A, timestamp = time.time())
                    message = Message(type = Message.PHASE2A,
                                      id = self._id,
                                      ballot = self.state[parsed_message.instance].ballot,
                                      msg = current_propose[1],
                                      instance = parsed_message.instance)
                    self.send(message, 'acceptors')

            #Received phase 2B
            elif (parsed_message.type == Message.PHASE2B and
            parsed_message.instance in self.state):
                if not parsed_message.instance in self.acceptor_decide:
                    self.acceptor_decide[parsed_message.instance] = [parsed_message]
                else:
                    self.acceptor_decide[parsed_message.instance].append(parsed_message)
                n_msgs = set([])
                for msg in self.acceptor_decide[parsed_message.instance]:
                    n_msgs.add(msg.id)
                
                #Quorum reached, must inform learners
                if (len(n_msgs) >= (self.get_number_of_acceptors()+1)/2 and
                    self.state[parsed_message.instance].phase != Message.DECISION):
                    debug('Informing decide to learners')
                    debug(parsed_message)

                    self.state[parsed_message.instance] = self.state[parsed_message.instance]._replace(phase = Message.DECISION, timestamp = time.time())

                    message = Message(type = Message.DECISION,
                                                  id = self._id,
                                                  msg = parsed_message.msg,
                                                  instance = parsed_message.instance)
                    self.send(message, 'learners')
Example #31
0
#!/usr/bin/python

import sys

from message_pb2 import Message
from request1_pb2 import Request1

msg = Message()
req1 = Request1()

msg.commandType = Message.REQ_1
msg.request1.arg1 = "Test string#1"
msg.request1.arg2 = "Blah blah blah"

print(msg.__str__())
Example #32
0
    return ms.decode('hex')

def pad(mi):
    return (mi << 192) | bytes_to_int(get_padding())

def unpad(mi):
    return mi >> 192

def encrypt(message):
    ciphertext = pow(pad(bytes_to_int(message)), publicExponent, modulus)
    return int_to_bytes(ciphertext)

def decrypt(ciphertext):
    plaintext = unpad(pow(bytes_to_int(ciphertext), privateExponent, modulus))
    return int_to_bytes(plaintext)

with open('message.txt', 'r') as f:
    flag_message = f.read().strip()

message = Message(id=0, msg=flag_message)
m1 = message.SerializeToString()
ct1 = encrypt(message.SerializeToString())
message.id = 1
m2 = message.SerializeToString()
ct2 = encrypt(message.SerializeToString())
print hex(pad(bytes_to_int(m2)) - (pad(bytes_to_int(m1)) + 2**2344   ))
print modulus
print len(message.SerializeToString())
print ct1.encode('hex')
print ct2.encode('hex')