Start, Stop, Enable, Disable a Systemd Service from C++ [duplicate] - c++

I have a .service for a process that i don't want to start at boot-time, but to call it somehow from another already running application, at a given time.
The other option would be to put a D-Bus (i'm using glib dbus in my apps ) service file in /usr/share/dbus-1/services and somehow call it from my application. Also, i don't manage to do this either.
Let's say that my dbus service file from /usr/share/dbus-1/services is com.callThis.service
and my main service file from /lib/systemd/system is com.startThis.service
If i run a simple introspect from command line:
/home/root # dbus-send --session --type=method_call --print-reply \
--dest=com.callThis /com/callThis org.freedesktop.DBus.Introspectable.Introspect
the D-Bus service file will get called and it will start what is in the Exec ( com.starThis ). The problem is that i want to achieve this from C/C++ code using D-Bus glib.

A combination of g_dbus_connection_send_message with g_dbus_message_new_method_call or g_dbus_message_new_signal should be what you are looking for.

I had trouble to do the same thing. The discover of : G_BUS_NAME_WATCHER_FLAGS_AUTO_START solve it.
g_bus_watch_name(G_BUS_TYPE_SYSTEM, "com.mydbus.listen",
G_BUS_NAME_WATCHER_FLAGS_AUTO_START, xOnNameAppeared, xOnNameVanished,
this, nullptr);

Related

How to use executable file within AWS lambda handler?

I want to use executable file within AWS lambda handler function.
Here is my handler function where I want to use executable
func handler(){
cmd := exec.Command("./balance", "GetBalance", id)
cmd.Dir = "/Users/xxxx/go/src/getBalance"
output, err := cmd.Output()
}
I want to use the output of the above command in this handler. Is it possible to use? If possible, do I need to zip both executables? Or is there any other way where I can use executable within the handler?
Sadly, you will not be able to write to /Users/xxxx/go/src/getBalance. In lambda, you have access only to /tmp.
Also, if you bundle the balance file with your deployment package it will be stored in /var/task alongside your function code.
EDIT:
Based on the new comments, to complete solution also required removal of cmd.Dir and recompilation of balance for linux.

multithreaded SimpleXMLRPCServer

Can someone give a small example of multi threaded SimpleXMLRPCServer. I tried googling around but none of the thins i found were what i needed. Most tell you to use some other library. I have a simple SimpleXMLRPCServer setup, but i don't know where do i add in the multi threading.
dumpServer just has pile of function that i want to call RPC manner. But now i need to add multi threading to it.
dump = dumpServer()
server_sock = ('127.0.0.1', 7777)
# Create XML_server
server = SimpleXMLRPCServer(server_sock,
requestHandler=DumpServerRequestHandler, allow_none=True)
server.register_introspection_functions()
# Register all functions of the Mboard instance
server.register_instance(dump)
How can i make it so that it can handle multiple clients simutaneously?

Using QFtp, abort list() command

I am actually wanting to get the newest files from a ftp server. For this, I am currently using QFtp to access the server and retrieve what I need.
This is what I do (like every 3 minutes) :
Connection & authentification to the server.
list() command to list all the files.
for each file listed by the list() command I call a slot that verify if the file currently listed has not been already downloaded (I am relying on the date of the file). If the file is recent enough, I download it.
So, it works. But it it really slow because there are thousands of files on the server and each time I verify the date of each of them. Is it possible to abort the list() command for example when I find a file too old ? Or is there another smarter way to fasten the process ?
Yes, there is a way to abort the long-playing command. When you call QFtp::list() it starts execution command on Ftp server, and if command finds an entry, QFtp emits QFtp::listInfo(const QUrlInfo &) signal. You can handle that signal, and check, whether the QUrlInfo::lastModified() returned time is too old. If yes, you can call QFtp::abort() function to abort the list command's execution on the server. Here is the sample code:
Establish connection to handle the ftp signals
connect(ftp, SIGNAL(listInfo(const QUrlInfo &)),
this, SLOT(onNewEntry(const QUrlInfo &)));
Implementation of the listInfo signal handling slot:
void MyFtp::onNewEntry(const QUrlInfo &url)
{
// If url.lastModified() is less than some time
// ftp->abort();
}

How to set Proxy Address using QProcessEnvironment on Linux?

I am stuck with a simple issue in Qt. I want set proxy address using Qt. The command to set proxy address
export http_proxy=http://wwgw.abcd.com:8080
works fine if passed by a terminal manually. but If the same command is run using QProcess, it fails without setting proxy. Even, I tried with QProcessEnvironment as
QProcess process_setupProxyServerUrl;
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
QString cmd = "http://wwgw.abcd.com:8080";
env.insert("HTTP_PROXY", cmd);
process_setupProxyServerUrl.setProcessEnvironment(env);
But this also fails in setting up proxy address. QProcessEnvironment is new for me. So may be i might be using it in wrong way.
In my application, I need to change the proxy address according to users choice (at run time).
Any way using Qt would be helpfull. Please provide some suggestions/ideas to resolve this issue.
Try something like that
QProcess process_setupProxyServerUrl;
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("HTTP_PROXY", "http://wwgw.abcd.com:8080");
process_setupProxyServerUrl.setProcessEnvironment(env);
Why did you use export ? This is just an executable, not the environment key

How to log SOAP messages which are sent by Metro stack

I'm trying to log my messages which are sent using a Metro stack into console.
Could not find any way.
Message logging to stdout (valid for METRO only!):
On the client
Java 5: Set system property
-Dcom.sun.xml.ws.transport.http.client.HttpTransportPipe.dump=true
Java 6: Set system property
-Dcom.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.dump=true
On the server side
Set system property
-Dcom.sun.xml.ws.transport.http.HttpAdapter.dump=true
Here everything is explained:
https://metro.java.net/2.0/guide/Logging.html
The following options enable logging of all communication to the console (technically, you only need one of these, but that depends on the libraries you use, so setting all four is safer option).
-Dcom.sun.xml.ws.transport.http.client.HttpTransportPipe.dump=true
-Dcom.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.dump=true
-Dcom.sun.xml.ws.transport.http.HttpAdapter.dump=true
-Dcom.sun.xml.internal.ws.transport.http.HttpAdapter.dump=true
Didn't mention the language but assuming Java, could you not just use something like Log4J e.g.
service = new Service();
port = service.getXxxPort();
result = port.doXxx(data);
Log.info("Result is " + result.getResult().toString());
where getResult is just a method on the return object.