Skip to content

Commit

Permalink
oh lol
Browse files Browse the repository at this point in the history
  • Loading branch information
JDJGInc committed Apr 25, 2024
1 parent e6fe005 commit 4e29e60
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import aiohttp
import asyncpg
import discord
from dotenv import load_dotenv
import uvicorn
from dotenv import load_dotenv
from fastapi import FastAPI, Response
from fastapi.responses import HTMLResponse, ORJSONResponse, PlainTextResponse

Expand Down
2 changes: 1 addition & 1 deletion utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .extra import RedirectEnum, stats_builder
from .server import handle_basic_response
from .views import BasicButtons
from .zmq_utils import server_url, bot_url # plaeholder for now proper methods soon.
from .zmq_utils import bot_url, server_url # plaeholder for now proper methods soon.

11 comments on commit 4e29e60

@JDJGInc
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given your requirements, you want the server to both send data periodically in a structured format (e.g., JSON) and respond to specific requests for additional data about a particular user ID. The bot will listen for incoming data and can request further information by sending a user ID to the server. Here's an update to the code with these details incorporated:

  1. Server Sender will send structured JSON data and also listen for and respond to requests from the bot.
  2. Bot Receiver will parse incoming JSON messages and send requests when it requires more data on a particular user ID.
  3. Data Handling should include JSON serialization and deserialization for communication.

Let's update the script with this functionality:

import asyncio
import time
import json

import zmq
from zmq.asyncio import Context, Poller

server_url = 'tcp://127.0.0.1:5555'
bot_url = 'tcp://127.0.0.1:5554'

ctx = Context.instance()

async def ping() -> None:
    """Print dots to indicate idleness"""
    while True:
        await asyncio.sleep(0.5)
        print('.')

async def bot_receiver() -> None:
    """Bot receives messages on port 5554 and processes them"""
    pull = ctx.socket(zmq.PULL)
    pull.connect(bot_url)
    poller = Poller()
    poller.register(pull, zmq.POLLIN)
    
    while True:
        events = await poller.poll()
        if pull in dict(events):
            msg = await pull.recv_string()
            data = json.loads(msg)
            print('Bot received:', data)
            # Process the data, decide if more info is needed
            for user_id, info in data.items():
                if need_more_info(user_id, info):  # Define this function based on your criteria
                    await request_data(user_id)

async def request_data(user_id: str):
    """Send a request for data to the server if more info is needed for user_id"""
    push = ctx.socket(zmq.PUSH)
    push.connect(server_url)
    await push.send_string(user_id)
    push.close()

async def server_sender() -> None:
    """Server sends data to port 5555, but also listens for requests from the bot"""
    push = ctx.socket(zmq.PUSH)
    push.bind(server_url)
    pull = ctx.socket(zmq.PULL)
    pull.bind(bot_url)  # Server also listens for data requests from the bot

    poller = Poller()
    poller.register(pull, zmq.POLLIN)

    while True:
        # Send periodic updates
        await asyncio.sleep(1)
        data = json.dumps({"212020258402205697": "data"})
        print("Server sending data")
        await push.send_string(data)

        # Check for incoming requests
        events = await poller.poll(timeout=0)  # Non-blocking
        if pull in dict(events):
            user_id = await pull.recv_string()
            await handle_request(user_id, push)

async def handle_request(user_id: str, push):
    """Handle requests received from the bot and send back specific data"""
    print(f"Server received a request for more info on {user_id}")
    detailed_info = json.dumps({user_id: "detailed info"})  # Fetch or generate the detailed info
    await push.send_string(detailed_info)

def need_more_info(user_id, info):
    """Decide if more info is needed for user_id based on the received info"""
    # Implement logic to determine if more info is needed
    return True  # Example: always true for demonstration

asyncio.run(
    asyncio.wait(
        [
            ping(),
            bot_receiver(),
            server_sender(),
        ]
    )
)

Key Modifications:

  • JSON Data: The server sends and receives data as JSON strings. Ensure that the data structure is correctly serialized and deserialized.
  • Handling Requests: When a request for a user ID is received, the server looks up additional details and sends them back.
  • Data Processing: The bot processes received data and requests additional info based on your defined logic.

Make sure to fill in the need_more_info() function based on what actually determines the necessity for additional data in your application. Adjust data handling to your specific data format and business logic.

(here's chatgpt's response it was weird using it)

@JDJGInc
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the bot listens for incoming data from local port 5554
server sends new data to it(only on api endpoint)

the server listens for connections from port 5555
bot asks for data it needs if the user_id is not found
does this makes sense?
I am unsure how to use pyzmq for that tho

(this is the main thing)

@JDJGInc
Copy link
Member Author

@JDJGInc JDJGInc commented on 4e29e60 Apr 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bot listener -> receives new data for a user

import json
json.dumps({"user_id" : "168422909482762240", "data" :data})

It properly json.dumps the data

Bot receiver ->
Recives the dump and puts into bot.guild_data (should call that oauth data actually)

fixed (guild_data (weird name) to oauth_data):
872c13a

Server receiver:
Recives fetch request to get data(will see if it has the user id if so returns yes)

if not it returns a failed dictionary

also server will have a delete method to delete data if the user runs /clear-data

@app_commands.command(description="Clears data", name="clear-data")

(this should be enough)

@JDJGInc
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The chatgpt thing explains it fairly well though

@JDJGInc
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Earlier chatgpt:

The provided example script creates an asynchronous ZeroMQ setup with PULL and PUSH sockets communicating over TCP port 5555. The script demonstrates an infinite loop where one coroutine (sender) sends timestamps to another coroutine (receiver) over a local connection.

To adapt this example to your specified requirements:

  1. Bot Listens on Port 5554: Modify the receiver coroutine to listen on port 5554 for incoming data.
  2. Server Sends to Port 5555: Keep the sender coroutine as is, with the server sending on port 5555.
  3. Bot-Specific Logic: Add logic to the receiver to request data if a specific user_id is not found.

Here’s how you can modify your existing script to fit these requirements:

import asyncio
import time

import zmq
from zmq.asyncio import Context, Poller

# Different ports for different tasks
server_url = 'tcp://127.0.0.1:5555'
bot_url = 'tcp://127.0.0.1:5554'

ctx = Context.instance()

async def ping() -> None:
    """Print dots to indicate idleness"""
    while True:
        await asyncio.sleep(0.5)
        print('.')

async def bot_receiver() -> None:
    """Bot receives messages on port 5554 and processes them"""
    pull = ctx.socket(zmq.PULL)
    pull.connect(bot_url)
    poller = Poller()
    poller.register(pull, zmq.POLLIN)
    
    while True:
        events = await poller.poll()
        if pull in dict(events):
            msg = await pull.recv_multipart()
            user_id = msg[0].decode('ascii')
            print('Bot received:', user_id)
            # Example logic: Check if user_id is found, if not, request from server
            if user_id not in user_database:
                await request_data(user_id)

async def request_data(user_id: str):
    """Send a request for data to the server if user_id not found"""
    push = ctx.socket(zmq.PUSH)
    push.connect(server_url)
    await push.send_multipart([user_id.encode('ascii')])
    push.close()

async def server_sender() -> None:
    """Server sends data to port 5555, but also listens for requests from the bot"""
    push = ctx.socket(zmq.PUSH)
    push.bind(server_url)
    pull = ctx.socket(zmq.PULL)
    pull.bind(bot_url)  # Server also listens for data requests from the bot

    poller = Poller()
    poller.register(pull, zmq.POLLIN)

    while True:
        # Send periodic updates
        await asyncio.sleep(1)
        print("Server sending")
        await push.send_multipart([str(time.time()).encode('ascii')])

        # Check for incoming requests
        events = await poller.poll(timeout=0)  # Non-blocking
        if pull in dict(events):
            request = await pull.recv_multipart()
            handle_request(request)

def handle_request(request):
    """Handle requests received from the bot"""
    user_id = request[0].decode('ascii')
    print(f"Server received a request for {user_id}")
    # Here, implement the logic to process the request and send back data

asyncio.run(
    asyncio.wait(
        [
            ping(),
            bot_receiver(),
            server_sender(),
        ]
    )
)

In this example, the server_sender function acts as the server sending regular updates via PUSH and listening for requests via PULL. The bot_receiver listens on port 5554 for incoming data and requests more data if necessary. Adjust the handle_request and request_data functions to implement your specific logic regarding what data needs to be sent based on requests and user ID checks.

Make sure that the overall architecture and flow align with your actual network conditions, data formats, and use case requirements.

@JDJGInc
Copy link
Member Author

@JDJGInc JDJGInc commented on 4e29e60 Apr 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vaccum_man

Here's the vaccum man (very spooky)

@JDJGInc
Copy link
Member Author

@JDJGInc JDJGInc commented on 4e29e60 Apr 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

app and bot must be in specific methods for proper lookups.

app.state.oauth_data -> server stuff
bot.oauth_data -> bot stuff

@JDJGInc
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:)

@JDJGInc
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:)

@JDJGInc
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JDJGInc
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also do not trust chatgpt but here it is providing a thing.

Please sign in to comment.