Skip to content

Commit 0bd5984

Browse files
committed
remote control server will be silently restarted if crashed
1 parent f52f49c commit 0bd5984

File tree

7 files changed

+46
-11
lines changed

7 files changed

+46
-11
lines changed

docs/headless.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ <h2 id="goal">Goal <span style="padding-left: 10px;"><sup style="font-size: 50%"
7474
<p><strong>Screen</strong> is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells)… Programs continue to run when their window is currently not visible and even when the whole screen session <strong>is detached from the user’s terminal</strong>.”</p>
7575
<p><strong>PyRadio</strong> users <a target="_blank" href="https://github.com/Wikinaut">Wikinaut</a> and <a target="_blank" href="https://github.com/aleksandr-sabitov">aleksandr-sabitov</a> on <a target="_blank" href="https://github.com/coderholic/pyradio/issues/184">github</a> have come up with the idea to use this approach to run the application on their headless Raspberry Pi, so kudos to them!</p>
7676
<h3 id="usage">Usage</h3>
77-
<p>After the program is started, the only way to interact with it is through its integrated web server. Please refref to the relevant document for more info on the <a href="server.html">remote control server</a>.</p>
77+
<p>After the program is started, the only way to interact with it is through its integrated web server. Please refer to the relevant document for more info on the <a href="server.html">remote control server</a>.</p>
7878
<p>The web server can be accessed either through a terminal (address <strong>http://ip:port</strong>) using wget or curl, or through a web browser (address <strong>http://ip:port/html</strong>).</p>
7979
<p>The ip and port will be set using the <strong>–headless</strong> command line option.</p>
8080
<p>The ip can either be:</p>
@@ -84,14 +84,16 @@ <h3 id="usage">Usage</h3>
8484
<li><strong>lan</strong><br />
8585
The server will be accessible by any system on the LAN. The ip is the one assigned to the network interface of the system.</li>
8686
<li>An actual <strong>IP</strong><br />
87-
This is in case when a machine has more than one network interfaces and the **lan* setting is ambigous.</li>
87+
This is in case when a machine has more than one network interfaces and the <strong>lan</strong> setting is ambiguous.</li>
8888
</ol>
8989
<p>For example:</p>
9090
<ul>
9191
<li><p>using <strong>–headless lan:12345</strong><br />
9292
will make the web server listen to the network interface IP address, port 12345.</p></li>
9393
<li><p>using <strong>–headless 192.168.122.101:4567</strong><br />
94-
will make the web server listen to the IP 192.168.122.101, port 4567.</p></li>
94+
will make the web server listen to the IP 192.168.122.101, port 4567.<br />
95+
<br />
96+
If the IP is not assigned to any network interfaces, the default (<strong>localhost:1111</strong>) will be silently used; please always check the server’s address with the command: <strong>pyradio –addr</strong>.</p></li>
9597
<li><p>using <strong>–headless localhost:23456</strong><br />
9698
will make the web server listen to 127.0.0.1, port 23456</p></li>
9799
<li><p>using <strong>–headless auto</strong><br />
@@ -121,6 +123,7 @@ <h3 id="how-it-works">How it works</h3>
121123
<li><p>we cannot start a second headless server, and</p></li>
122124
<li><p>we can get info about the server running.</p></li>
123125
</ul>
126+
<p>The “headless server lock file” is saved as <em>~/.config/pyeadio/data/server-headles.txt</em> and will contain the IP address and port the servers is listening to. This is especially useful in case a user script needs to get this info (instead of parsing the output of the command <strong>pyradio –addr</strong>).</p>
124127
<h2 id="installation">Installation <span style="padding-left: 10px;"><sup style="font-size: 50%"><a href="#" title="Go to top of the page">Top</a></sup></span></h2>
125128
<p>By the term “installation”, we mean that we set up things in such a way, that after we log into the system, we find <strong>PyRadio</strong> ready to accept connections.</p>
126129
<p>So, the installation can be as easy as adding a line in a configuration file (or the startup section of the <em>desktop environment</em>) or as hard as adding a system service.</p>

docs/headless.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Now, **PyRadio** is a **terminal application**; it actually **needs** a terminal
3939

4040
### Usage
4141

42-
After the program is started, the only way to interact with it is through its integrated web server. Please refref to the relevant document for more info on the [remote control server](server.md).
42+
After the program is started, the only way to interact with it is through its integrated web server. Please refer to the relevant document for more info on the [remote control server](server.md).
4343

4444
The web server can be accessed either through a terminal (address **http://ip:port**) using `wget` or `curl`, or through a web browser (address **http://ip:port/html**).
4545

@@ -52,15 +52,17 @@ The server will be accessible only by programs running in the system. The `ip` i
5252
2. **lan** \
5353
The server will be accessible by any system on the LAN. The `ip` is the one assigned to the network interface of the system.
5454
3. An actual **IP** \
55-
This is in case when a machine has more than one network interfaces and the **lan* setting is ambigous.
55+
This is in case when a machine has more than one network interfaces and the **lan** setting is ambiguous.
5656

5757
For example:
5858

5959
- using **--headless lan:12345** \
6060
will make the web server listen to the network interface IP address, port 12345.
6161

6262
- using **--headless 192.168.122.101:4567** \
63-
will make the web server listen to the IP 192.168.122.101, port 4567.
63+
will make the web server listen to the IP 192.168.122.101, port 4567. \
64+
\
65+
If the IP is not assigned to any network interfaces, the default (**localhost:1111**) will be silently used; please always check the server's address with the command: **pyradio --addr**.
6466

6567
- using **--headless localhost:23456** \
6668
will make the web server listen to 127.0.0.1, port 23456
@@ -109,6 +111,8 @@ It will create a "headless server lock file", though, so that
109111

110112
- we can get info about the server running.
111113

114+
The "headless server lock file" is saved as *~/.config/pyeadio/data/server-headles.txt* and will contain the IP address and port the servers is listening to. This is especially useful in case a user script needs to get this info (instead of parsing the output of the command **pyradio --addr**).
115+
112116
## Installation
113117

114118
By the term "installation", we mean that we set up things in such a way, that after we log into the system, we find **PyRadio** ready to accept connections.

docs/pyradio_server.1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ PyRadio 0.9.2.20
274274
Status: In playback
275275
.br
276276
Station (id=5): "Classical Christmas FM"
277+
.br
278+
Title: Patti Page - Jingle bells
277279
.br
278280
Selection (id=5): "Classical Christmas FM"\fB
279281

docs/server.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ <h2 id="table-of-contents">Table of Contents <span style="padding-left: 10px;"><
5050
<li><a href="#web-interface-buttons">Web Interface buttons</a></li>
5151
</ul></li>
5252
<li><a href="#using-the-text-server">Using the Text Server</a></li>
53+
<li><a href="#server-lock-file">Server lock file</a></li>
5354
<li><a href="#examples">Examples</a></li>
5455
<li><a href="#text-vs.-web-commands">Text vs. Web commands</a></li>
5556
</ul></li>
@@ -69,7 +70,7 @@ <h2 id="remote-control-server">Remote Control Server <span style="padding-left:
6970
<li><p><strong>Server IP</strong><br />
7071
This can either be <strong>localhost</strong> (the server will be accessible from the current system only) or <strong>LAN</strong> (the server will be accessible from any PC on the local network).<br />
7172
<br />
72-
If the machine has more that one interface (network card), the actual IPs will be available for selection.</p></li>
73+
If the machine has more that one interface (network card), the actual IPs will be available for selection as well.</p></li>
7374
<li><p><strong>Server Port</strong><br />
7475
This is the port the server is listening to. Any free port number between 1025 and 65535 can be set here (default value is 9998).</p></li>
7576
<li><p><strong>Auto-start Server</strong><br />
@@ -160,6 +161,8 @@ <h3 id="using-the-text-server">Using the Text Server</h3>
160161
/search_radio_browser/x /srb/x execute search item x
161162
(x comes from /lrb)</pre>
162163
<p>The “<strong>Restricted Commands</strong>” will not work in <strong>Playlist mode</strong>; the “<strong>Global Commands</strong>” will work everywhere.</p>
164+
<h3 id="server-lock-file">Server lock file</h3>
165+
<p>When the server is up and running, a “server lock file” will be created; the file is named <strong>~/.config/pyradio/data/server.txt</strong> and contains the IP address and port the server is listening to; this is especially useful for user scripts that want to get hold of this information.</p>
163166
<h3 id="examples">Examples</h3>
164167
<p>The following commands will increase / decrease the volume and mute the player:</p>
165168
<pre>$ curl http://192.168.122.4:9998/vu
@@ -205,6 +208,7 @@ <h3 id="examples">Examples</h3>
205208
Search: Name: christmas, Order: votes, Reverse: true
206209
Status: In playback
207210
Station (id=5): &quot;Classical Christmas FM&quot;
211+
Title: Patti Page - Jingle bells
208212
Selection (id=5): &quot;Classical Christmas FM&quot;</pre>
209213
<h3 id="text-vs.-web-commands">Text vs. Web commands</h3>
210214
<p>On first glance, the difference between a <strong>Text</strong> and a <strong>Web</strong> command is the <em>/html</em> part that exists in the later.</p>

docs/server.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* [Using the Web Server](#using-the-web-server)
88
* [Web Interface buttons](#web-interface-buttons)
99
* [Using the Text Server](#using-the-text-server)
10+
* [Server lock file](#server-lock-file)
1011
* [Examples](#examples)
1112
* [Text vs. Web commands](#text-vs.-web-commands)
1213

@@ -29,7 +30,7 @@ The options one can set are:
2930
1. **Server IP** \
3031
This can either be **localhost** (the server will be accessible from the current system only) or **LAN** (the server will be accessible from any PC on the local network). \
3132
\
32-
If the machine has more that one interface (network card), the actual IPs will be available for selection.
33+
If the machine has more that one interface (network card), the actual IPs will be available for selection as well.
3334

3435
2. **Server Port** \
3536
This is the port the server is listening to. Any free port number between 1025 and 65535 can be set here (default value is 9998).
@@ -157,6 +158,10 @@ Restricted Commands (Main mode only)
157158

158159
The "**Restricted Commands**" will not work in **Playlist mode**; the "**Global Commands**" will work everywhere.
159160

161+
### Server lock file
162+
163+
When the server is up and running, a "server lock file" will be created; the file is named **~/.config/pyradio/data/server.txt** and contains the IP address and port the server is listening to; this is especially useful for user scripts that want to get hold of this information.
164+
160165
### Examples
161166

162167
The following commands will increase / decrease the volume and mute the player:
@@ -231,6 +236,7 @@ PyRadio 0.9.2.20
231236
Search: Name: christmas, Order: votes, Reverse: true
232237
Status: In playback
233238
Station (id=5): "Classical Christmas FM"
239+
Title: Patti Page - Jingle bells
234240
Selection (id=5): "Classical Christmas FM"
235241
```
236242

pyradio/radio.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,8 @@ def _text_info(self):
816816
if self.player.isPlaying():
817817
out.append(' Status: In playback {}'.format('(muted)' if self.player.muted else ''))
818818
out.append(' Station (id={0}): "{1}"'.format(self.playing+1, self.stations[self.playing][0]))
819+
if self.stations[self.playing][0] not in self.log.song_title:
820+
out.append(' Title: {}'.format(self.log.song_title))
819821
else:
820822
out.append(' Status: Idle')
821823
out.append(' Selection (id={0}): "{1}"'.format(self.selection+1, self.stations[self.selection][0]))
@@ -865,6 +867,7 @@ def _set_text_volume(self, vol):
865867
if self.player.isPlaying() and \
866868
not self.player.muted:
867869
ivol = int(vol)
870+
# self._remote_control_server._send_text('Volume set!')
868871
self.player.set_volume(vol)
869872
sleep(.1)
870873
return 'Volume set to: {}'.format(vol)
@@ -3678,6 +3681,13 @@ def _print_session_locked(self):
36783681
is_message=True)
36793682

36803683
def _print_remote_control_server_error(self, msg=None):
3684+
''' restart the srver instead of displaying
3685+
a message and terminate '''
3686+
self._remote_control_server = self._remote_control_server_thread = None
3687+
self._restart_remote_control_server()
3688+
if logger.isEnabledFor(logging.DEBUG):
3689+
logger.debug('Remote Control server crashed! Restarting...')
3690+
return
36813691
if msg:
36823692
self._server_error_msg = str(msg)
36833693
txt = r'''
@@ -3698,6 +3708,13 @@ def _print_remote_control_server_error(self, msg=None):
36983708
self._remote_control_server = self._remote_control_server_thread = None
36993709

37003710
def _print_remote_control_server_dead_error(self, msg=None):
3711+
''' restart the srver instead of displaying
3712+
a message and terminate '''
3713+
self._remote_control_server = self._remote_control_server_thread = None
3714+
self._restart_remote_control_server()
3715+
if logger.isEnabledFor(logging.DEBUG):
3716+
logger.debug('Remote Control server crashed! Restarting...')
3717+
return
37013718
if msg:
37023719
self._server_dead_msg = str(msg)
37033720
txt = '''

pyradio/server.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -945,15 +945,14 @@ def start_remote_control_server(
945945
self.client_socket, address = server.accept()
946946
request = self.client_socket.recv(1024)
947947
except socket.error as e:
948-
if logger.isEnabledFor(logger.ERROR):
949-
logger.error('Server accept error: "{}"'.format(e))
950948
self._remove_report_file()
951949
dead_func(e)
952950
break
953951
self.error = None
954952
self._handle_client_connection(address, request)
955953
if self.error is not None:
956-
self.client_socket.close()
954+
# self.client_socket.close()
955+
self._remove_report_file()
957956
dead_func(self.error)
958957
break
959958
if self._path == '/quit':

0 commit comments

Comments
 (0)