Ejemplo n.º 1
0
    def add_slave(self, pathid, _slave_tuple: (str, int)):
        """

        :param int:
        :param _slave_tuple: tuple
        :type pathid: int
        """
        # after Server requests to add a slave, master decided that should be _slave_tuple
        assert isinstance(pathid, int)
        if not self.slaves.__contains__(pathid):
            self.slaves[pathid] = slave_config(
                _slave_tuple[0], _slave_tuple[1],
                socket.socket(socket.AF_INET, socket.SOCK_DGRAM))
            this_slave = self.slaves[pathid]
            this_slave.sock.bind(
                client_address.addr_dict[pathid])  # binding on slave tuple
            print('debug: PathID {0} is {1!s} ...'.format(
                pathid, _slave_tuple))
            segment = SEGMENT(
                pTYPES.RESPONSE_TO_ADD_SLAVE, 0, 0, pathid, 0, 0,
                str(client_address.addr_dict[pathid]).strip('(').strip(')'))
            # pkt_type, pkt_seq, pkt_ack, pathid, pkt_ratio, opt, pkt_data
            self.master.send(segment.encap().encode())
        else:
            print('debug: {0} has already existed...'.format(pathid))
Ejemplo n.º 2
0
    def waiting_for_order(self):
        while True:
            print('debug: Master is waiting for order...')
            recv_packet = self.master.recv(65535).decode()
            print(recv_packet)
            if recv_packet == '':
                print('debug: empty packet wtf...?')
                break
            segment = SEGMENT.decap(recv_packet)
            if segment.pkt_type == pTYPES.REQUEST_TO_ADD_SLAVE:
                print(
                    'debug: sending message to add slave...\ncreat a event of path{}'
                    .format(segment.pathid))
                # self.slave_start_event[segment.pathid] = Event()
                self.add_slave(segment.pathid,
                               server_address.addr_dict[segment.pathid])
                self.slave_offset += 1
            else:
                if segment.pkt_type == pTYPES.KILL_MASTER:
                    segment = SEGMENT(
                        pTYPES.KILL_MASTER, 0, 0, 0, 0, 0, ''
                    )  # pkt_type, pkt_seq, pkt_ack, pathid, pkt_ratio, opt, pkt_data
                    send_packet = segment.encap()  # 打包并发出
                    self.master.send(send_packet.encode())
                    print('debug: Long live the Soviet!!!')
                    break
                else:
                    if segment.pkt_type == pTYPES.MASTER_HEARTBEAT:
                        print('Master is  alive!')
                    else:
                        if segment.pkt_type == pTYPES.NO_MORE_SLAVE:
                            break

        print('debug: No longer waiting, Master is dead...')
Ejemplo n.º 3
0
 def kill_master(self):
     segment = SEGMENT(
         pTYPES.KILL_MASTER, 0, 0, 0, 0, 0,
         '')  # pkt_type, pkt_seq, pkt_ack, pathid, pkt_ratio, opt, pkt_data
     send_packet = segment.encap()  # 打包并发出
     self.master.send(send_packet.encode())
     print('debug: Slaves\' revolution!!!')
     recv_packet = self.master.recv(65535)  # 得到数据包
     recv_segment = SEGMENT.decap(recv_packet.decode())
     if recv_segment.pkt_type == pTYPES.KILL_MASTER:
         print('debug: Long live the Soviet!!!')
         sock = self.master
         sock.close()
Ejemplo n.º 4
0
    def put_segment_and_encap(self, segment):
        """

        :type segment: SEGMENT
        """
        isinstance(segment, SEGMENT)
        self.buffer.put((segment.pkt_seq, SEGMENT.encap(segment)))
Ejemplo n.º 5
0
 def waiting_for_ack(self):
     while True:
         print('debug: Server is waiting for acks...')
         ack_pkt = self.master.recv(65535).decode()
         ack_segment = SEGMENT.decap(ack_pkt)
         if ack_segment.pkt_type == pTYPES.ACK:  # 这里要给一个mailbox发送pkt_seq,然后重传这些包
             self.retrans_seq.append(ack_segment.pkt_ack)
             print('debug: Got a ACK{}'.format(ack_segment.pkt_ack))
         else:
             if ack_segment.pkt_type == pTYPES.DONE_TRANSMISSION:
                 print('debug: Client done...')
                 break
Ejemplo n.º 6
0
 def request_to_add_slave(self):
     segment = SEGMENT(pTYPES.REQUEST_TO_ADD_SLAVE, 0, 0, self.slave_offset,
                       0, 0, str(server_address.addr_dict[0]))
     # pkt_type, pkt_seq, pkt_ack, pathid, pkt_ratio, opt, pkt_data
     send_packet = segment.encap()  # 打包并发出
     self.master.send(send_packet.encode())
     print('debug: Sending request to add a slave...')
     recv_packet = self.master.recv(65535)  # 得到数据包
     print('debug: response received...')
     recv_segment = SEGMENT.decap(recv_packet.decode())
     if recv_segment.pkt_type == pTYPES.RESPONSE_TO_ADD_SLAVE:
         slave_tuple = (  # 从client获得client的本地地址
             recv_segment.pkt_data.split(',')[0].strip('\'').strip('\''),
             int(recv_segment.pkt_data.split(',')[1]))
         # IP,port,socket
         print('debug: Now slave {0} has been added...'.format(slave_tuple))
         self.slaves[segment.pathid] = slave_config(
             slave_tuple[0], slave_tuple[1],
             socket.socket(socket.AF_INET, socket.SOCK_DGRAM))
         this_slave = self.slaves[segment.pathid]
         this_slave.sock.bind(server_address.addr_dict[segment.pathid])
         self.slave_start_event[
             segment.pathid] = Event()  # 等待slave开始运行后再进行操作
         self.slave_offset += 1
Ejemplo n.º 7
0
 def grabdata(self, pathid: int, buffer: BUFFER):
     assert isinstance(buffer, BUFFER)
     if self.slaves.__contains__(pathid):
         this_slave = self.slaves[pathid]
         while True:
             print('debug: waiting for packet at Path{0}...'.format(
                 this_slave.sock.getsockname()))
             data, source = this_slave.sock.recvfrom(65535)
             print('debug: got a packet at Path{0}...'.format(pathid))
             this_segment = SEGMENT.decap(
                 data.decode()
             )  # decap the data, the data is encoded and need to be decoded
             this_segment.showseg('')
             buffer.put((this_segment.pkt_seq, this_segment.encap()))
             # buffer.put_segment_and_encap(this_segment)
             if this_segment.pkt_type == pTYPES.END or this_segment.pkt_type == pTYPES.END_OF_SLAVE:
                 break
Ejemplo n.º 8
0
                    break

    # def slave_run(self, pathid: int):  # meixiehao!!
    #     t = Thread(target=self.slaves[pathid].send_data_through_slave, args=self.buffer)
    #     t.start()
    #     self.slave_start_event[pathid].set()  # 指示该slave已经开始运行
    #     # threading


m = Master_TCP()
m.wait_for_handshake()
m.request_to_add_slave()
print(m.slaves)
m.request_to_add_slave()
print(m.slaves)
segment = SEGMENT(pTYPES.NO_MORE_SLAVE, 0, 0, 0, 1, 0, '')
m.master.send(segment.encap().encode())

pkt = m.master.recv(65535).decode()
segment = SEGMENT.decap(pkt)
if segment.pkt_type == pTYPES.I_WANT_DATA:
    pass
else:
    print('debug: NO Data wanted..')

with open('send.txt', 'r') as file:
    raw_file = str(file.read())
seq = 0
for i in range(0, len(raw_file), 5000):
    if i + 5000 < len(raw_file):
        this_segment = SEGMENT(pTYPES.CHUNK, seq, 0, 0, 1, 0,
Ejemplo n.º 9
0
                    if segment.pkt_type == pTYPES.MASTER_HEARTBEAT:
                        print('Master is  alive!')
                    else:
                        if segment.pkt_type == pTYPES.NO_MORE_SLAVE:
                            break

        print('debug: No longer waiting, Master is dead...')


m = Master_TCP()
m.handshake(server_address.addr_dict[0])  # Server ip
t_master = Thread(target=m.waiting_for_order, daemon=True)
t_master.start()
t_master.join()

segment = SEGMENT(pTYPES.I_WANT_DATA, 0, 0, 0, 1, 0, '')
m.master.send(segment.encap().encode())
print('debug: I want data...')

data_segment = []
pathids = list(m.slaves.keys())
slave_post_office = []
for pathid in pathids:
    t_slave_grab_data = Thread(target=m.grabdata, args=(pathid, m.buffer))
    slave_post_office.append(t_slave_grab_data)
for slave in slave_post_office:
    slave.start()
time.sleep(3)
for slave in slave_post_office:
    slave.join()
# m.grabdata(1, m.buffer)
Ejemplo n.º 10
0
import socket
from TNU.modules.xSEGMENT import SEGMENT
from TNU.modules.xSTATES import pTYPES

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
buff = []
# i = 0
s.bind(('127.0.0.1', 9889))
print('Binding UDP on 9889...')
while True:
    data, source = s.recvfrom(65535)
    segmnt = SEGMENT.decap(data.decode(
    ))  # decap the data, the data is encoded and need to be decoded
    segmnt.showseg('all')

    print('Receiving from {0!r}{1}...'.format(source, segmnt.pkt_seq))
    # segmnt = SEGMENT(_pTYPES.END, 0, segmnt.pkt_ack, 0, 0, '')  # pkt_type, pkt_seq, pkt_ack, pkt_ratio, opt, pkt_data
    # packet = segmnt.encap()
    # s.sendto(packet.encode(), source)

    # i += 1
    if segmnt.pkt_type == pTYPES.END:
        buff.append(segmnt.pkt_data)
        break
    else:
        if segmnt.pkt_type == pTYPES.CHUNK:  # ???
            buff.append(segmnt.pkt_data)
print('break')
buffer = "".join(buff)
s.close()