Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shutdown service #9

Merged
merged 2 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions snap/hooks/install
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
#!/bin/sh -e

# Define a function to log messages
log() {
local message="$1"
# Log the message with logger
logger -t "${SNAP_NAME}" "install hook: $message"
}


# Parameters are 'unset'.
# Default configuration values is left to the launch file.
snapctl set driver.mecanum=True
Expand All @@ -14,22 +22,22 @@ snapctl set ros-domain-id=0
snapctl set fastdds-default-profiles-file="fastdds.xml"

if ! snapctl is-connected ros-humble-ros-base; then
logger -t ${SNAP_NAME} "Plug 'ros-humble-ros-base' isn't connected, \
please run: snap connect ${SNAP_NAME}:ros-humble-ros-base ros-humble-ros-base:ros-humble-ros-base"
log "Plug 'ros-humble-ros-base' isn't connected, please run:"
log "sudo snap connect ${SNAP_NAME}:ros-humble-ros-base ros-humble-ros-base:ros-humble-ros-base"
fi

if ! snapctl is-connected shm-plug; then
logger -t ${SNAP_NAME} "Plug 'shm-plug' isn't connected, \
please run: snap connect ${SNAP_NAME}:shm-plug ${SNAP_NAME}:shm-slot"
log "Plug 'shm-plug' isn't connected, please run:"
log "sudo snap connect ${SNAP_NAME}:shm-plug ${SNAP_NAME}:shm-slot"
fi

if ! snapctl is-connected raw-usb; then
logger -t ${SNAP_NAME} "Plug 'raw-usb' isn't connected, \
please run: snap connect ${SNAP_NAME}:raw-usb"
log "Plug 'raw-usb' isn't connected, please run:"
log "sudo snap connect ${SNAP_NAME}:raw-usb"
fi

# copy meshes to shared folder
logger -t ${SNAP_NAME} "copy meshes to '${SNAP_COMMON}/ros2_ws/'"
log "copy meshes to '${SNAP_COMMON}/ros2_ws/'"
mkdir -p ${SNAP_COMMON}/ros2_ws
cp -r $SNAP/opt/ros/snap/share/rosbot_xl_description ${SNAP_COMMON}/ros2_ws/rosbot_xl_description
cp -r $SNAP/opt/ros/snap/share/ros_components_description ${SNAP_COMMON}/ros2_ws/ros_components_description
88 changes: 88 additions & 0 deletions snap/local/db_server_launcher.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/bin/bash
# place in /usr/local/sbin

# Define a function to log messages
log() {
local message="$1"
# Log the message with logger
logger -t "${SNAP_NAME}" "db_server_launcher: $message"
# Echo the message to standard error
echo >&2 "$message"
}

SERVER_IP=192.168.77.2
SERVER_PORT=3000

fifo_path="/tmp/response"
rm -f "$fifo_path"
mkfifo "$fifo_path"

# Define a path for the shutdown flag file
SHUTDOWN_FLAG="/tmp/shutdown_flag"
rm -f "$SHUTDOWN_FLAG" # Ensure it does not exist initially

handleRequest() {
while read -r line; do
echo "$line"
trline=$(echo "$line" | tr -d '[\r\n]')

[ -z "$trline" ] && break

HEADLINE_REGEX='(.*?)\s(.*?)\sHTTP.*?'

if [[ "$trline" =~ $HEADLINE_REGEX ]]; then
REQUEST=$(echo "$trline" | sed -E "s/$HEADLINE_REGEX/\1 \2/")
fi
done

case "$REQUEST" in
"GET /ping")
RESPONSE="HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\npong"
;;
"GET /shutdown")
RESPONSE="HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nshutting down"
touch "$SHUTDOWN_FLAG" # Create the flag file to signal shutdown
;;
*)
RESPONSE="HTTP/1.1 404 NotFound\r\nContent-Type: text/html\r\n\r\nNot Found"
;;
esac

echo -e "$RESPONSE" >"$fifo_path"
}

# check if we can start db-server

# Send a GET request to the server's /ping endpoint
status=$(curl --silent --max-time 1 "http://$SERVER_IP:$SERVER_PORT/ping")
if [ $? -eq 0 ]; then
log "Received response: $status"
fi

# Check the response
if [[ "$status" == "pong" ]]; then
log "Legacy server running on port 3000. To turn it off run:"
log "sudo systemctl stop db-server.service && sudo systemctl disable db-server.service"
log "After that start the service:"
log "sudo snap start --enable ${SNAP_NAME}.db-server"

snapctl stop --disable ${SNAP_NAME}.db-server 2>&1 || true
fi

log "Listening on $SERVER_IP:$SERVER_PORT..."

while true; do
# Handle requests
cat "$fifo_path" | nc -lN -s $SERVER_IP -p $SERVER_PORT | handleRequest

if [ -f "$SHUTDOWN_FLAG" ]; then
log "shutting down..."

dbus-send --system --print-reply --dest=org.freedesktop.login1 \
/org/freedesktop/login1 org.freedesktop.login1.Manager.PowerOff boolean:true

break
fi

sleep 1
done
16 changes: 12 additions & 4 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ apps:
slots: [shm-slot]
extensions: [ros2-humble-ros-base]

db-server:
command: usr/bin/db_server_launcher.sh
daemon: simple
install-mode: enable
plugs: [network, network-bind, shutdown]

rosbot-xl:
command: usr/bin/launcher.sh
command-chain: [usr/bin/check_daemon_running.sh, usr/bin/ros_setup.sh]
Expand Down Expand Up @@ -137,10 +143,12 @@ parts:
stage-packages:
- ros-humble-teleop-twist-keyboard

# apt-dependencies:
# plugin: nil
# stage-packages:
# - ros-humble-rmw-cyclonedds-cpp
apt-deppendencies:
plugin: nil
stage-packages:
- dbus
- curl
- netcat

pip3-dependencies:
plugin: python
Expand Down
Loading