-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_simulation.sh
More file actions
251 lines (201 loc) · 6.56 KB
/
run_simulation.sh
File metadata and controls
251 lines (201 loc) · 6.56 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/bin/bash
# Network-Performance-Monitor Simulation Mode Setup and Run Script
# This script runs the application in simulation mode for testing without real hardware
set -e # Exit immediately if a command exits with a non-zero status
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to check dependencies
check_dependencies() {
print_status "Checking for required dependencies..."
# Check for Python
if command_exists python3; then
PYTHON_CMD="python3"
print_success "Python found: $(python3 --version 2>&1)"
elif command_exists python; then
PYTHON_CMD="python"
print_success "Python found: $(python --version 2>&1)"
else
print_error "Python is not installed. Please install Python 3.x and try again."
exit 1
fi
# Check for Node.js
if command_exists node; then
print_success "Node.js found: $(node --version)"
else
print_error "Node.js is not installed. Please install Node.js and try again."
exit 1
fi
# Check for npm
if command_exists npm; then
print_success "npm found: $(npm --version)"
else
print_error "npm is not installed. Please install npm and try again."
exit 1
fi
}
# Function to install Python dependencies
install_python_deps() {
print_status "Installing Python dependencies from requirements.txt..."
if [ ! -f "requirements.txt" ]; then
print_error "requirements.txt not found in project root."
exit 1
fi
# Create virtual environment if it doesn't exist
if [ ! -d "venv" ]; then
print_status "Creating Python virtual environment..."
$PYTHON_CMD -m venv venv
fi
# Activate virtual environment
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" || "$OSTYPE" == "cygwin" ]]; then
# Windows
source venv/Scripts/activate
else
# Unix/Linux/macOS
source venv/bin/activate
fi
# Upgrade pip
pip install --upgrade pip
# Install requirements
pip install -r requirements.txt
print_success "Python dependencies installed successfully."
}
# Function to install Node.js dependencies
install_node_deps() {
print_status "Installing Node.js dependencies in frontend/app1 directory..."
if [ ! -d "frontend/app1" ]; then
print_error "frontend/app1 directory not found."
exit 1
fi
cd frontend/app1
if [ ! -f "package.json" ]; then
print_error "package.json not found in frontend/app1 directory."
exit 1
fi
npm install
print_success "Node.js dependencies installed successfully."
# Return to project root
cd ../..
}
# Function to start the backend API in simulation mode
start_backend() {
print_status "Starting backend API in simulation mode on port 8000..."
if [ ! -d "backend/api" ]; then
print_error "backend/api directory not found."
exit 1
fi
# Activate virtual environment
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" || "$OSTYPE" == "cygwin" ]]; then
# Windows
source venv/Scripts/activate
else
# Unix/Linux/macOS
source venv/bin/activate
fi
# Set simulation mode and start the backend in background
cd backend/api
SIMULATION_MODE=true $PYTHON_CMD -m uvicorn main:app --host 127.0.0.1 --port 8000 --reload &
BACKEND_PID=$!
cd ../..
# Wait a moment for the server to start
sleep 3
# Check if backend started successfully
if kill -0 $BACKEND_PID 2>/dev/null; then
print_success "Backend API started successfully in simulation mode with PID $BACKEND_PID"
echo $BACKEND_PID > backend_simulation.pid
else
print_error "Failed to start backend API in simulation mode"
exit 1
fi
}
# Function to start the frontend
start_frontend() {
print_status "Starting frontend on port 300..."
if [ ! -d "frontend/app1" ]; then
print_error "frontend/app1 directory not found."
exit 1
fi
cd frontend/app1
# Start the frontend in development mode
npm run dev &
FRONTEND_PID=$!
cd ../..
# Wait a moment for the server to start
sleep 3
# Check if frontend started successfully
if kill -0 $FRONTEND_PID 2>/dev/null; then
print_success "Frontend started successfully with PID $FRONTEND_PID"
echo $FRONTEND_PID > frontend_simulation.pid
else
print_error "Failed to start frontend"
exit 1
fi
}
# Function to clean up background processes
cleanup() {
print_status "Cleaning up background processes..."
# Kill backend if running
if [ -f "backend_simulation.pid" ]; then
BACKEND_PID=$(cat backend_simulation.pid)
if kill -0 $BACKEND_PID 2>/dev/null; then
kill $BACKEND_PID
print_status "Backend process $BACKEND_PID terminated."
fi
rm -f backend_simulation.pid
fi
# Kill frontend if running
if [ -f "frontend_simulation.pid" ]; then
FRONTEND_PID=$(cat frontend_simulation.pid)
if kill -0 $FRONTEND_PID 2>/dev/null; then
kill $FRONTEND_PID
print_status "Frontend process $FRONTEND_PID terminated."
fi
rm -f frontend_simulation.pid
fi
}
# Main execution
main() {
print_status "Network-Performance-Monitor Setup and Run Script (Simulation Mode)"
echo ""
# Set up cleanup trap
trap cleanup EXIT
# Check dependencies
check_dependencies
# Set simulation mode environment variable
export SIMULATION_MODE=true
print_status "Simulation mode enabled: $SIMULATION_MODE"
# Install dependencies
install_python_deps
install_node_deps
# Start the application in simulation mode
start_backend
start_frontend
print_status "Application started successfully in simulation mode!"
print_status "Backend API: http://127.0.1:8000"
print_status "Frontend: http://127.0.1:3000"
print_status "Press Ctrl+C to stop the application."
# Wait for both processes to finish
wait
}
# Execute main function
main "$@"