Skip to content

Commit

Permalink
Merge pull request #176 from COS301-SE-2024/feature/mergeFixes
Browse files Browse the repository at this point in the history
Feature/merge fixes
  • Loading branch information
Donatello-Carboni authored Sep 28, 2024
2 parents eea91c0 + 0012c6b commit fbddea9
Show file tree
Hide file tree
Showing 33 changed files with 363 additions and 267 deletions.
Binary file removed Agent/API/Download/885184504/2.mp4
Binary file not shown.
24 changes: 13 additions & 11 deletions Agent/API/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
app = FastAPI()
RUN_ONCE_FILE = "run_once_flag.txt"

HOST_IP = os.getenv("HOST_IP")


@app.get("/")
def status():
Expand Down Expand Up @@ -57,21 +59,22 @@ def getHardwareInfo():

@app.on_event("startup")
async def startup_event():
if not os.path.exists(RUN_ONCE_FILE):
await install()
# TODO Run initial setup
with open(RUN_ONCE_FILE, "w") as file:
file.write("This file indicates the one-time function has run.")
else:
print("One-time setup function has already run, skipping.")
# if not os.path.exists(RUN_ONCE_FILE):
# await install()
# # TODO Run initial setup
# with open(RUN_ONCE_FILE, "w") as file:
# file.write("This file indicates the one-time function has run.")
# else:
# print("One-time setup function has already run, skipping.")
await install()


# TODO TEST
@app.get("/install")
async def install():
async with httpx.AsyncClient() as client:
# # get my agent details
# response = await client.get('http://127.0.0.1:8006/agent')
# response = await client.get('http://' + HOST_IP + ':8006/agent')
# if response.status_code != 200:
# raise HTTPException(status_code=response.status_code, detail="Error fetching external data")
# print("Response:", response.json())
Expand Down Expand Up @@ -103,7 +106,7 @@ async def install():

# Transmit the encrypted data
response2 = await client.post(
"http://127.0.0.1:8006/test",
"http://" + HOST_IP + ":8006/test",
json={"aid": os.getenv("AID"), "message": encrypted_message},
)
if response2.status_code != 200:
Expand Down Expand Up @@ -142,7 +145,7 @@ async def install():
),
)
response3 = await client.post(
"http://127.0.0.1:8006/handshake",
"http://" + HOST_IP + ":8006/handshake",
json={"aid": os.getenv("AID"), "corporation": os.getenv("CORPORATION_NAME"), "message": message},
)
if response3.status_code != 200:
Expand All @@ -156,7 +159,6 @@ async def install():

def findOpenPort():
port = 8002
# ip = "127.0.0.1"
ip = socket.gethostbyname(socket.gethostname())
while True:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
Expand Down
7 changes: 6 additions & 1 deletion Agent/API/broker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from fastapi import FastAPI, BackgroundTasks, Request
import requests
import uvicorn
import os
from dotenv import load_dotenv
load_dotenv()

HOST_IP = os.getenv("HOST_IP")

app = FastAPI()

Expand All @@ -14,7 +19,7 @@ def getAgentIp():
# Here we would use a database lookup to get the IP
# Then get the port from a request
if(checkVerified()):
url = "http://127.0.0.1:8001/startup/"
url = "http://" + HOST_IP + ":8001/startup/"
print("Sending request to: ", url)
response = requests.get(url)

Expand Down
80 changes: 0 additions & 80 deletions Agent/API/clientTest.py

This file was deleted.

3 changes: 1 addition & 2 deletions Backend/Django/Django/Django/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['localhost']

ALLOWED_HOSTS = ['localhost', '206.189.188.197']

# Application definition

Expand Down
4 changes: 1 addition & 3 deletions Backend/Django/Django/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['localhost']
ALLOWED_HOSTS = []

ALLOWED_HOSTS = ['localhost', '206.189.188.197']

# Application definition

Expand Down
6 changes: 4 additions & 2 deletions Backend/Django/Django/testApp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

load_dotenv()

HOST_IP = os.getenv("HOST_IP")


class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
Expand Down Expand Up @@ -758,7 +760,7 @@ def uploadFile(request):

print(message)

url = "http://localhost:8006/brokerStore"
url = "http://" + HOST_IP + ":8006/brokerStore"
response = requests.post(url, json=message)
data = response.json()
print(response.status_code)
Expand Down Expand Up @@ -1021,7 +1023,7 @@ def send_invite(teamName, email, token):
from_email = "bitforge.capstone@gmail.com"
from_password = os.getenv("APP_PASSWORD")
subject = "Join our team!"
body = f"Hello, you have been invited to join {teamName} on our platform. Please sign up and join us! \n\n Download the app here: http://localhost:8000/download \n\n Use the following token to join the team: {token}"
body = f"Hello, you have been invited to join {teamName} on our platform. Please sign up and join us! \n\n Download the app here: http://" + HOST_IP + ":8000/download \n\n Use the following token to join the team: {token}"
message = MIMEMultipart()
message["From"] = from_email
message["To"] = email
Expand Down
15 changes: 12 additions & 3 deletions Backend/Django/testApp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

load_dotenv()

HOST_IP = os.getenv("HOST_IP")


class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
Expand Down Expand Up @@ -752,13 +754,20 @@ def uploadFile(request):

if dataToken.token != utoken:
return Response({'error': 'Invalid token'}, status=status.HTTP_400_BAD_REQUEST)

user = User.objects.get(uid=uid)
cid = user.cid.cname

# corporation = Corporation.objects.get(cid=cid)

print("Corporation: ", cid)

size = data.get("size")
message = {"size": size, "uid": uid, "utoken": utoken}
message = {"size": size, "uid": uid, "utoken": utoken, "corporation": cid}

print(message)

url = "http://localhost:8006/brokerStore"
url = "http://" + HOST_IP + ":8006/brokerStore"
response = requests.post(url, json=message)
data = response.json()
print(response.status_code)
Expand Down Expand Up @@ -1021,7 +1030,7 @@ def send_invite(teamName, email, token):
from_email = "bitforge.capstone@gmail.com"
from_password = os.getenv("APP_PASSWORD")
subject = "Join our team!"
body = f"Hello, you have been invited to join {teamName} on our platform. Please sign up and join us! \n\n Download the app here: http://localhost:8000/download \n\n Use the following token to join the team: {token}"
body = f"Hello, you have been invited to join {teamName} on our platform. Please sign up and join us! \n\n Download the app here: http://" + HOST_IP + ":8000/download \n\n Use the following token to join the team: {token}"
message = MIMEMultipart()
message["From"] = from_email
message["To"] = email
Expand Down
20 changes: 13 additions & 7 deletions Broker/API/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@
import charon
import media
import base64
import os
from dotenv import load_dotenv

load_dotenv()

app = FastAPI()

HOST_IP = os.getenv("HOST_IP")


@app.get("/")
def status():
Expand Down Expand Up @@ -48,7 +54,7 @@ async def simStore():
avail = agent
break
if avail is None:
avail = {"aid": 28, "aport": 8001, "aip": "127.0.0.1"}
avail = {"aid": 28, "aport": 8001, "aip": HOST_IP}

if not await charon.ping(avail["aip"], avail["aport"]):
return {"status": "no agents available"}
Expand All @@ -74,16 +80,16 @@ def agent():
public = response["public"]

public = base64.b64encode(public).decode("utf-8")

# make env file
with open("./package/.env", "w") as f:
f.write(f"AID={aid}\n")
f.write(f"PUBLIC_TEST={public}")
f.write(f"PUBLIC_TEST={public}\n")
f.write(f"HOST_IP=206.189.188.197")

# subprocess.run(["makensis", "./package/setup.nsi"])
subprocess.run(["makensis", "./package/setup.nsi"])

filePath = "./package/MyFastAPIAppSetup.exe"
return FileResponse(filePath, filename="MyFastAPIAppSetup.exe")
return FileResponse(filePath, filename="AgentSetup.exe")

@app.get("/windows")
def windows():
Expand Down Expand Up @@ -144,15 +150,15 @@ async def brokerStore(request: Request):
message = await request.json()
print(">>>BrokerStore initiated", message)
print("Getting available agents")
agents = media.get_avail_store_agents(message['size'])
agents = media.get_avail_store_agents(message['size'], message['corporation'])
print("avail agents: ", agents)
avail = None
for agent in agents:
if await charon.ping(agent['aip'], agent['aport']):
avail = agent
break
if avail is None:
avail = {"aid": 1, "aport": 8001, "aip": "127.0.0.1"}
avail = {"aid": 1, "aport": 8001, "aip": HOST_IP, "corporation": "dev"}

if not await charon.ping(avail["aip"], avail["aport"]):
return {"status": "no agents available"}
Expand Down
8 changes: 4 additions & 4 deletions Broker/API/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,16 @@ def get_agent_details_from_media(mid):
return result


def get_avail_store_agents(storage_capacity):
def get_avail_store_agents(storage_capacity, corporation):
conn = get_connection()
cursor = conn.cursor()
query = """
SELECT aid, aport, aip
SELECT aid, aport, aip, corporation
FROM agentstore
WHERE verified = FALSE AND (capacity = 'dual' OR capacity = 'store')
AND storage >= %s
AND storage >= %s AND corporation = %s
"""
cursor.execute(query, (storage_capacity,))
cursor.execute(query, (storage_capacity, corporation))
rows = cursor.fetchall()
column_names = [desc[0] for desc in cursor.description]

Expand Down
Binary file modified Broker/API/package/MyFastAPIAppSetup.exe
Binary file not shown.
Loading

0 comments on commit fbddea9

Please sign in to comment.