System Interfaces
NeuroKernel uses many technologies present in modern browsers and makes these features available as system interfaces to the developers via its extensive API. Some features are made available to tasks directly using their task container, and some require authorization from the Kernel and only available if the authorization given. Remote applications if not trusted will be denied to some of these interfaces by the Kernel.
1. Client Storage
Client storage is the persistent storage feature available from the browser client. NeuroKernel uses IndexDB by default for the persistent storage but fall backs to local storage if indexDB is not supported. If remote applications fail to request persistent storage access, a memory based implementation is used to offset operational functionality but nothing will persist in this case. The existence of persistent storage can be checked using the ISystem interface. Client storage can also store images via a sub interface called IPixmapStorage
available from IClientStorage
.
import com.neurokernel.client.*; import com.neurokernel.system.io.*; public class StorageExample extends NApplication { @Override public void main(int argc, String[] argv) { getClientStorage().add("Hello", "World", response -> { NFrame mainFrame = getMainFrame(); if(response.hasError()) { NMessageDialog.showError(mainFrame, "Hello already exists"); } else { new NLabel(mainFrame, "added " + response.getRequestData()); mainFrame.repaint(); } }); setVisible("Storage Interface")); } }
2. File System
Accessing file systems is very straight forward with NeuroKernel /OS. System can recognize which file system a file belongs to by its prefix. If the file starts with slash /
, then it is the default file system used by the system. If it starts with nfs:[name]/
, it is a mounted file system that may be living on a remote location. The default file system is just a memory mapped file system when an application is a unregistered remote application. Trusted remote applications will have access to shared temporary file system. This file system is also available for all registered and trusted applications. Users may copy and paste from default file system to temporary file system so that remote application may access it if desired.
import com.neurokernel.client.*; import com.neurokernel.system.io.*; import com.neurokernel.adapter.IActionListener; public class FileSystemExample extends NApplication { @Override public void main(int argc, String[] argv) { NFrame mainFrame = getMainFrame(); new NButton(mainFrame,"Get Date").addListener((IActionListener) e -> { IFileSystem fileSystem=myFile.getFileSystem(getSystem()); fileSystem.exists(new NFile("/myfile.txt"), response -> { if(response.hasError()) NMessageDialog.showError(mainFrame,"File does not exist"); else NMessageDialog.showInfo(mainFrame,"File exists"); }); }); setVisible("File System Interface"); } }
3. Terminal
NeuroKernel also offers an interface to input and output to a terminal. A terminal application is available with the default package which is used for terminal IO. The terminal access request is made with getTerminal
method which returns an ITerminal
interface object, and ITerminalListener
is used to manage the input and output operations. Application must be executed from the terminal command line in order to have terminal functionality. ITerminal
interface object is also passed to the listener methods for more convenient way of interaction.
import com.neurokernel.client.*; import com.neurokernel.system.io.*; import com.neurokernel.system.*; /** * This application must be run from NeuroKernel terminal */ public class TerminalExample extends NApplication { @Override public void main(int argc, String[] argv) { NLabel label=new NLabel(getMainFrame()); getTerminal(stdio -> { String input=stdio.readString(); if (input == null && stdio.getKeyCode() == 0) { stdio.writeString("Hello Console!"); stdio.setPrompt("password:", true); } else if (stdio.getKeyCode() > 0) { stdio.setPrompt("Command> "); } else { stdio.writeString("You have entered: " + input); if (input.equals("close")) { stdio.close(); } else { label.setText(input); //also sets the label text stdio.writeString("Press any key to continue...", NSystemConstants.TERMINAL_WAIT_KEY_INPUT); } } }); setVisible("Terminal Example",20,20,300,300); } }
4. CallBack Handler
Callbacks are important part of protocol based systems. There should be an automated interaction between application and presentation system. Callbacks can be used for this purpose. A callback can also be used to display or process a repeated job. It is possible to make a callback continuous that can be called constantly in a specified interval until it is stopped. ICallbackHandler
is the main system interface for this purpose. The delay interval is given as milliseconds.
import com.neurokernel.client.*; import com.neurokernel.system.*; public class CallBackExample extends NApplication { @Override public void main(int argc, String[] argv) { NConsolePane console = new NConsolePane(new NScrollView(getMainFrame()); getCallbackHandler().start(1000, () -> console.print("Hello Callback")); setVisible("Callback Interface"); } }
5. System Console
Modern web browsers have a console for information tracing purposes from the running scripts. NeuroKernel can utilize this console feature with a IConsole
system interface. C like text formatting can be achieved with printf
method which is also available from NTools
utility class for any kind of string formatting purpose. It does not have all the functionality of the C supplied version of the formatting.
import com.neurokernel.client.*; import com.neurokernel.client.adapter.*; public class HistoryExample extends NApplication { private int currentState; @Override public void main(int argc, String[] argv) { new NButton(getMainFrame(),"Print"). addListener((IActionListener) e -> getConsole().print("Hello "+(currentState++))); etVisible("History Interface"); } }
6. Geo Location
Geo location interface is only available when the application is run from an HTTPS secure domain. Browser will also ask permission to use the geo location. An example of how to use this IGeoLocation
system interface given below.
import com.neurokernel.client.*; import com.neurokernel.system.io.*; import com.neurokernel.client.adapter.*; public class GeoLocationExample extends NApplication { @Override public void main(int argc, String[] argv) { NFrame mainFrame = getMainFrame(); new NButton(mainFrame,"GeoLocation").addListener((IActionListener) e -> { getSystem().getGeoLocation().addPositionListener((IGeoPositionListener) position -> NMessageDialog.showInfo(mainFrame, position.getLongitude()+" "+position.getLatitude()); }); }); setVisible("Geo Location Interface"); } }
7. Game Pad
NeuroKernel has support for gamepad interface available in modern browsers. IGamepadHandler
is the main system interface to access to the gamepad features. IGamepad
is on the other hand is used to process the events sent from the gamepad control. Gamepad handler can be accessed from the IWindowContext
interface. When a new gamepad is connected to the computer, system makes the gamepad ready. It then sends the actions on gamepad in an event package to the currently focused task window. Gamepad interface is especially useful for game developers.
8. Shared Data
Shared data access is available to all trusted applications. It is important not to use easy to guess shared data entries for important data if remote applications are utilized. Shared data entries can be created as read only which cannot be overwritten by other tasks.
import com.neurokernel.client.*; import com.neurokernel.system.io.*; public class SharedDataExample extends NApplication { @Override public void main(int argc, String[] argv) { NFrame mainFrame = getMainFrame(); ISharedData sharedData=getSharedDataHandler(); sharedData.write("name","NeuroKernel OS",true); new NButton(mainFrame,"Read"). addListener(e -> sharedData.read("name", response -> NMessageDialog.showInfo(mainFrame, response.readString())), NEventTypes.ACTION); setVisible("Shared Data Interface"); } }
9. Creating Sound Effects
NeuroKernel allows developers to create custom sound effects using IAudioGenerator
interface. The created custom sound can be cached to the display server.
import com.neurokernel.client.*; import com.neurokernel.system.media.IAudioGenerator; import com.neurokernel.client.adapter.IActionListener; public class MyApplication extends NApplication { @Override public void main(int argc, String[] argv) { new NButton(getMainFrame(),"Play"). addListener((IActionListener) e -> { IAudioGenerator audio = getSystem().getAudioGenerator(); audio.setWaveType(WaveType.SINE); audio.setEnvelopeSustainTime(0.31718502829007483); audio.setEnvelopeDecayTime(0.2718540993592685); audio.setStartFrequency(0.26126191208337196); audio.setFrequencySlide(0.43787689856926615); audio.setDutyCycle(1); audio.setRetriggerRate(0.7558565452384385); audio.setVolume(25); audio.setSampleRate(44100); audio.setSampleSize(8); audio.play(); }); setVisible("Audio Generator Demo"); } }
10. Printing
Printing in NeuroKernel is integrated into the printing functionality of the web browser itself. NeuroKernel API gives support on paper sizes and other printing related features. System allows developers to print a window or single component. PDF Viewer can also be used for printing purposes. PDF Viewer component does not use browser native pdf viewer, and it is based on pdf.js library for better programmatic control. The printContent
method is available to all components and containers. Some containers may have more specific printing options.
import com.neurokernel.client.*; public class HelloWorld extends NApplication { @Override public void main(int argc, String[] argv) { NFrame mainFrame=getMainFrame(); new NButton(mainFrame,"Print"). addListener(e -> mainFrame.printContent(true), NEventTypes.ACTION); mainFrame.setTitle("Print Window"); mainFrame.setBounds(20,20,200,200); mainFrame.setVisible(true); } }
11. History
Web browser history feature is accessible using IHistory
interface. Applications can push or pop history states which may be triggered using the back and forward buttons of the browser. It may useful for many practical purposes. This interface is only available to desktop managers.
import com.neurokernel.client.*; import com.neurokernel.system.*; import com.neurokernel.system.desktop.*; public class HistoryExample extends NDesktopManager { private int currentState; @Override public void main(int argc, String[] argv) { NFrame mainFrame = getMainFrame(); getHistoryManager().addHistoryListener((HistoryListener) id -> NMessageDialog.showInfo(mainFrame,"History State:"+ id)); new NButton(mainFrame,"Push State").addListener(e -> history.pushState("State"+(currentState++)), NEventTypes.ACTION); setVisible("History Interface"); } }