summaryrefslogtreecommitdiff
path: root/transport/transport_plus_ipc.sh.inc
blob: 92e515a8da1850dd299a7d630d2acd2a4a4624f7 (plain)
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

# set $line variable for further processing
transport_plus_ipc_read_line() {
    local postpone_ipc=0

    while true
    do
        local n_msgs=$(wc -l "${IPC_QUEUE_FILE}" 2> /dev/null | cut -d ' ' -f 1)

        # set $line variable from IPC message
        if ! (( ${postpone_ipc} )) && (( ${n_msgs} )) && [[ -w "${IPC_QUEUE_FILE}" ]]
        then
            read -r line < "${IPC_QUEUE_FILE}"

            if [[ "${line}" =~ ^([0-9]{10})\ (#[^\ ]*)\ ([^\ ].*)$ ]]
            then
                local delivery_time="${BASH_REMATCH[1]}"
                local target_channel="${BASH_REMATCH[2]}"
                local message="${BASH_REMATCH[3]}"
                local pending_line="${IPC_INJECT_PREFIX} ${target_channel} :${message}"
                local should_postpone=$( (( ${delivery_time} > $(date +%s) )) && echo 1 || echo 0)

                line=$( ! (( ${should_postpone} )) && echo "${pending_line}" || echo '')

# echo "[IPC]: delivery_time=${BASH_REMATCH[1]}"
# echo "[IPC]: target_channel=${BASH_REMATCH[2]}"
# echo "[IPC]: message=${BASH_REMATCH[3]}"
# echo "[IPC]: pending_line=${pending_line}"

                [[ -n "${line}" ]] && log_info "[IPC]: relaying to channel: ${target_channel} msg: ${message}" # || log_info "[IPC]: postponing"

            else
                line='ERROR: invalid IPC message: ${line}'
            fi

            [[ -z "${line}" ]] && postpone_ipc=1 && continue

            if (( ${n_msgs} < 2 ))
            then
                echo -n > "${IPC_QUEUE_FILE}"
            else
                tail -n +2 "${IPC_QUEUE_FILE}" | sponge "${IPC_QUEUE_FILE}"
            fi

            break

        # set $line variable from network message
        else
            read -t 5 -ru 3 line
            the_return_code="${?}"
            (( the_return_code == 0 )) && break
            (( the_return_code > 128 )) && continue
            [[ "${the_return_code}" -ne 0 ]] && return
        fi
    done

    time_get_current 'transport_lastvalidtime'
    line=${line//$'\r'/}
}