Ejemplo n.º 1
0
    def dispatch(self, action, addr):
        if action['type'] == 'newpeer':
            print("A new peer is coming")
            self.peers[action['data']] = addr
            # print(addr)
            pu.sendJS(self.udp_socket, addr, {
                "type": 'peers',
                "data": self.peers
            })

        if action['type'] == 'peers':
            print("Received a bunch of peers")
            self.peers.update(action['data'])
            # introduce youself.
            pu.broadcastJS(self.udp_socket, {
                "type": "introduce",
                "data": self.myid
            }, self.peers)

        if action['type'] == 'introduce':
            print("Get a new friend.")
            self.peers[action['data']] = addr

        if action['type'] == 'input':
            print(action['data'])
Ejemplo n.º 2
0
    def rece(self):
        while 1:
            data, addr = pu.recembase(self.udp_socket)
            action = json.loads(data)
            print(action["type"])
            if action['type'] == 'newpeer':
                print("A new peer is coming")
                self.peers[action['data']] = addr
                print(addr)
                pu.sendJS(self.udp_socket, addr, {
                    "type": 'peers',
                    "data": self.peers
                })

            if action['type'] == 'newProduct':
                print('Someone post a new product')
                newProduct = Product(action)
                newProduct.printInfo()
                if newProduct.name in self.product_lst:
                    self.product_lst[newProduct.name].append(newProduct)
                else:
                    self.product_lst[newProduct.name] = [newProduct]
                #end if

            if action['type'] == 'peers':
                print("Received a bunch of peers")
                self.peers.update(action['data'])
                # introduce youself.
                pu.broadcastJS(self.udp_socket, {
                    "type": "introduce",
                    "data": self.myid
                }, self.peers)

            if action['type'] == 'introduce':
                print("Get a new friend.")
                self.peers[action['data']] = addr

            if action['type'] == 'input':
                print(action['data'])

            if action['type'] == 'exit':
                if (self.myid == action['data']):
                    #cannot be closed too fast.
                    time.sleep(0.5)
                    break
                    # self.udp_socket.close()
                value, key = self.peers.pop(action['data'])
                print(action['data'] + " is left.")
Ejemplo n.º 3
0
 def send(self):
     while 1:
         msg_input = input("$:")
         if msg_input == "exit":
             break
         if msg_input == "friends":
             print(self.peers)
             continue
         l = msg_input.split()
         if l[-1] in self.peers.keys():
             toA = self.peers[l[-1]]
             s = ' '.join(l[:-1])
             pu.sendJS(self.udp_socket, toA, {"type": "input", "data": s})
         else:
             pu.broadcastJS(self.udp_socket, {
                 "type": "input",
                 "data": msg_input
             }, self.peers)
             continue
Ejemplo n.º 4
0
Archivo: main.py Proyecto: anyangge/P2P
    def rece(self):
        while 1:
            data, addr = pu.recembase(self.udp_socket)
            action = json.loads(data)
            # print(action["type"])
            #     self.dispatch(action, addr)
            # def dispatch(self, action,addr):
            if action['type'] == 'newpeer':
                print("A new peer is coming")
                self.peers[action['data']] = addr
                # print(addr)
                pu.sendJS(self.udp_socket, addr, {
                    "type": 'peers',
                    "data": self.peers
                })

            if action['type'] == 'peers':
                print("Received a bunch of peers")
                self.peers.update(action['data'])
                # introduce youself.
                pu.broadcastJS(self.udp_socket, {
                    "type": "introduce",
                    "data": self.myid
                }, self.peers)

            if action['type'] == 'introduce':
                print("Get a new friend.")
                self.peers[action['data']] = addr

            if action['type'] == 'input':
                print(action['data'])

            if action['type'] == 'exit':
                if (self.myid == action['data']):
                    #cannot be closed too fast.
                    time.sleep(0.5)
                    break
                    # self.udp_socket.close()
                value, key = self.peers.pop(action['data'])
                print(action['data'] + " is left.")
Ejemplo n.º 5
0
    def send(self):

        while 1:
            msg_input = input("$:")
            if msg_input == "exit":
                pu.broadcastJS(self.udp_socket, {
                    "type": "exit",
                    "data": self.myid
                }, self.peers)
                break
            if msg_input == "friends":
                print(self.peers)
                continue
            if msg_input == 'product':
                #TODO
                print('print product infomation here')
                newProduct = Product()
                newProduct.name = input('Your product name: ')
                newProduct.description = input(
                    'Please descript your product status:')
                newProduct.price = input('what is the price of the product:')
                newProduct.owner = self.myid
                newProduct.printInfo()
                smsg = newProduct.jsonFile()
                smsg.update({'type': 'newProduct'})
                print(smsg)
                pu.broadcastJS(self.udp_socket, smsg, self.peers)
                continue

            if msg_input == 'products info':
                for p_lst in self.product_lst:
                    print(p_lst)
                    for p in p_lst:
                        p.printInfo()
                continue

            l = msg_input.split()

            if l[-1] in self.peers.keys():
                toA = self.peers[l[-1]]
                s = ' '.join(l[:-1])
                pu.sendJS(self.udp_socket, toA, {"type": "input", "data": s})
            else:
                pu.broadcastJS(self.udp_socket, {
                    "type": "input",
                    "data": msg_input
                }, self.peers)
                continue
Ejemplo n.º 6
0
    def rece(self):
        while 1:
            data, addr = pu.recembase(self.udp_socket)
            action = json.loads(data)
            if action['type'] == 'newpeer':
                self.peers[action['data']]= addr
                print(self.peers)
                pu.sendJS(self.udp_socket, addr,{
                "type":'peers',
                "data":self.peers
                })         

            if action['type'] == 'newProduct':
                #print('Someone post a new product')
                newProduct = Product(action)
                newProduct.printInfo()
                if newProduct.name in self.product_lst:
                    self.product_lst[newProduct.name].append(newProduct)
                else:
                    self.product_lst[newProduct.name] =  [newProduct]
                #end if
                print('debug: sync value is:', action['sync'])
                if not action['sync']:
                    self.version+=1
            #end rec newProduct

            if action['type'] == 'remove':
                name = action['Name']
                uid = action['UID']
                # Assume the product in list
                if name in self.product_lst:
                    p_lst = self.product_lst[name]
                    for p in p_lst:
                        if p.uid == uid:
                            p_lst.remove(p)
                            #print('debug: remove signal, ack')
                            break
                        #end if
                    #end for
                else:
                    pass
                    #print('debug: there is no such a product')
                #end if
            #end if action

            if action['type'] == 'update': 
                name = action['Name']
                uid = action['UID']
                attr = action['Attribute']
                value = action['Value']
                # Assume the product in list
                if name in self.product_lst:
                    p_lst = self.product_lst[name]
                    for p in p_lst:
                        if p.uid == uid:
                            p.setAttr(attr,value)
                            #print('debug: update signal, ack')
                            break
                        #end if
                    #end for
                else:
                    pass
                    #print('debug: there is no such a product')
                #end if 
            #end if action

            if action['type'] == 'peers':
                self.peers.update(action['data'])
                pu.broadcastJS(self.udp_socket, {
                    "type":"introduce",
                    "data": self.myid,
                },self.peers)
                #   syncchronize dictionary
                for peer in self.peers:
                    dist = self.peers[peer]
                    pu.sendJS(self.udp_socket, dist, {
                        "type": "sync",
                        "version": self.version
                        })
                    break
                #end for
            #end rec peers

            if action['type'] == 'sync':
                print('debug: in sync')
                pversion = action['version']
                if pversion > self.version:
                    pu.sendJS(self.udp_socket, addr, {
                        "type": 'broadcast',
                        })
                else:
                    pu.sendJS(self.udp_socket, addr, {'type': 'reset'}) 
                    time.sleep(1)
                    for pn in self.product_lst:
                        p_lst = self.product_lst[pn]
                        for p in p_lst:
                            smsg = p.jsonFile()
                            smsg.update({'type': 'newProduct', 'sync': True})
                            pu.sendJS(self.udp_socket, addr, smsg) 
                        #end for
                    #end for
                    pu.sendJS(self.udp_socket, addr, {'type': 'syncVersion', 'version': self.version})
                #end if
            #end rec sync

            if action['type'] == 'syncVersion':
                self.version == action['version']
            #end rec syncVersion

            if action['type'] == 'broadcast':
                pu.broadcastJS(self.udp_socket, {'type': 'reset'}, self.peers)
                time.sleep(1)
                for pn in self.product_lst:
                    p_lst = self.product_lst[pn]
                    for p in p_lst:
                        smsg = p.jsonFile()
                        smsg.update({'type': 'newProduct', 'sync': True})
                        pu.broadcastJS(self.udp_socket, smsg, self.peers) 
                    #end for
                #end for
                pu.broadcastJS(self.udp_socket, {'type': 'syncVersion', 'version': self.version}, self.peers) 
            #end rec broadcast


            if action['type'] == 'reset':
                self.product_lst.clear()
            #end rec reset

            if action['type'] == 'introduce':
                self.peers[action['data']]= addr
            #end rec introduce

            if action['type'] == 'input':
                print(action['data'])  

            if action['type'] == 'exit':
                if(self.myid == action['data']):
                #cannot be closed too fast.  
                    time.sleep(0.5) 
                    break
                    # self.udp_socket.close()
                value, key = self.peers.pop(action['data'])
                print( action['data'] + " is left.") 
Ejemplo n.º 7
0
    def send(self):
        while 1: 
            msg_input = input("$:")
            msg_input = msg_input.strip()
            if msg_input == "exit":
                pu.broadcastJS(self.udp_socket, {
                    "type":"exit",
                    "data":self.myid
                },self.peers)
                self.save()
                break     


            if msg_input == "friends":
                print(self.peers) 
                continue      

            if msg_input == 'product':
                # implement create Product later
                #newProduct = self.createProduct()
                newProduct = Product()
                newProduct.name = input('Your product name: ')
                newProduct.description = input('Please descript your product status:')
                newProduct.price = input('what is the price of the product:')
                newProduct.owner = self.myid
                newProduct.uid = self.getUID()
                newProduct.printInfo()
                smsg = newProduct.jsonFile()
                smsg.update({'type': 'newProduct', 'sync': False})
                print('Product added')
                pu.broadcastJS(self.udp_socket, smsg, self.peers)
                continue

            if msg_input == 'save':
                self.save()
                continue

            if msg_input == 'products info':
                #   print format here, todo later
                self.printProductInfo()
                continue

            if msg_input == 'update':
                p, attr, value = self.update()
                if p != None:
                    #print('debug: send a update command to peers')
                    pu.broadcastJS(self.udp_socket, {
                        "type": "update",
                        "Name": p.name,
                        "UID": p.uid,
                        "Attribute": attr,
                        "Value": value}, self.peers)
                else:
                    pass
                    #print('debug: update canceled, nothing happend')
                #end if
                continue

            if msg_input == 'remove':
                p = self.remove()
                if p != None:
                    #print('debug: send a remove command to peers')
                    pu.broadcastJS(self.udp_socket, {
                            "type": "remove",
                            "Name": p.name,
                            "UID": p.uid}, self.peers)
                else:
                    pass
                    #print('debug: Remove canceled, nothing happened')
                continue

            if msg_input == 'search':
                self.search()
                continue

            if msg_input == 'guide':
                self.printGuide()
                continue


            l = msg_input.split()
            
            if l[-1] in self.peers.keys():
                toA = self.peers[l[-1]]
                s = ' '.join(l[:-1]) 
                pu.sendJS(self.udp_socket, toA,{
                    "type":"input",
                    "data":s
                })      
            else :
                pu.broadcastJS(self.udp_socket, {
                    "type":"input",
                    "data":msg_input
                },self.peers)
                continue 
        #end while
        if self.myid == 'helper':
            dist = self.udp_socket.getsockname()
            pu.sendJS(self.udp_socket, dist, {'type': 'exit', 'data': self.myid})
Ejemplo n.º 8
0
    def send(self):
        while 1:
            msg_input = input("$:")
            msg_input = msg_input.strip()
            if msg_input == "exit":
                if self.myid != 'helper':
                    self.save()
                pu.broadcastJS(self.udp_socket, {
                    "type": "exit",
                    "data": self.myid
                }, self.peers)
                break

            if msg_input == "friends":
                print(self.peers)
                continue

            if msg_input == 'product':
                # implement create Product later
                #newProduct = self.createProduct()
                newProduct = Product()
                newProduct.name = input('Your product name: ')
                newProduct.description = input(
                    'Please descript your product status: ')
                newProduct.price = float(
                    input('what is the price of the product: '))
                newProduct.phone = input(
                    'what is your phone number(enter None to skip this question): '
                )
                newProduct.email = input('what is your email address: ')
                newProduct.owner = self.myid
                newProduct.uid = self.getUID()
                newProduct.printInfo()
                smsg = newProduct.jsonFile()
                smsg.update({
                    'type': 'newProduct',
                    'sync': False,
                    'data': self.myid
                })
                # add to list
                if newProduct.name in self.product_lst:
                    self.product_lst[newProduct.name].append(newProduct)
                else:
                    self.product_lst[newProduct.name] = [newProduct]
                print('Product added')
                pu.broadcastJS(self.udp_socket, smsg, self.peers)
                self.version += 1
                continue

            if msg_input == 'save':
                self.save()
                continue

            if msg_input == 'products info':
                #   print format here, todo later
                self.printProductInfo()
                continue

            if msg_input == 'update':
                p, attr, value = self.update()
                if p != None:
                    print('Update successfully')
                    pu.broadcastJS(
                        self.udp_socket, {
                            "type": "update",
                            "data": self.myid,
                            "Name": p.name,
                            "UID": p.uid,
                            "Attribute": attr,
                            "Value": value
                        }, self.peers)

                    if attr == 'name':
                        p_lst = self.product_lst[p.name]
                        p_lst.remove(p)
                        if value in self.product_lst:
                            self.product_lst[value].append(p)
                        else:
                            self.product_lst[value] = [p]
                        #end if
                    #end if
                    p.setAttr(attr, value)
                    self.version += 1
                else:
                    pass
                    #print('debug: update canceled, nothing happend')
                #end if
                continue

            if msg_input == 'remove':
                p = self.remove()
                if p != None:
                    #print('debug: send a remove command to peers')
                    pu.broadcastJS(
                        self.udp_socket, {
                            "type": "remove",
                            "data": self.myid,
                            "Name": p.name,
                            "UID": p.uid
                        }, self.peers)
                else:
                    pass
                    #print('debug: Remove canceled, nothing happened')
                self.version += 1
                continue
            #end remove command

            if msg_input == 'search':
                self.search()
                continue

            if msg_input == 'guide':
                self.printGuide()
                continue

            l = msg_input.split()

            if l[-1] in self.peers.keys():
                toA = self.peers[l[-1]]
                s = ' '.join(l[:-1])
                pu.sendJS(self.udp_socket, toA, {"type": "input", "data": s})
            else:
                pu.broadcastJS(self.udp_socket, {
                    "type": "input",
                    "data": msg_input
                }, self.peers)
                continue
        #end while
        pu.sendJS(self.udp_socket, self.seed, {
            'type': 'exit',
            'data': self.myid
        })