File size: 7,400 Bytes
4686cf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217

#!/usr/bin/env python3
"""
مشغل موحد لنظام توزيع المهام
يوفر خيارات متعددة للتشغيل
"""

import sys
import os
import subprocess
import argparse
import time
from pathlib import Path
from peer_discovery import PORT

def check_requirements():
    """فحص المتطلبات والاعتماديات"""
    required_files = [
        'background_service.py',
        'main.py',
        'peer_server.py',
        'rpc_server.py',
        'load_balancer.py'
    ]
    
    missing_files = []
    for file in required_files:
        if not Path(file).exists():
            missing_files.append(file)
            
    if missing_files:
        print(f"❌ ملفات مفقودة: {', '.join(missing_files)}")
        return False
        
    return True

def install_tray_dependencies():
    """تثبيت اعتماديات أيقونة شريط النظام"""
    try:
        subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'pystray', 'Pillow'])
        print("✅ تم تثبيت اعتماديات أيقونة شريط النظام")
        return True
    except subprocess.CalledProcessError:
        print("❌ فشل في تثبيت اعتماديات أيقونة شريط النظام")
        return False

def start_background_service():
    """بدء تشغيل الخدمة في الخلفية"""
    print("🚀 بدء تشغيل الخدمة في الخلفية...")
    
    # تشغيل الخدمة الخلفية
    process = subprocess.Popen(
        [sys.executable, 'background_service.py', 'start'],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE
    )
    
    # انتظار قليل للتأكد من بدء التشغيل
    time.sleep(2)
    
    if process.poll() is None:
        print("✅ تم بدء تشغيل الخدمة الخلفية بنجاح")
        return process
    else:
        print("❌ فشل في بدء تشغيل الخدمة الخلفية")
        return None

def start_with_tray():
    """تشغيل النظام مع أيقونة شريط النظام"""
    print("🖱️ تشغيل النظام مع أيقونة شريط النظام...")
    
    # بدء الخدمة الخلفية أولاً
    bg_process = start_background_service()
    if not bg_process:
        return False
        
    time.sleep(3)  # انتظار حتى تصبح الخدمة جاهزة
    
    try:
        # تشغيل أيقونة شريط النظام
        subprocess.run([sys.executable, 'system_tray.py'])
    except KeyboardInterrupt:
        print("\n🛑 إيقاف النظام...")
        # إيقاف الخدمة الخلفية
        try:
            import requests
            requests.post('http://localhost:8888/stop', timeout=5)
        except:
            bg_process.terminate()
            
    return True

def start_interactive():
    """تشغيل النظام في الوضع التفاعلي"""
    print("🖥️ تشغيل النظام في الوضع التفاعلي...")
    
    # بدء الخدمة الخلفية
    bg_process = start_background_service()
    if not bg_process:
        return False
        
    time.sleep(3)
    
    # تشغيل الواجهة التفاعلية
    try:
        import requests
        requests.post('http://localhost:8888/show-ui', timeout=5)
        print("✅ تم تشغيل الواجهة التفاعلية")
        
        # فتح المتصفح
        import webbrowser
        time.sleep(2)
        webbrowser.open('http://localhost:5173')
        
        # انتظار إنهاء المستخدم
        input("اضغط Enter لإيقاف النظام...")
        
    except KeyboardInterrupt:
        pass
    finally:
        print("🛑 إيقاف النظام...")
        try:
            import requests
            requests.post('http://localhost:8888/stop', timeout=5)
        except:
            bg_process.terminate()
            
    return True

def start_headless():
    """تشغيل النظام بدون واجهة (للخوادم)"""
    print("⚙️ تشغيل النظام بدون واجهة...")
    
    try:
        # تشغيل الخدمة الخلفية والانتظار
        subprocess.run([sys.executable, 'background_service.py', 'start'])
    except KeyboardInterrupt:
        print("\n🛑 إيقاف النظام...")
        
    return True

def show_status():
    """عرض حالة النظام"""
    subprocess.run([sys.executable, 'background_service.py', 'status'])

def stop_system():
    """إيقاف النظام"""
    subprocess.run([sys.executable, 'background_service.py', 'stop'])

def main():
    parser = argparse.ArgumentParser(
        description="مشغل نظام توزيع المهام الذكي",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
أمثلة الاستخدام:
  python launcher.py --tray           # تشغيل مع أيقونة شريط النظام
  python launcher.py --interactive    # تشغيل تفاعلي مع واجهة
  python launcher.py --headless       # تشغيل بدون واجهة (للخوادم)
  python launcher.py --status         # عرض حالة النظام
  python launcher.py --stop           # إيقاف النظام
        """
    )
    
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument('--tray', action='store_true', 
                      help='تشغيل مع أيقونة شريط النظام')
    group.add_argument('--interactive', action='store_true',
                      help='تشغيل تفاعلي مع واجهة')
    group.add_argument('--headless', action='store_true',
                      help='تشغيل بدون واجهة (للخوادم)')
    group.add_argument('--status', action='store_true',
                      help='عرض حالة النظام')
    group.add_argument('--stop', action='store_true',
                      help='إيقاف النظام')
    
    parser.add_argument('--install-deps', action='store_true',
                       help='تثبيت الاعتماديات المطلوبة')
    
    args = parser.parse_args()
    
    # فحص المتطلبات
    if not check_requirements():
        return 1
        
    # تثبيت الاعتماديات إذا طُلب ذلك
    if args.install_deps:
        install_tray_dependencies()
        return 0
        
    # تنفيذ الأمر المطلوب
    if args.status:
        show_status()
    elif args.stop:
        stop_system()
    elif args.headless:
        success = start_headless()
    elif args.interactive:
        success = start_interactive()
    elif args.tray:
        # تثبيت اعتماديات أيقونة شريط النظام إذا لم تكن موجودة
        try:
            import pystray
        except ImportError:
            print("📦 تثبيت اعتماديات أيقونة شريط النظام...")
            if not install_tray_dependencies():
                print("❌ فشل في تثبيت الاعتماديات، التشغيل في الوضع التفاعلي...")
                success = start_interactive()
            else:
                success = start_with_tray()
        else:
            success = start_with_tray()
    
    return 0 if success else 1

if __name__ == "__main__":
    sys.exit(main())