VARAWatch – Wine & Box86 Socket-loss Workaround (Obsolete!)

Update

A newer method has been developed to handle the VARA TNC losing ports under Wine & box86 (see RPI4 Trifecta: VARA, Packet & ARDOP – One Audio Interface). The below document is still descriptive of the problem but a slightly more complex method that handles a couple of extra situations that cannot be detected using the now-obsolete method below. The new method: VARA Linux – Keeping the TNC alive


intro

The VARA process running within the Wine & box86 emulator environment on the Raspberry Pi4 has a slight issue when VARA TNC dies or is terminated manually or restarted via systemd.

The Control Port of VARA becomes completely unreachable after the previous connection TIME_WAIT timer has counted to zero and the socket is released.

Either it is the VARA process itself or the underlying Windows system processes running within Wine and box86 emulator that are to blame, at the moment this is not entirely clear as although VARA does not exhibit this issue on real Windows systems the fact this hack works indicates that it could be addressed by the developer.

Here is a video demonstration:

Prerequisites:

This script comes with a systemd service unit file, if you wish to use this, it has a dependancy upon the vara.service file documented here, however there is a small modification required in order to wire the varawatch and vara unit files together, this is included in the appendix.

sudo apt install screen

Scripts

File name: /home/pi/bin/startvarawatch

Set Permission: chmod 755 startvarawatch
#!/bin/bash  
ControlPort="8300"
DataPort="8301"
Host="localhost"

function WakeyWakey {
	nc -w 5 $Host $DataPort
}

function GetState {
	ControlPortState=`netstat -apnlt | grep ${ControlPort} | grep box86 | awk '{print $6}'`
	ControlOldPort=`netstat -apnlt | grep ${ControlPort} | grep TIME_WAIT | awk '{print $5}' | cut -d':' -f2`

	DataPortState=`netstat -apntl | grep ${DataPort} | grep box86 | awk '{print $6}'`
	DataOldPort=`netstat -apntl | grep ${DataPort} | grep TIME_WAIT | awk '{print $5}' | cut -d':' -f2`
}

function Start {
	clear
	GetState
	CheckState
}
function CheckState {
	if [ ! -z "$ControlOldPort" ];
	then
		echo "Control Port is waiting to timeout... waiting"
		sleep 10
		Start	
	fi
	if [[ "$DataPortState" == LISTEN ]] && [[ "$ControlPortState" == "" ]];
	then
		echo "Control Port is dead but Data Port is open, let's wake the Control Port up (THIS IS A SMELLY HACK!)"
		WakeyWakey
		Start
	elif [[ "$DataPortState" == TIME_WAIT ]] && [[ "$ControlPortState" == "" ]];
	then
		echo "Control Port is dead but now Waiting for our own port ping to timeout"
		sleep 5
		Start
	fi
	if [[ "$ControlPortState" == LISTEN ]] && [[ "$DataPortState" == LISTEN ]]
	then
		echo "Control Port is listening...nothing to do"
		sleep 5
		Start
	fi
	if [[ "$ControlPortState" == ESTABLISHED ]] && [[ "$DataPortState" == ESTABLISHED ]]
	then
		echo "VARA is connected to something properly now"
		sleep 5
		Start
	fi
	echo "Nowt to do"
	sleep 5
	Start

}

GetState
CheckState
File name: /lib/systemd/system/varawatch.service
[Unit]
Description=VARA Port Watch Script by PE1RRR
After=vara.service
#Conflicts=wsjtx.service

[Service]
Type=forking
WorkingDirectory=/home/pi/bin
Restart=always
RestartSec=10
StartLimitInterval=60
StartLimitBurst=6
User=root
Group=root
ExecStart=/usr/bin/screen -S VARA-Watch -d -m /home/pi/bin/startvarawatch
SyslogIdentifier=VARAWatch

[Install]
WantedBy=multi-user.target

Monitoring The Console

To monitor the console output of the script, switch user to root and attach the screen session, if wondering- root is required for the netstat socket interrogation.

sudo su -
screen -r VA 

Only the first few letters of the screen console’s label is required to re-attach to the correct screen session (presuming more are running), the label is assigned in the ExecStart line of the varawatch.service systemd unit file.

Appendix

Slightly modified vara.service (notably the Wants). The startvara script is available on this page.

File name: /lib/systemd/system/vara.service
[Unit]
Description=VARA Start Script by PE1RRR
After=vvnc-modems.service
Wants=vvnc-modems.service varawatch.service
#Conflicts=wsjtx.service
[Service]
Type=forking
WorkingDirectory=/home/pi/bin
Restart=always
RestartSec=5
StartLimitInterval=60
StartLimitBurst=12
User=pi
Group=pi
ExecStart=/usr/bin/screen -S VARA-Console -d -m /home/pi/bin/startvara
ExecStop=/usr/local/bin/wineserver -k
SyslogIdentifier=VARA-Debug

[Install]
WantedBy=multi-user.target

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s