Ressources
Kalima Blockchain APIs and Examples
The Kalima Embedded Blockchain provides open Apis, or application programming interface, to enable users to make applications tailored to their needs and that allow them to collect, tansport and share trusted data's to services, machines and to mobile or users.
Here is a simple example of use
The application sends 10 messages "hello x" in the channel "sensors" with the unique id "/key" As the unique id remains the same, "hello 1" will be overwritten by "hello2" in memcache and so on. However all values will persist in blockchain.
for(int i=0 ; i<10 ; i++) {
String body = "hello" + sequence;
KMsg kMsg = new KMsg(sequence);
node.sendToNotaryNodes(kMsg.getMessage(devId,KMessage.PUB,
"sensors", "/key", body.getBytes(), new KProps("-1")));
sequence++ ;
}
Smart contracts creation
Example of a client running smart contracts public void initComponents :public void initComponents(){
byte[] key = new byte[] {
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
};
devId=KKeyStore.setDevId(clonePreferences.getLoadConfig().getFilesPath(), "devID", key, logger);
node = new Node(clonePreferences.getLoadConfig()); node.setDevID(devId);
clone = new Clone(clonePreferences, node);
serverCallBack = new KalimaServerCallBack(this);
clientCallBack = new KalimaClientCallBack(this);
try {
node.connect(serverCallBack, clientCallBack);
contractManager.initContracts(service_Contracts, clonePreferences.getContractsPropsFileName(), this);
contractManager.runReqContract();
} catch (IOException e) {
logger.log_srvMsg("Kalima2Auth", "Client", Logger.ERR, "initComponents initNode failed : " + e.getMessage());
}
}
Smart contracts creation with Javascript
Example of smart contracts using JAVASCRIPT :
importPackage(Packages.java.io);
importPackage(Packages.java.lang);
importPackage(Packages.java.util);
var trk = trackDocument;
var userCenterCode = trk.getUserCenter().getUserCenterCode();
var cptLigne = trk.getUserCenter().sizeOfLineArray();
for (var i=0; i< cptLigne; i=i+1){
var assemblyLine=trk.getUserCenter().getLineList().get(i).getAssemblyLine();
var cptGatePoint=trk.getUserCenter().getLineList().get(i).getGatePointList().size();
var ligne ="";
for (var j=0; j< cptGatePoint; j=j+1) {
var production ="";
var cptProductionSequence=trk.getUserCenter().getLineList().get(i).getGatePointList().get(j).getProductionSequenceList().size();
for(var k=0; k< cptProductionSequence; k=k+1) {
varsequ =trk.getUserCenter().getLineList().get(i).getGatePointList().get(j).getProductionSequenceList().get(k); writeLigneMage.setDateMessage(sequ.getSequenceStartDate()); writeLigneMage.setDayOfYear(sequ.getManufacturingReference().getAssemblyID().getDayOfYear()); writeLigneMage.setSequenceDay(sequ.getManufacturingReference().getAssemblyID().getSequenceOfDay());
production = production + writeLigneMage.buildLigneMage() ;
}
ligne=ligne+"\t"+writeLigneMage.setManufacturingAreaID(trk.getUserCenter().getLineList().get(i).getGatePointList().get(j).getManufacturingAreaID())+production ; }
var keySauv = "/client/" + assemblyLine;
kvmsg.setBody(toUTF8Array("L"+assemblyLine+ligne));
clonecli.set("track", keySauv, kryoSerializer.serialize(kvmsg), -2);
}
result = "OK";
Smart contracts creation for Android
Example of java smart contract in Android
final EditText editText = findViewById(R.id.edit_send);
Button testSend = findViewById(R.id.test_send);
testSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
// send message in cachePath /test with key "key"
kalimaServiceAPI.set("/test", "key", editText.getText().toString().getBytes(), "-1");
editText.setText("");
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
#Un client Android un callback défini les actions a exécuter sur réception d’un message : kalimaCacheCallback = new KalimaCacheCallback.Stub()
{
@Override
public void onEntryUpdated(String s, KMsgParcelable kMsgParcelable) throws RemoteException {
Log.d("onEntryUpdated", "cachePath=" + s + ", key=" + kMsgParcelable.getKey());
}
@Override
public void onEntryDeleted(String s, String s1) throws RemoteException {
Log.d("onEntryDeleted", "cachePath=" + s + ", key=" + s1);
}
@Override
public void onConnectionChanged(int i) throws RemoteException {
}
};
Communication REST in HTTP with CURL
Example of Kalima PUT and GET calls from CURL :
curl --location --request PUT 'https://kloner.io/?action=add&cachePath=/alarmes/incendies&key=test1' \ --header 'Content-Type: application/json' \ --data-urlencode '{"id":"test1","value":5}'
curl --location --request GET 'https://kloner.io/?action=get&cachePath=/alarmes/incendies&key=test1'
Communication REST in HTTP with Java
Example with the GET method in Java
URL obj = new URL("https://kloner.io/?action=get&cachePath=/alarmes/incendies&key=test1");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
nt responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) { // success BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) { response.append(inputLine);
}
in.close();
Communication REST in HTTP with Java
Example with the PUT method in Java
URL obj = new URL("https://kloner.io/?action=add&cachePath=/alarmes/incendies&key=test1");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("PUT");
requestProperties.entrySet().forEach(entry->{
con.setRequestProperty(entry.getKey(), entry.getValue());
});
con.setUseCaches(false);
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write("{"id":"test1","value":5}");
os.flush();
os.close();
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_ACCEPTED) { //success
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();