Login  Register

Re: Is it possible to send a command to a running copy of ImageJ via a socket?

Posted by Michael P Ellis on Nov 04, 2020; 4:05pm
URL: http://imagej.273.s1.nabble.com/Is-it-possible-to-send-a-command-to-a-running-copy-of-ImageJ-via-a-socket-tp5024089p5024145.html

Many thanks to Stein Rørvik, Curtis Rueden, Albert Cardona fo your replies to my question!

Regards -- Michael Ellis


October 24, 2020 12:06 PM, "Albert Cardona" <[hidden email]> wrote:

> Hi Michael,
>
> There is an example of using sockets in TrakEM2:
>
> https://github.com/trakem2/TrakEM2/blob/master/src/main/java/ini/trakem2/utils/RedPhone.java
>
> When the UI freezes up, the automatically launched red phone enables the user via sockets to e.g.
> trigger a save command, or flush the cache, etc.
>
> It’s quite easy to setup.
>
> Hope this helps.
>
> Albert
>
>> On Oct 23, 2020, at 7:23 PM, Curtis Rueden <[hidden email]> wrote:
>>
>> Hi Michael,
>>
>>> Is it possible to send a command to a running copy of ImageJ via a socket?
>>
>> You could take a look at the ImageJ Server:
>> https://github.com/imagej/imagej-server
>>
>> It's a REST API.
>>
>> Regards,
>> Curtis
>>
>> --
>> Curtis Rueden
>> Software architect, LOCI/Eliceiri lab - https://loci.wisc.edu/software
>> ImageJ2 lead, Fiji maintainer - https://imagej.net/User:Rueden
>> Have you tried the Image.sc Forum? https://forum.image.sc
>>
>>> On Fri, Oct 23, 2020 at 6:21 AM Michael Ellis <[hidden email]>
>>> wrote:
>>>
>>> Hi and thanks for your reply.
>>>
>>> The problem with his approach for my use case is that I wish to open
>>> images in ImageJ from another Java application. I could use a
>>> java.lang.Runtime.exec(String command) but this would be messy
>>> necessitating different OS-specific command strings to constructed for each
>>> platform.
>>>
>>> Since ImageJ already can interpret various scripting languages (its own
>>> macro scripts and also javascript), it seems a shame that there is not just
>>> a socket one can send a piece of javascript or macro scripting language to
>>> drive ImageJ remotely.
>>>
>>> A nice benefit is that an ImageJ could take a command-line argument to
>>> determine whether the sockets accepted requests for local and or remote
>>> access thus making it possible to drive ImageJ remotely from a different
>>> computer.
>>>
>>> I might consider implementing this as a plugin if this functionality does
>>> not already exist. I have used this approach elsewhere. I just dod not want
>>> to start reinventing the wheel if ImageJ already had this.
>>>
>>> -- Michael Ellis
>>>
>>> October 23, 2020 1:30 PM, "Stein Rørvik" <[hidden email]> wrote:
>>
>> Can't you just open ImageJ via the OS, with the multiple instance
>>> listener active?
>>
>> Then subsequent commands will use the same instance.
>> You can use of the -port option to pass the command to one of several
>>> instances.
>>
>> The following works as expected in a Windows command batch file:
>>
>> start ImageJ.exe -eval "eval('script',
>>> 'IJ.getInstance().setTitle(\"ImageJ default\")');"
>>
>> start ImageJ.exe -port1 -eval "eval('script',
>>> 'IJ.getInstance().setTitle(\"ImageJ at port 1\")');"
>>
>> start ImageJ.exe -port2 -eval "eval('script',
>>> 'IJ.getInstance().setTitle(\"ImageJ at port 2\")');"
>>
>> timeout 10
>> start ImageJ.exe -eval "print('hello from ImageJ at the default port')"
>> start ImageJ.exe -port1 -eval "print('hello from ImageJ at port 1')"
>> start ImageJ.exe -port2 -eval "print('hello from ImageJ at port 2')"
>> timeout 3
>> start ImageJ.exe -eval "print('another hello from ImageJ the default
>>> port')"
>>
>> start ImageJ.exe -port1 -eval "print('another hello from ImageJ at port
>>> 1')"
>>
>> start ImageJ.exe -port2 -eval "print('another hello from ImageJ at port
>>> 2')"
>>
>> Here we have three instances running; I start each instance with a small
>>> JavaScript to rename the
>>
>> main window in order to easier tell the difference between them. Then we
>>> can pass commands,
>>
>> specifying which instance we want each of the commands to execute in.
>>> The "timeout 10" command
>>
>> inserts a 10 seconds delay to give sufficient time for all three
>>> instances of ImageJ to launch,
>>
>> before we start passing commands to each of them. Opening an image or
>>> running a macro in each
>>
>> instance will work the same way.
>>
>> I use the "start" command to launch ImageJ asynchronously, or else the
>>> Windows script would wait
>>
>> until each instance of ImageJ has quit. Since this runs asynchronously,
>>> we need to add an
>>
>> appropriate delay between subsequent commands to the same instance, to
>>> allow whatever command you
>>
>> passed to it to finish; in this case 3 seconds.
>>
>> Stein
>>
>> -----Original Message-----
>> From: ImageJ Interest Group <[hidden email]> On Behalf Of Michael
>>> Ellis
>>
>> Sent: 19. oktober 2020 14:37
>> To: [hidden email]
>> Subject: Is it possible to send a command to a running copy of ImageJ
>>> via a socket?
>>
>> Hello all.
>>
>> Is it possible to send a command to a running copy of ImageJ via a
>>> socket?
>>
>> The sort of thing I had in mind could be illustrated with the unix 'nc'
>>> command as follows:
>>
>> $ echo 'open /path/to/file/Meta1.tif' | nc localhost 57294
>>
>> I can see that ImageJ can use Socket ports to prevent multiple instances
>>> of ImageJ from running but
>>
>> I cannot see how to explicitly send a command via a Socket.
>>
>> My final use case is from my own standalone java application to be able
>>> to request an already
>>
>> running copy of ImageJ open an image file.
>>
>> If this is not possible is there already an ImageJ plugin that already
>>> written that does the job?
>>
>> As ever thanks in advance for any help.
>> Michael Ellis
>>
>> Digital Scientific UK Ltd
>>
>> --
>> ImageJ mailing list:
>>
>>> https://eur03.safelinks.protection.outlook.com/?url=http://imagej.nih.gov/ij/list.html&data=04|01|st
>>
>> [hidden email]
>>> |cd3da405f538449585dd08d8742bec55|e1f00f39604145b0b309e0210d8b32af|1|0|6373870792
>>
>>> 4701433|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0=|1000&sd
>>
>> ta=gQxlzYvUrFDJMgQqUKXnLjR1woFAOmpdxjl+oiiUJk4=&reserved=0
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>> --
>>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html