Distributed and decentralized pi calculation

Compare changes

Choose any two refs to compare.

Changed files
+65 -29
.github
workflows
src
client
server
+17
.github/workflows/tngl.yml
··· 1 + name: Mirroring 2 + 3 + on: [push, delete] 4 + 5 + jobs: 6 + to_tangled: 7 + runs-on: ubuntu-latest 8 + steps: # <-- must use actions/checkout before mirroring! 9 + - uses: actions/checkout@v3 10 + with: 11 + fetch-depth: 0 12 + - uses: pixta-dev/repository-mirroring-action@v1 13 + with: 14 + target_repo_url: 15 + git@tangled.sh:wish.freakybob.site/pistributed 16 + ssh_private_key: # <-- use 'secrets' to pass credential information. 17 + ${{ secrets.TANGLED_SSH }}
+6 -2
README.md
··· 1 1 # Pistributed 2 - > [!WARNING] 3 - > 1.1 -> ver.txt will no longer be included in the migration guide after 1.4! 2 + > [!NOTE] 3 + > Python 3.14 has released! 🥳 Pistributed is untested on 3.14 at the moment (ironically). <3 4 4 5 5 Distributed and decentralized pi calculation 6 + 7 + Tangled users; get Pistributed's client/server from GitHub here: https://github.com/wish13yt/pistributed/releases 8 + 9 + Website: https://wish13yt.github.io/pistributed/ 6 10 # Guide 7 11 Download the server from the [latest release](https://github.com/wish13yt/pistributed/releases/latest). 8 12
+24 -19
src/client/client.py
··· 4 4 import threading 5 5 import requests 6 6 import sys 7 + import urllib.request 7 8 8 9 print("Pistributed, an app to calculate Pi with distributed power.") 9 10 print("Do not run on systems owned by another entity, entities, person, or people, without explicit permission from them.") ··· 19 20 server = input("What is the server URL that you'd like to connect to? (ex: https://example.com, http://localhost:5000) ") 20 21 print("Alright! Thanks. Let the calculating begin :D (to exit, run ctrl+c or close terminal)") 21 22 print("-------------------") 23 + 22 24 global pi_val 23 25 pi_val = None 26 + 27 + for line in urllib.request.urlopen(server + "/api/getmultiplier"): 28 + multiplier = int(line.decode('utf-8')) 29 + print("Multiplier from server is " + str(multiplier)) 30 + 24 31 try: 25 32 def calc(n, prec): 26 33 getcontext().prec = prec ··· 32 39 deno = factorial(3*k) * (factorial(k)**3) * (640320**(3*k)) 33 40 pi += Decimal(t) / Decimal(deno) 34 41 pi = pi * Decimal(12) / Decimal(640320**Decimal(1.5)) 35 - pi = 1/pi 42 + pi = 1/pi * multiplier 36 43 return pi 37 44 38 45 def upload(): 46 + time.sleep(1) 39 47 while True: 40 - if str(pi_val).startswith("3.14") == True: 41 - print(">> checking and uploading latest calculation") 42 - response = requests.get(server + "/api/getpi") 48 + print(">> checking and uploading latest calculation") 49 + response = requests.get(server + "/api/getpi") 50 + print(response.text) 51 + replen = len(response.text) 52 + pilen = len(str(pi_val)) 53 + if response.text == "No clients have connected yet. Become one of the first!": 54 + print("server pi doesn't exist! uploading") 55 + data = {'pi': str(pi_val)} 56 + print(data) 57 + response = requests.post(server + "/api/postpi", data=data) 43 58 print(response.text) 44 - replen = len(response.text) 45 - pilen = len(str(pi_val)) 46 - if response.text == "No clients have connected yet. Become one of the first!": 47 - print("server pi doesn't exist! uploading") 59 + else: 60 + if replen > pilen: 61 + print("server pi length is greater than local pi length! not uploading") 62 + elif pilen > replen: 63 + print("local pi length longer than server pi length! uploading") 48 64 data = {'pi': str(pi_val)} 49 - print(data) 50 65 response = requests.post(server + "/api/postpi", data=data) 51 66 print(response.text) 52 - else: 53 - if replen > pilen: 54 - print("server pi length is greater than local pi length! not uploading") 55 - elif pilen > replen: 56 - print("local pi length longer than server pi length! uploading") 57 - data = {'pi': str(pi_val)} 58 - response = requests.post(server + "/api/postpi", data=data) 59 - print(response.text) 60 - else: 61 - print("pi_val not yet defined!") 62 67 time.sleep(60) 63 68 64 69 threading.Thread(target=upload, daemon=True).start()
+14 -6
src/server/app.py
··· 8 8 load_dotenv() 9 9 name = os.getenv("NAME") 10 10 email = os.getenv("EMAIL") 11 + multi = int(os.getenv("MULTI")) 11 12 atEmail = email.replace("@", " at ") 12 13 safeEmail = atEmail.replace(".", " dot ") 13 14 @app.route('/api/getpi') ··· 24 25 @app.route('/api/postpi', methods=['POST', 'GET']) 25 26 def postpi(): 26 27 f = open("picalc/pi.txt", "r") 28 + checkCorrectly = 3.14 * multi 27 29 if request.form['pi']: 28 30 if f.read() == "No clients have connected yet. Become one of the first!": 29 - if request.form['pi'].startswith("3.14") == True: 31 + if request.form['pi'].startswith(str(checkCorrectly)) == True: 30 32 with open("picalc/pi.txt", "w") as f: 31 33 f.write(request.form['pi']) 32 34 else: ··· 56 58 pilen = "Current Pi length couldn't be calculated!" 57 59 piresult = "Current Pi count is " + piresult 58 60 except: 59 - piresult = "picalc/pi.txt wasn't found on this web server. Sorry!" 61 + piresult = "picalc/pi.txt wasn't found on this server! It will be created now to fix this issue." 62 + try: 63 + with open("picalc/pi.txt", "w") as f: 64 + f.write("No clients have connected yet. Become one of the first!") 65 + except: 66 + piresult = f"Couldn't create pi.txt! Please contact {name} at {safeEmail} to fix this!" 60 67 if request.method == 'GET': 61 68 ver = open("ver.txt", "r") 62 69 version = ver.read() 63 - try: 64 - return render_template('index.html', pi=piresult, name=name, email=safeEmail, version=version, pilen=str(pilen)) 65 - except: 66 - return render_template('index.html', pi=piresult, name=name, email=safeEmail, version=version) 70 + return render_template('index.html', pi=piresult, name=name, email=safeEmail, version=version, pilen="Current Pi length is " + str(pilength), multi=str(multi)) 67 71 else: 68 72 return "Please use GET to access this page." 69 73 ··· 93 97 @app.route('/api/getemail') 94 98 def getemail(): 95 99 return safeEmail 100 + 101 + @app.route('/api/getmultiplier') 102 + def getMultiply(): 103 + return str(multi) 96 104 97 105 @app.errorhandler(404) 98 106 def page_not_found(error):
+3 -2
src/server/setup.py
··· 12 12 input("I agree, and would like to continue. (to continue, press enter)") 13 13 email = input("What email would you like to display for server support? THIS WILL BE MADE PUBLIC ") 14 14 called = input("What would you like to be called? (ex: Wish) THIS WILL BE MADE PUBLIC ") 15 + multi = input("What would you like Pi to be multiplied by? (number ONLY, default is 1)") or 1 15 16 print("Thank you! Creating a .env file... Make sure to run server.py when you are ready!") 16 17 with open(".env", "w") as file: 17 - file.write("EMAIL=" + email + "\nNAME=" + called) 18 + file.write("EMAIL=" + email + "\nNAME=" + called + "\nMULTI=" + multi) 18 19 isExist = os.path.exists("picalc") 19 20 if not isExist: 20 21 os.makedirs("picalc") ··· 23 24 pifile.write("No clients have connected yet. Become one of the first!") 24 25 print("Created picalc/pi.txt successfully. Do NOT edit this file!") 25 26 with open("ver.txt", "w") as versionfile: 26 - versionfile.write("1.4") 27 + versionfile.write("1.7") 27 28 print("Created version file. Server will not work without this!!!")
+1
src/server/templates/index.html
··· 24 24 <body> 25 25 <h1>Pistrubuted {{ version }}</h1> 26 26 <p>Pistrubuted is an open-source server & client to calculate Pi with distrubuted machines. Get started by downloading the client and connecting to this server.</p> 27 + <p>Pi is multiplied by {{ multi }}</p> 27 28 <p>{{ pi }}</p> 28 29 <p>{{ pilen }}</p> 29 30 <hr>