File size: 5,290 Bytes
b00c961
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import http.server
import socketserver
import threading
import json
import time
import urllib.request
import urllib.parse
import sys

# --- CONFIG ---
ROUTER_PORT = 8010
SHARD_PORTS = [8011, 8012, 8013]
TOPICS = ["Science", "History", "Coding"]

# --- MOCK KNOWLEDGE ---
KNOWLEDGE = {
    "Science": "The speed of light is 299,792,458 m/s.",
    "History": "Rome fell in 476 AD.",
    "Coding": "Python uses indentation."
}

# --- REUSABLE SERVER ---
class ReuseTCPServer(socketserver.TCPServer):
    allow_reuse_address = True

# --- SHARD SERVER ---
class ShardHandler(http.server.BaseHTTPRequestHandler):
    def do_POST(self):
        content_len = int(self.headers.get('Content-Length'))
        body = json.loads(self.rfile.read(content_len))
        query = body["query_text"]
        
        my_topic = self.server.topic
        
        score = 0.0
        if my_topic.lower() in query.lower():
            score = 1.0
        elif "light" in query.lower() and my_topic == "Science": score = 0.9 
        elif "rome" in query.lower() and my_topic == "History": score = 0.9
        elif "python" in query.lower() and my_topic == "Coding": score = 0.9
            
        response = {
            "shard_id": f"Shard_{my_topic}",
            "best_text": KNOWLEDGE[my_topic],
            "score": score
        }
        
        self.send_response(200)
        self.end_headers()
        self.wfile.write(json.dumps(response).encode())
    
    def log_message(self, format, *args): return # Silence logs

def run_shard(port, topic):
    print(f"🐝 [Shard] Starting {topic} on port {port}...")
    server = ReuseTCPServer(("localhost", port), ShardHandler)
    server.topic = topic
    server.serve_forever()

# --- ROUTER SERVER ---
class RouterHandler(http.server.BaseHTTPRequestHandler):
    def do_POST(self):
        if self.path != "/v1/chat/completions":
            self.send_error(404)
            return

        content_len = int(self.headers.get('Content-Length'))
        body = json.loads(self.rfile.read(content_len))
        query = body["messages"][-1]["content"]
        
        print(f"πŸ‘‘ [Router] Broadcasting: '{query}'")
        
        # Broadcast to Shards
        results = []
        for port in SHARD_PORTS:
            try:
                req = urllib.request.Request(
                    f"http://localhost:{port}", 
                    data=json.dumps({"query_text": query}).encode(),
                    headers={'Content-Type': 'application/json'}
                )
                with urllib.request.urlopen(req) as f:
                    results.append(json.loads(f.read().decode()))
            except:
                pass
        
        # Consensus
        if not results:
            self.send_error(500, "No shards reachable")
            return

        winner = max(results, key=lambda x: x["score"])
        
        # Response
        resp = {
            "choices": [{
                "message": {
                    "role": "assistant", 
                    "content": f"[Truth from {winner['shard_id']}] {winner['best_text']}"
                }
            }]
        }
        
        self.send_response(200)
        self.end_headers()
        self.wfile.write(json.dumps(resp).encode())

    def log_message(self, format, *args): return # Silence logs

def run_router():
    print(f"πŸ‘‘ [Router] Starting on port {ROUTER_PORT}...")
    server = ReuseTCPServer(("localhost", ROUTER_PORT), RouterHandler)
    server.serve_forever()

# --- MAIN ---
if __name__ == "__main__":
    threads = []
    
    # Start Shards
    for i, port in enumerate(SHARD_PORTS):
        t = threading.Thread(target=run_shard, args=(port, TOPICS[i]), daemon=True)
        t.start()
        threads.append(t)
        
    # Start Router
    t = threading.Thread(target=run_router, daemon=True)
    t.start()
    threads.append(t)
    
    time.sleep(2) # Warmup
    
    print("\n⚑ STARTING INTEGRATION TEST (Native HTTP) ⚑")
    
    test_q = "Tell me about Python coding."
    print(f"\nQUERY: {test_q}")
    
    req = urllib.request.Request(
        f"http://localhost:{ROUTER_PORT}/v1/chat/completions",
        data=json.dumps({"messages": [{"content": test_q}]}).encode(),
        headers={'Content-Type': 'application/json'}
    )
    
    try:
        with urllib.request.urlopen(req) as f:
            res = json.loads(f.read().decode())
            print(f"RESPONSE: {res['choices'][0]['message']['content']}")
            if "Shard_Coding" in res['choices'][0]['message']['content']:
                print("βœ… SUCCESS: Router correctly selected Coding Shard.")
            else:
                print("❌ FAIL: Wrong shard selected.")
    except Exception as e:
        print(f"❌ TEST FAILED: {e}")
        
    # Test 2
    test_q2 = "What happened in Rome?"
    print(f"\nQUERY: {test_q2}")
    req2 = urllib.request.Request(
        f"http://localhost:{ROUTER_PORT}/v1/chat/completions",
        data=json.dumps({"messages": [{"content": test_q2}]}).encode(),
        headers={'Content-Type': 'application/json'}
    )
    with urllib.request.urlopen(req2) as f:
        res = json.loads(f.read().decode())
        print(f"RESPONSE: {res['choices'][0]['message']['content']}")
        
    print("\nπŸ›‘ Test Complete.")