└── pysync.py /pysync.py: -------------------------------------------------------------------------------- 1 | #simple Sync System 2 | 3 | #current features 4 | #have a record of when you last synced with each contact. 5 | #When connecting with a contact, get them up to date with items since. 6 | 7 | #future features 8 | #each data item has a record of who you *first* recieved it from, and when.#not implemented 9 | #check with contact before sending data item by sending hash (if item is large) 10 | 11 | items_by_recv_date = [] 12 | #item_from = {} 13 | items = {} 14 | contacts = {} 15 | 16 | groups = {} 17 | topics = {} 18 | 19 | ownid = 123 20 | import time, random 21 | 22 | contacts[ownid] = {'last_sync':0} 23 | 24 | #Dummy function that should use a backend like RS or XMPP 25 | def send_item(contact, item): 26 | print contact 27 | print item 28 | print "not sending" + str(item) + " to " + str(contact) 29 | 30 | #get contact up to date with own data 31 | def sync_to_contact(cId): 32 | contact = contacts[cId] 33 | ls = contact['last_sync'] 34 | #now =int(time.time()) 35 | for i in items_by_recv_date[ls:]: 36 | can_send = True 37 | if 'group' in item and cId not in groups[item['group']]: 38 | can_send = False 39 | if cId not in topics[item['topic']]: 40 | can_send = False 41 | 42 | if can_send: 43 | send_item(cId, i) 44 | #should confirm contact has got data, then: 45 | contact['last_sync'] = len(items_by_recv_date) 46 | 47 | #update all contacts 48 | def sync_to_contacts(): 49 | for c in contacts: 50 | sync_to_contact(c) 51 | 52 | #create new data 53 | def publish_item(item): 54 | #now =time.time() 55 | #items_by_recv_date[now]=item 56 | #items[item.hash] = item 57 | item['hash'] = hash(str(item)) 58 | on_recv_item(ownid, item) 59 | 60 | #ask contact to update you 61 | def request_sync(cId): 62 | item = {'type':'hello'} 63 | send_item(cId, item) 64 | 65 | #triggered on program load 66 | def onload(): 67 | for c in contacts: 68 | request_sync(c) 69 | sync_to_contacts() 70 | 71 | 72 | #triggered by data arriving from function send_item over network 73 | def on_recv_item(cId, item): 74 | now =time.time() 75 | contact = contacts[cId] 76 | if item['hash'] not in items: 77 | itemtype = item['type'] 78 | if itemtype=='hello' and cId != ownid: 79 | on_request_sync(contact) 80 | return 81 | 82 | #store item 83 | items_by_recv_date.append(item) 84 | #items_from[now]=cId 85 | items[item['hash']] = item 86 | 87 | if itemtype=='message': 88 | update_display(item) 89 | 90 | #for changing status relating to topic/group 91 | #could run through history, and remove old status 92 | if itemtype=='leaveGroup': 93 | del groups[item['groupid']][cId] 94 | if itemtype=='joinGroup': 95 | print "---" 96 | print groups[item['groupid']] 97 | groups[item['groupid']][cId]=contact 98 | if itemtype=='leaveTopic': 99 | del topics[item['topicId']][cId] 100 | if itemtype=='joinTopic': 101 | tId = item['topicId'] 102 | print "---" 103 | print topics 104 | print tId 105 | if tId not in topics: 106 | topics[tId] = {} 107 | topics[tId][cId]=contact 108 | 109 | #this should update a GUI 110 | def update_display(item): 111 | print "GOT ITEM" 112 | print item 113 | 114 | #normally triggerd by a contact coming online 115 | def on_request_sync(c): 116 | if c in contacts: 117 | sync_to_contact[c] 118 | 119 | 120 | 121 | #high level actions 122 | def submit_post(text, topic, group=None, parent=None): 123 | item={'type':'message', 'text':text,topic:topic, group:group, parent:parent} 124 | publish_item(item) 125 | 126 | 127 | def submit_vote(text, topic, group=None, parent=None): 128 | item={'type':'vote', 'vote':vote,topic:topic, group:group, parent:parent} 129 | publish_item(item) 130 | 131 | def join_group(group): 132 | item={'type':'joinGroup', 'groupid':group} 133 | publish_item(item) 134 | 135 | def leave_group(group): 136 | item={'type':'leaveGroup', 'groupid':group} 137 | publish_item(item) 138 | 139 | def join_topic(topic): 140 | item={'type':'joinTopic', 'topicId':topic} 141 | print item 142 | publish_item(item) 143 | 144 | def leave_topic(topic): 145 | item={'type':'leaveTopic', 'topicId':topic} 146 | publish_item(item) 147 | 148 | 149 | def main(): 150 | onload() 151 | 152 | main() 153 | 154 | ttopic = 1 155 | join_topic(ttopic) 156 | 157 | submit_post("HI", ttopic) --------------------------------------------------------------------------------