Main Page | Features | Central Services | csv-Files | Types | Transfer | Access | API-C | API-.NET | API-Java | Examples | Downloads
page generated on 05.05.2024 - 04:45
A Simple TINE Java Client (console)

Introduction

TINE console client applications play an important role in middle-layer servers, automatic servers, scripts, diagnostic checks, etc. Client applications requiring a user interface are best written as GUI applications.

The example below is a simple java client code module which obtains the sine curve from the simple console server example, and illustrates both synchronous and asynchronous calls.

Step 1 Create a SineClient class

Create a new java project and make sure that tine.jar is included in the library list on otherwise on the class path. In this example, we'll assume this is a java project in Eclipse called "SINE-console", where a package "tineSineClientExample" has been created with a class called "SineClient".

In the SineClient.java code, make sure you add "import TINE.*;" underneath the package declaration.

Add the String variable 'devSrvName' and int variable 'size' to the class, initializing 'devSrvName' to the device server name of your test sine server and 'size' to 1024. Now add the float arrays sdat and refdat, dimensioned to size; Also add an embedded class 'cdSineClient' which implements the TLinkCallback class. The callback class will simply print out the first 10 values of the sine wave (minus the initial reference). Your SineClient.java code should now look like the following:

/*
* Created on Apr 8, 2004
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package tineSineClientExample;
import de.desy.tine.client.*;
import de.desy.tine.dataUtils.*;
import de.desy.tine.definitions.*;
import de.desy.tine.queryUtils.*;
import de.desy.tine.structUtils.*;
import de.desy.tine.types.*;
public class SineClient
{
private static String devSrvName = new String("/TEST/SINE");
private static int size = 1024;
private float[] sdat = new float[size];
private float[] refdat = new float[size];
public class cdSineClient implements TLinkCallback
{
public void callback(TLink lnk)
{
if (lnk.getLinkStatus() != 0)
{
System.out.println("Link Error : " + lnk.getLastError());
return;
}
for (int i=0; i<size && i<10; i++) System.out.print("" + (sdat[i] - refdat[i]));
System.out.println("");
}
}
public static void main(String[] args)
{
}
}

Step 2 Initialization

Add an initialize() method which obtains a 'reference' curve and starts an asynchronous link to get the sine wave from the sine server. The details are shown below:

public class SineClient
{
private static String devSrvName = new String("/TEST/SINEWAVE");
private static int size = 1024;
private float[] sdat = new float[size];
private float[] refdat = new float[size];
public class cdSineClient implements TLinkCallback
{
public void callback(TLink lnk)
{
if (lnk.getLinkStatus() != 0)
{
System.out.println("Link Error : " + lnk.getLastError());
return;
}
for (int i=0; i<size && i<10; i++) System.out.print("" + (sdat[i] - refdat[i]));
System.out.println("");
}
}
public int initialize()
{
int cc = 0;
String devname = new String(devSrvName);
devname = devname.concat("/#1");
TDataType dout = new TDataType(refdat);
// get a reference array : synchronous call ...
TLink ref = new TLink(devname,"SINE",dout,null,TAccess.CA_READ);
if ((cc=ref.execute()) != 0)
{
System.out.println("get reference : " + ref.getLinkStatus());
}
ref.cancel();
dout = new TDataType(sdat);
TLink sin = new TLink(devname,"SINE",dout,null,TAccess.CA_READ);
cdSineClient showsine = new cdSineClient();
if (sin.attach(TMode.CM_POLL, showsine, 1000) < 0)
{
System.out.println("attach : " + sin.getLinkStatus());
}
return cc;
}
public static void main(String[] args)
{
}
}

Note that after using the 'ref' link to synchronously obtain a 'reference' curve, we have terminated it explicitily by calling 'ref.cancel()'. If an application needs to make intermittent synchronous calls to the same link it would be better NOT to terminate the link reference and instead to issue 'execute()' calls when needed.

The callback (showsine()) is used as an asynchronous callback. Note that this is just an example. In general, displaying 1024 float values on the console at 1 Hz is a bad idea!

Step 3 Running the program

In the main method, you can create an instance of the SineClient class, call the initialization routine, and then block the thread from exiting by calling sleep, as shown below.

public static void main(String[] args)
{
SineClient sc = new SineClient();
sc.initialize();
try
{
Thread.currentThread().sleep(10000);
}
catch (InterruptedException e) {};
System.exit(0);
}

Here, the thread is blocked for 10 seconds before exiting, as a way of demonstrating this simple client program. There are several alternatives to this coding style. Namely, you could also put the code contained in the initialize() method directly in the main method and forgo the need to create an instance of the SineClient class. You can also block the thread for exiting forever, etc.

When you run the application, make sure the the virtual machine definition -Dtine.home is set (e.g -Dtine.home=L:\database or -Dtine.home=/usr/etc/tine), or that a properties file tine.properties containing this information is located in the same directory where tine.jar is located.


Impressum   |   Imprint   |   Datenschutzerklaerung   |   Data Privacy Policy   |   Declaration of Accessibility   |   Erklaerung zur Barrierefreiheit
Generated for TINE API by  doxygen 1.5.8