1 year ago
#348155
Dr. Jedi
Custom Gnome indicator disappears
I'm using the attached python script to implement a Gnome indicator in the top panel. It receives items from a Java app via a socket. These items are then showed as a list when I click on it.
The problem is that sometimes, when it receives a message from the Java app, it disappears. I guess it crashes but I do not know how to find out why, or what exactly is happening.
A little walk-through on the attached code:
- in socket_consumer() it waits for a message from the socket
- in processCmd() it processes the message
- waits for the next message in socket_consumer()
When I look at the logs at the time the problem happens, I see that it receives a message, but does not reach the point where it waits for the next one (the usual "Waiting for message" does not appear in the log again).
Do you see an issue in the code?
Or, how could go about debugging the script in addition to the logging?
Here is the code:
#!/usr/bin/env python3
#
# Copyright 2009 Canonical Ltd.
#
# Authors: Neil Jagdish Patel <neil.patel@canonical.com>
# Jono Bacon <jono@ubuntu.com>
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of either or both of the following licenses:
#
# 1) the GNU Lesser General Public License version 3, as published by the
# Free Software Foundation; and/or
# 2) the GNU Lesser General Public License version 2.1, as published by
# the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the applicable version of the GNU Lesser General Public
# License for more details.
#
# You should have received a copy of both the GNU Lesser General Public
# License version 3 and version 2.1 along with this program. If not, see
# <http://www.gnu.org/licenses/>
#
import gi
import os
gi.require_version('Gtk', '3.0')
gi.require_version('AppIndicator3', '0.1')
from gi.repository import Notify
from gi.repository import AppIndicator3 as appindicator
from gi.repository import Gtk as gtk
import sys
import threading
import logging
import signal
import socket
from socket import *
menu_items={}
addr = ("127.0.0.1", 7002)
workIcon='work-1.png'
idleIcon='work-2.png'
LOG = "/tmp/jpc.log"
#workIcon='circle-cropped.png'
#idleIcon='circle-cropped-2.png'
def menuitem_response(w, buf):
#sys.stdout.write(buf+"\n")
#sys.stdout.flush()
write_socket(buf)
def notify(msg):
Notify.Notification.new(msg).show()
def processCmd(cmd, arg1, arg2="n/a"):
logging.debug("Received "+cmd)
if (cmd=='add-menuitem'):
menu_item = gtk.MenuItem(arg1)
menu.append(menu_item)
menu_items[arg1]=menu_item
menu_item.connect("activate", menuitem_response, arg1)
menu_item.show()
elif (cmd=='highlight-menuitem'):
menu_items[arg1].get_child().set_text("=== "+arg1+" ==")
elif (cmd=='clearall'):
for item in menu_items.values():
menu.remove(item)
menu_items.clear()
elif (cmd=='unhighlight-menuitems'):
for key,value in menu_items.items():
value.get_child().set_text(key)
elif (cmd=='change-icon-working'):
ind.set_icon(os.path.abspath('scripts/'+workIcon))
elif (cmd=='change-icon-not-working'):
ind.set_icon(os.path.abspath('scripts/'+idleIcon))
elif (cmd=='read-items'):
logging.debug("read-items")
content="###testA"
for item in menu_items:
content=content+str(item)
logging.debug("item string is"+content)
#sys.stdout.write(content+"\n")
#sys.stdout.flush()
write_socket(content)
def write_socket(msg):
logging.debug("would print something")
client_socket.sendto(msg.encode(), addr)
#def sysin_consumer(name):
# s = sys.stdin.readline().strip()
# while True:
# #notify("processing: "+s);
# (cmd, arg)=s.split(';')
# processCmd(cmd, arg)
# s = sys.stdin.readline().strip()
def socket_consumer(name):
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('127.0.0.1', 7001))
logging.debug("socket bound")
while True:
logging.debug("Waiting for message")
message, address = serverSocket.recvfrom(256)
logging.debug("Received message from socket")
(cmd,arg)=message.decode().split(';')
processCmd(cmd, arg)
if __name__ == "__main__":
signal.signal(signal.SIGINT, signal.SIG_DFL)
logging.basicConfig(filename=LOG, filemode="w", level=logging.DEBUG)
logging.debug("log test")
ind = appindicator.Indicator.new("example-simple-client",
os.path.abspath('scripts/'+idleIcon),appindicator.IndicatorCategory.APPLICATION_STATUS)
ind.set_status (appindicator.IndicatorStatus.ACTIVE)
Notify.init("gnome-cannula")
client_socket = socket(AF_INET, SOCK_DGRAM)
# create a menu
menu = gtk.Menu()
ind.set_menu(menu) #this is actually crucial to show anything in the panel
#x=threading.Thread(target=sysin_consumer, args=(1,))
#x.start()
y=threading.Thread(target=socket_consumer, args=(1,))
y.start()
gtk.main()
This is how I start the script from the Java app:
String cmd = "scripts/gnome-indicator.py";
try {
indicatorProcess = Runtime.getRuntime().exec(cmd);
Thread.sleep(300);
if (!indicatorProcess.isAlive()) {
System.out.println("Indicator error - process not alive. Exit value: " + indicatorProcess.exitValue());
}
inp = new BufferedReader(new InputStreamReader(indicatorProcess.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(indicatorProcess.getOutputStream()));
//did not helped to get script errors
//err = new BufferedReader(new InputStreamReader(indicatorProcess.getErrorStream()));
} catch (Exception e) {
e.printStackTrace();
}
The BufferedReader and Writer above are not used. This is how I send a message to it:
DatagramPacket packet = null;
try {
packet = new DatagramPacket(s.getBytes(), s.getBytes().length, address, 7001);
clientSocket.send(packet);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Here is an example log output:
DEBUG:root:log test
DEBUG:root:socket bound
DEBUG:root:Waiting for message
DEBUG:root:Received message from socket
DEBUG:root:Received clearall
DEBUG:root:Waiting for message
DEBUG:root:Received message from socket
DEBUG:root:Received add-menuitem
My system setup is:
- Ubuntu 18.04.06
- OpenJDK 11.0.14
- Python: 3.6.9
python
java
sockets
gnome
0 Answers
Your Answer