1 package org
.asamk
.signal
.commands
;
3 import net
.sourceforge
.argparse4j
.impl
.Arguments
;
4 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
5 import net
.sourceforge
.argparse4j
.inf
.Subparser
;
7 import org
.asamk
.signal
.DbusConfig
;
8 import org
.asamk
.signal
.DbusReceiveMessageHandler
;
9 import org
.asamk
.signal
.JsonDbusReceiveMessageHandler
;
10 import org
.asamk
.signal
.OutputType
;
11 import org
.asamk
.signal
.dbus
.DbusSignalImpl
;
12 import org
.asamk
.signal
.manager
.Manager
;
13 import org
.freedesktop
.dbus
.connections
.impl
.DBusConnection
;
14 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
15 import org
.slf4j
.Logger
;
16 import org
.slf4j
.LoggerFactory
;
18 import java
.io
.IOException
;
19 import java
.util
.ArrayList
;
20 import java
.util
.List
;
21 import java
.util
.concurrent
.TimeUnit
;
23 public class DaemonCommand
implements MultiLocalCommand
{
25 private final static Logger logger
= LoggerFactory
.getLogger(ReceiveCommand
.class);
28 public void attachToSubparser(final Subparser subparser
) {
29 subparser
.addArgument("--system")
30 .action(Arguments
.storeTrue())
31 .help("Use DBus system bus instead of user bus.");
32 subparser
.addArgument("--ignore-attachments")
33 .help("Don’t download attachments of received messages.")
34 .action(Arguments
.storeTrue());
35 subparser
.addArgument("--json")
36 .help("WARNING: This parameter is now deprecated! Please use the global \"--output=json\" option instead.\n\nOutput received messages in json format, one json object per line.")
37 .action(Arguments
.storeTrue());
41 public int handleCommand(final Namespace ns
, final Manager m
) {
42 boolean inJson
= ns
.get("output") == OutputType
.JSON
|| ns
.getBoolean("json");
44 // TODO delete later when "json" variable is removed
45 if (ns
.getBoolean("json")) {
46 logger
.warn("\"--json\" option has been deprecated, please use the global \"--output=json\" instead.");
49 boolean ignoreAttachments
= ns
.getBoolean("ignore_attachments");
51 DBusConnection
.DBusBusType busType
;
52 if (ns
.getBoolean("system")) {
53 busType
= DBusConnection
.DBusBusType
.SYSTEM
;
55 busType
= DBusConnection
.DBusBusType
.SESSION
;
58 try (DBusConnection conn
= DBusConnection
.getConnection(busType
)) {
59 String objectPath
= DbusConfig
.getObjectPath();
60 Thread t
= run(conn
, objectPath
, m
, ignoreAttachments
, inJson
);
62 conn
.requestBusName(DbusConfig
.getBusname());
66 } catch (InterruptedException ignored
) {
69 } catch (DBusException
| IOException e
) {
70 logger
.error("Dbus command failed", e
);
76 public int handleCommand(final Namespace ns
, final List
<Manager
> managers
) {
77 boolean inJson
= ns
.get("output") == OutputType
.JSON
|| ns
.getBoolean("json");
79 // TODO delete later when "json" variable is removed
80 if (ns
.getBoolean("json")) {
81 logger
.warn("\"--json\" option has been deprecated, please use the global \"--output=json\" instead.");
84 boolean ignoreAttachments
= ns
.getBoolean("ignore_attachments");
86 DBusConnection
.DBusBusType busType
;
87 if (ns
.getBoolean("system")) {
88 busType
= DBusConnection
.DBusBusType
.SYSTEM
;
90 busType
= DBusConnection
.DBusBusType
.SESSION
;
93 try (DBusConnection conn
= DBusConnection
.getConnection(busType
)) {
94 List
<Thread
> receiveThreads
= new ArrayList
<>();
95 for (Manager m
: managers
) {
96 String objectPath
= DbusConfig
.getObjectPath(m
.getUsername());
97 Thread thread
= run(conn
, objectPath
, m
, ignoreAttachments
, inJson
);
98 receiveThreads
.add(thread
);
101 conn
.requestBusName(DbusConfig
.getBusname());
103 for (Thread t
: receiveThreads
) {
106 } catch (InterruptedException ignored
) {
110 } catch (DBusException
| IOException e
) {
111 logger
.error("Dbus command failed", e
);
117 DBusConnection conn
, String objectPath
, Manager m
, boolean ignoreAttachments
, boolean inJson
118 ) throws DBusException
{
119 conn
.exportObject(objectPath
, new DbusSignalImpl(m
));
121 final Thread thread
= new Thread(() -> {
129 ?
new JsonDbusReceiveMessageHandler(m
, conn
, objectPath
)
130 : new DbusReceiveMessageHandler(m
, conn
, objectPath
));
131 } catch (IOException e
) {
132 logger
.warn("Receiving messages failed, retrying", e
);
137 logger
.info("Exported dbus object: " + objectPath
);