B4J Library [B4J]jMQTTBroker v2 added the onConnect and onDisconnect for broker interceptor

For this and that reasons. I modified the library jMQTTBroker v2 and added raising onConnect and onDisconnect events for broker interceptor.

Broker example is from Erel's this code.

Library source code is attached.
 

Attachments

  • BrokerwithInterceptor.zip
    3.4 KB · Views: 68
  • jMqttBroker2ext.jar
    148.2 KB · Views: 101
  • jMqttBroker2ext.xml
    130.6 KB · Views: 74
  • Libs_MqttBroker2_ext_src.zip
    113.8 KB · Views: 73

Mike1970

Well-Known Member
Licensed User
Longtime User
AWESOME now the events actually triggers (I'm testing on Android).

Now I've to understand how to get the data from the InterceptMessages.
I found the class declarations here https://github.com/andsel/moquette/...c/main/java/io/moquette/interception/messages

But I do not know how to access the information.

I tried with:
B4X:
Private Sub Broker_Connect (Msg As JavaObject)
    Try
        Log("Connect: " & Msg.RunMethod("getClientID", Null))
    Catch
        Log(LastException)
    End Try
End Sub

But I got: java.lang.RuntimeException: Method: RunMethod not found in: io.moquette.interception.messages.InterceptConnectMessage


However, this is my limitation and I need to look deeper into it.
I wanted to answer to your post to thank you very much! I think it is a good starting point to a JMqttBroker v3
 
Last edited:

Mike1970

Well-Known Member
Licensed User
Longtime User
AWESOME now the events actually triggers (I'm testing on Android).

Now I've to understand how to get the data from the InterceptMessages.
I found the class declarations here https://github.com/andsel/moquette/...c/main/java/io/moquette/interception/messages

But I do not know how to access the information.

I tried with:
B4X:
Private Sub Broker_Connect (Msg As JavaObject)
    Try
        Log("Connect: " & Msg.RunMethod("getClientID", Null))
    Catch
        Log(LastException)
    End Try
End Sub

But I got: java.lang.RuntimeException: Method: RunMethod not found in: io.moquette.interception.messages.InterceptConnectMessage


However, this is my limitation and I need to look deeper into it.
I wanted to answer to your post to thank you very much! I think it is a good starting point to a JMqttBroker v3
Never mind.

I found the right syntax
B4X:
Private Sub Broker_Connect (Msg As Object)
    Dim jo As JavaObject = Msg
    Dim clientID As String = jo.RunMethod("getClientID", Null)
    Log("Client ID: " & clientID)
End Sub

Argument must be OBJECT not JAVAOBJECT
 

teddybear

Well-Known Member
Licensed User
AWESOME now the events actually triggers (I'm testing on Android).

Now I've to understand how to get the data from the InterceptMessages.
I found the class declarations here https://github.com/andsel/moquette/...c/main/java/io/moquette/interception/messages

But I do not know how to access the information.

I tried with:
B4X:
Private Sub Broker_Connect (Msg As JavaObject)
    Try
        Log("Connect: " & Msg.RunMethod("getClientID", Null))
    Catch
        Log(LastException)
    End Try
End Sub

But I got: java.lang.RuntimeException: Method: RunMethod not found in: io.moquette.interception.messages.InterceptConnectMessage


However, this is my limitation and I need to look deeper into it.
I wanted to answer to your post to thank you very much! I think it is a good starting point to a JMqttBroker v3
You need to cast the msg to javaobject.
B4X:
Private Sub Broker_Connect (Msg As JavaObject)
    Try
        Log("Connect: " & Msg.As(JavaObject).RunMethod("getClientID", Null))
    Catch
        Log(LastException)
    End Try
End Sub
 

Mike1970

Well-Known Member
Licensed User
Longtime User
You need to cast the msg to javaobject.
Yeah, I thought that changing the type directly in the argument declaration will end in auto-casting it.
But apparently it does not work as I supposed 😂
 
Last edited:

Mike1970

Well-Known Member
Licensed User
Longtime User
I attach an edited snippet (helped by ChatGPT) to get data from the Publish event (calling the .getPayload like .getClientID does not work)


Java Section:
#if Java
import io.moquette.interception.InterceptHandler;
import io.moquette.interception.messages.*;
import io.netty.buffer.ByteBuf;
import anywheresoftware.b4a.BA;

public static class MyHandler implements InterceptHandler {

    BA ba;

    public MyHandler(B4AClass parent) {
        this.ba = parent.getBA();
    }

    @Override
    public String getID() {
        return "handler";
    }

    @Override
    public Class<?>[] getInterceptedMessageTypes() {
        return ALL_MESSAGE_TYPES;
    }

    @Override
    public void onConnect(InterceptConnectMessage msg) {
        this.ba.raiseEventFromUI(this, "broker_connect", msg);
    }

    @Override
    public void onDisconnect(InterceptDisconnectMessage msg) {
        this.ba.raiseEventFromUI(this, "broker_disconnect", msg);
    }

    @Override
    public void onConnectionLost(InterceptConnectionLostMessage msg) {
        this.ba.raiseEventFromUI(this, "broker_connectionlost", msg);
    }

    @Override
    public void onPublish(InterceptPublishMessage msg) {
        String topic = msg.getTopicName();
        String clientId = msg.getClientID();

        ByteBuf buf = msg.getPayload();
        byte[] payload = new byte[buf.readableBytes()];
        int readerIndex = buf.readerIndex();
        buf.getBytes(readerIndex, payload);

        PublishEvent event = new PublishEvent(topic, payload, clientId);
        this.ba.raiseEventFromUI(this, "broker_publish", event);
    }

    @Override
    public void onSubscribe(InterceptSubscribeMessage msg) {
        this.ba.raiseEventFromUI(this, "broker_subscribe", msg);
    }

    @Override
    public void onUnsubscribe(InterceptUnsubscribeMessage msg) {
        this.ba.raiseEventFromUI(this, "broker_unsubscribe", msg);
    }

    @Override
    public void onMessageAcknowledged(InterceptAcknowledgedMessage msg) {
        this.ba.raiseEventFromUI(this, "broker_messageacknowledged", msg);
    }


    public static class PublishEvent {
        public String topic;
        public byte[] payload;
        public String clientId;

        public PublishEvent(String topic, byte[] payload, String clientId) {
            this.topic = topic;
            this.payload = payload;
            this.clientId = clientId;
        }
    }
}
#End If



Publish Event Example:
Private Sub Broker_Publish (msg As Object)
    Try
        Dim jo As JavaObject = msg
        Dim topic As String = jo.GetField("topic")
        Dim payload() As Byte = jo.GetField("payload")
        Dim clientId As String = jo.GetField("clientId")

        Log("Topic: " & topic)
        Log("ClientID: " & clientId)
       
        Dim payloadString As String = BytesToString(payload, 0, payload.Length, "UTF8")
        Log("Payload: " & payloadString)

    Catch
        Log(LastException)
    End Try
End Sub
 

teddybear

Well-Known Member
Licensed User
Another way to add the interceptor.
B4X:
    Broker.Initialize("broker", 51042)
    Broker.Start
    Dim server As JavaObject = Broker.As(JavaObject).GetField("server")
    Dim pck As String = GetType(Me) & "$MyHandler"
    Dim handler As JavaObject
    handler.InitializeNewInstance(pck, Array(Me))
    server.RunMethod("addInterceptHandler", Array(handler))
 
Top