[Openchemistry-developers] RPC Listener?

Patrick Avery psavery at buffalo.edu
Tue Apr 4 18:21:11 EDT 2017


Here's a rundown of what I've learned and how to use it. It is used more
extensively in MoleQueue. Right now, in Avogadro2, it looks like you can do
two things: tell it to open a file, and send it a molecule in a byte stream
for it to read and load.

Basic QJsonObject to be sent:
{
  { "jsonrpc" : "2.0" },
  { "id" : idNum }
}

The other current keys are "method" and "params".

Current options for "method":

"openFile" - tells Avogadro2 to open a file
"loadMolecule" - send molecule data using any supported Avogadro2 format to
get Avogadro2 to set that as a new molecule

If the method is "openFile", then "params" needs to be set like this:
{
  { "fileName" : "thefilename" }
}

So the overall json looks like this:

{
  { "jsonrpc" : "2.0" },
  { "id" : idNum },
  { "method" : "openFile" },
  { "params" :
    {
      { "fileName" : "rutile.POSCAR" }
    }
  }
}

If the method is "loadMolecule", then "params" needs to be set like this:
{
  { "format" : "whateverFormat" },
  { "content" : "theData" }
}

So the overall json looks like this:

{
  { "jsonrpc" : "2.0" },
  { "id" : idNum },
  { "method" : "openFile" },
  { "params" :
    {
      { "format" : "POSCAR" },
      { "content" : "TiO2 rutile\n1.00000000\n2.95812000   0.00000000
0.00000000\n0.00000000   4.59373000   0.00000000\n0.00000000   0.00000000
4.59373000\nO   Ti\n4   2\nDirect\n0.00000000  0.30530000
 0.30530000\n0.00000000  0.69470000  0.69470000\n0.50000000  0.19470000
 0.80530000\n0.50000000  0.80530000  0.19470000\n0.00000000  0.00000000
 0.00000000\n0.50000000  0.50000000  0.50000000" }
    }
  }
}

With the QJsonObject type, you can initialize QJsonObjects with a standard
initializer like so:

QJsonObject jsonObject {
  { "jsonrpc", "2.0" },
  { "id", idNum }
};

Where you just use commas instead of colons.

To send and receive the data to the RPC, you need to use a QLocalSocket
object, and you can follow methods in this class here:
https://github.com/OpenChemistry/molequeue/blob/master/molequeue/client/jsonrpcclient.cpp

Basically, you have to connect the QLocalSocket like so:

QLocalSocket* socket = new QLocalSocket;
socket->connectToServer("serverName");

In our case, "serverName" is "avogadro".

You can create a QDataStream with the socket and send info to the socket
that way.

QDataStream stream(socket);
stream << jsonData;

When you receive a signal back from the RPC server, the local socket will
emit readyRead(), so if your class is a QObject, you can connect to it like
so:

 connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket()));

Right now, Avogadro just communicates back error messages (I think). But we
can eventually get it to send back other information too.

On Tue, Apr 4, 2017 at 5:40 PM, Geoffrey Hutchison <
geoff.hutchison at gmail.com> wrote:

> > I think I will be able to get this to work just fine. Thank you Marcus!
>
> If so, do you think you could take some notes on this? (e.g., what methods
> are available)
>
> Thanks,
> -Geoff
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/openchemistry-developers/attachments/20170404/4e3702db/attachment.html>


More information about the Openchemistry-developers mailing list