Skip to content

Commit

Permalink
implemented feature as requested by Kay, Skadi is now a singleton app…
Browse files Browse the repository at this point in the history
…lication, when a second instance is launched, it will send a message to the first which will then request window focus. The second instance will terminate after sending the message.
  • Loading branch information
s1mpl3x committed Sep 22, 2014
1 parent b3d89b9 commit b5d043c
Showing 1 changed file with 78 additions and 1 deletion.
79 changes: 78 additions & 1 deletion src/main/java/eu/over9000/skadi/SkadiMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
******************************************************************************/
package eu.over9000.skadi;

import java.awt.Frame;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Arrays;

import eu.over9000.skadi.channel.Channel;
import eu.over9000.skadi.channel.ChannelManager;
import eu.over9000.skadi.channel.ChannelUpdater;
Expand All @@ -39,9 +47,14 @@
public class SkadiMain {

public static final String CLIENT_ID = "i2uu9j43ure9x7n4ojpgg4hvcnw6y91";
private static final int SKADI_LOCKING_PORT = 37973;

private DatagramSocket lockingSocket;

private static SkadiMain instance;

private static final byte[] WAKEUP_SIGNATURE = "SKADI".getBytes();

private final boolean isLinux = System.getProperty("os.name").equals("Linux");

public String chrome_exec = this.isLinux ? "chromium-browser"
Expand All @@ -53,6 +66,7 @@ public class SkadiMain {
public boolean use_livestreamer = true;
public boolean display_notifications = true;
public boolean minimize_to_tray = false;
private Thread wakeupReceiverThread;

public static SkadiMain getInstance() {
if (SkadiMain.instance == null) {
Expand All @@ -66,6 +80,13 @@ public static void main(final String[] args) {
}

private void runInit(final String[] args) {

if (!this.createSocketLock()) {
System.out.println("could not create socket on port " + SkadiMain.SKADI_LOCKING_PORT
+ " (Skadi already running?), exiting.");
return;
}

this.addShutdownHook();

ChannelManager.getInstance();
Expand All @@ -81,6 +102,56 @@ private void runInit(final String[] args) {

}

private boolean createSocketLock() {
try {
this.lockingSocket = new DatagramSocket(SkadiMain.SKADI_LOCKING_PORT, InetAddress.getLoopbackAddress());
this.wakeupReceiverThread = new Thread(new Runnable() {

@Override
public void run() {
while ((SkadiMain.this.lockingSocket != null) && !SkadiMain.this.lockingSocket.isClosed()) {
final byte[] buffer = new byte[SkadiMain.WAKEUP_SIGNATURE.length];

final DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
try {
SkadiMain.this.lockingSocket.receive(incoming);

if (Arrays.equals(SkadiMain.WAKEUP_SIGNATURE, incoming.getData())) {
SkadiGUI.getInstance().setVisible(true);
SkadiGUI.getInstance().setState(Frame.NORMAL);
SkadiGUI.getInstance().toFront();
SkadiLogging.log("received wakeup on locking socket");
}

} catch (final IOException e) {
if ((SkadiMain.this.lockingSocket == null) || SkadiMain.this.lockingSocket.isClosed()) {
return;
}
e.printStackTrace();
}
}

}
}, "SkadiWakeupReceiver");

this.wakeupReceiverThread.start();
} catch (final SocketException e) {
try {
final DatagramSocket sendWakeupSocket = new DatagramSocket(0, InetAddress.getLoopbackAddress());

final DatagramPacket sendWakeupPacket = new DatagramPacket(SkadiMain.WAKEUP_SIGNATURE,
SkadiMain.WAKEUP_SIGNATURE.length, InetAddress.getLoopbackAddress(),
SkadiMain.SKADI_LOCKING_PORT);
sendWakeupSocket.send(sendWakeupPacket);
sendWakeupSocket.close();
} catch (final IOException e1) {
e1.printStackTrace();
}
}

return this.lockingSocket != null;
}

private void addShutdownHook() {
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

Expand All @@ -94,8 +165,14 @@ public void run() {
}
SkadiLogging.log("SAVING DATA..");
PersistenceManager.getInstance().saveData();
SkadiLogging.log("SHUTDOWN COMPLETE");

if (SkadiMain.this.lockingSocket != null) {
SkadiLogging.log("RELEASING LOCK..");
SkadiMain.this.wakeupReceiverThread.interrupt();
SkadiMain.this.lockingSocket.close();
}

SkadiLogging.log("SHUTDOWN COMPLETE");
}
}));
}
Expand Down

0 comments on commit b5d043c

Please sign in to comment.