All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bdware.client.EventSubscriberClient Maven / Gradle / Ivy

The newest version!
package org.bdware.client;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.bdware.client.ws.BaseClient;
import org.bdware.client.ws.Handler;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import org.zz.gmhelper.SM2KeyPair;
import org.zz.gmhelper.SM2Util;

import javax.websocket.ClientEndpointConfig;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

public class EventSubscriberClient implements Handler {
    BaseClient bc;

    // ws://localhost:8080/SCIDE/SCExecutor
    public EventSubscriberClient(String uri) {
        try {
            bc = new BaseClient(this);
            ContainerProvider.getWebSocketContainer()
                    .connectToServer(
                            bc, ClientEndpointConfig.Builder.create().build(), URI.create(uri));
        } catch (DeploymentException | IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void handle(JsonObject obj) {
        System.out.println("[EventClient] receive" + obj.toString());
    }

    public void waitForConnect() {
        while (!bc.connected()) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
//        Map setNodeID = new HashMap<>();
//        setNodeID.put("action", "setNodeID");
//        SM2KeyPair keyPair = SM2Util.generateSM2KeyPair(null);
//        setNodeID.put("id", keyPair.getPublicKeyStr());
//
//        byte[] sig;
//        try {
//            sig =
//                    SM2Util.sign(
//                            keyPair.getPrivateKeyParameter(), keyPair.getPublicKeyStr().getBytes());
//            setNodeID.put("signature", ByteUtils.toHexString(sig));
//
//        } catch (CryptoException e) {
//            e.printStackTrace();
//        }
//        setNodeID.put("nodeName", "[LiteClient_" + System.currentTimeMillis() + "]");
//        bc.sendText(new Gson().toJson(setNodeID));
    }

    public void subscribeEvent(String contractID, String topic) {
        Map msg = new HashMap<>();
        msg.put("action", "subEvent");
        msg.put("contractID", contractID);
        msg.put("topic", topic);
        bc.sendText(new Gson().toJson(msg));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy