User:Oleg Kobchenko/Interactive Task

From J Wiki
Jump to navigation Jump to search

Wm yes check.png File:Itask.ijs (sample session)

The task script in J base library provides operations to launch windowed or command line applications from J in Windows and communicate with them using stdin/stdout. However, they are limited to single exchange: one whole input and one whole output.

Interactive command line applications require a session of multiple exchanges between the parent and the child processes. To maintain necessary handles, a stateful approach is needed, thus we choose object-oriented implementation using jtask class, i.e. an extension to the same task script that allows creating a task object and using it to communicate with the child.

Destroying the object closes the pipes and terminates the child process, if it is still running.

If buffers are not processed before new read or write operations, deadlock happens, which can only be resolved by killing the child in Task Manager. In order to avoid deadlock

  • wait or peek before reading
  • limit amount of written block to capacity of the child
  • maintain protocol, e.g. write only after reading prompt, such as ready

Sample J session,

   require 'task'

   p=. 'jtask'conew~ 'jconsole'

   wait__p 1
3
   '[',']',~read__p ''
[   ]

   write__p 'i.3 4',LF
1
   wait__p 1
36
   '[',']',~read__p ''
[0 1  2  3
4 5  6  7
8 9 10 11
   ]

   write__p '+/i.3 4',LF
1
   wait__p 1
16
   '[',']',~read__p ''
[12 15 18 21
   ]

   write__p 'exit$0',LF
1
   wait__p 1

   '[',']',~read__p ''
[]

   destroy__p''

Sample chess session

     p=. 'jtask'conew~ 'cmd /c arasanx.exe';'D:\Tools\Arasan'
     write__p 'help',LF
1
     wait__p 1
1459
     read__p ''
Arasan 8.4 Copyright 1997-2005 by Jon Dart
Using book file book.bin
analyze:         enter Winboard analyze mode
black:           set computer to play Black
...

     write__p 'e2e4',LF
1
     wait__p 3
22
     read__p ''
1. e2e4
1. ... c7c5

     write__p ;,&LF each;:'Nf3 d4 Nxd4 Nc3 Be2 f4'
1
     wait__p 3
124
     read__p ''
2. Nf3
2. ... d7d6
3. d4
3. ... c5d4
4. Nxd4
4. ... g8f6
5. Nc3
5. ... e7e6
6. Be2
6. ... f8e7
7. f4
7. ... o-o

   NB.  write__p 'quit',LF
     close__p''
0
     wait__p 3
6
     read__p ''
quit

     destroy__p''