def confirm_transaction(params): log.write(request.method+' on /confirmtrans/'+params+"\n") log.flush() event = params.split(':')[0] transid = int(params.split(':')[1]) ret = database.start_voting(event,transid,g) log.write('Confirming transaction start voting returns :'+ ret+'\n') log.flush() room.release() return ret
def upload_image(event): ''' uploading a picture ''' if event != 'new' and request.method == 'POST': log.write('Uploading a picture The url to redirect to is:'+event+"\n") log.write('Request:'+str(request)+'\n') log.flush() file = request.files['capture'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) if database.isfileuploaded(file.filename,event,g) == True: log.write('Avoiding reupload'+"\n") log.flush() return redirect('/event/'+event) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) ''' add user to event only when he uploads''' if('user' in request.cookies): log.write('Cookie found in user ' + request.cookies.get('user')+"\n") log.flush() if(database.user_not_in_event(event,request.cookies.get('user'),g)): log.write("Cookie found in user but user not in event adding: " + getip(request)+"\n") log.flush() database.add_user_inevent(event,request.cookies.get('user'),g) ''' add new event to database ''' log.write('Adding new image ' + filename + ' into database\n') log.flush() database.insert_new_image(event,filename,-1,g) ''' send the image to the master and get the new transaction ID then update the TID ''' master = database.get_event_master(event,g) transid = servcomm.get_new_transid(event,master) if transid == -1: log.write('Cannot proceed with upload event closed\n') log.flush() return redirect('/event/'+event) database.update_images_transid(event,filename,transid,g) ''' Send image to remaining servers ''' ret = servcomm.send_image_to_servers(event,SERVER2,SERVER3,transid,filename) if ret != 'success': log.write('Upload is not accepted everywhere abort ret: '+ret+'\n') log.flush() return redirect('/event/'+event) ''' Send message to all 3 servers to confirm the TID in events table ''' ''' Also confirm the image linked with the ttransaction to be made permanent ''' ''' This also nullifies all previous votes and starts a new voting ''' ''' check if event is still open before confirming transaction ''' ret = servcomm.confirm_transaction(event,SERVER2,SERVER3,transid) if ret != 'success': log.write('Upload could not be confirmed so abort\n') log.flush() return redirect('/event/'+ event) ret = database.start_voting(event,transid,g) ''' nullify all the participants votes and then start a new timer by checking the prev ious timers number and incrementing it''' #transaction = database.start_voting(event,g) log.write("Start voting returned:%s timer started...\n"%(ret)) log.flush() # start a new timer for end of voting period if ret == 0: log.write("Event published cannot upload any more pictures\n") log.flush() return redirect('/event'+event) else: log.write("Start timer here\n") log.flush() threading.Timer(10,functools.partial(servcomm.timeout,SERVER1,event+":"+str(transid))).start() #subprocess.Popen(["python", "callee.py",event,str(timer)]) # wake everyone up and reload images in everyone. log.write("releasing all events now on picture upload :"+request.cookies.get('user')+": " + event) log.flush() room.release() else: log.write("File not accepted by server name:" + file.filename) log.flush() return redirect('/event/'+event)