B4J Question Library event does not fire

madru

Active Member
Licensed User
Longtime User
Hi,

I need a helping hand, can somebody explain why the event does not fire?

THX


B4X:
package com.socket;

import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.*;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


@Events(values={"packetreceived (message() As Byte,ipaddress as String,len as int,Myip as string)"})

@Version(0.2f)
@ShortName("Multicast")
public class Wrapper {

  private  BA baevent;
  private  String _eventName;
  private  MulticastSocket _socket;
  private  ExecutorService _executor = Executors.newSingleThreadExecutor();
  private volatile boolean _shouldReceive = true;

  public void initialize(final BA ba, final String eventName, final int port, final String host) throws Exception {
   baevent = ba;


    final InetAddress ip = InetAddress.getByName(host);
    _socket = new MulticastSocket(port);
    _socket.joinGroup(ip);

    _executor.execute(this::receiveLoop);
  }

  public void close() throws IOException {
    try {
      _shouldReceive = false;
      _executor.shutdownNow();
    } finally {
      _socket.close();
    }
  }


  private void receiveLoop() {
    try {
      final byte[] receiveData = new byte[188]; // Get one TS packet from stream
      final DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
      while (_shouldReceive && !Thread.interrupted()) {
        _socket.receive(receivePacket);
        if (receivePacket.getLength() > 0) {
            final String sourceHost = receivePacket.getAddress().getHostAddress();
            //System.out.println("message: " + new String(receivePacket.getData(), StandardCharsets.UTF_8));
            baevent.raiseEventFromDifferentThread(this, null, 0, _eventName + "_packetreceived", false, new Object[]{receivePacket.getData(), sourceHost, receivePacket.getLength() });
        }
      }
    } catch (final IOException e) {
      e.printStackTrace();
    }
  }
}
 
Last edited:

madru

Active Member
Licensed User
Longtime User
Hi Erel,

thank you, for the suggestion but didn't help.....






B4X:
package anywheresoftware.b4a.objects;

import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.*;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Events(values={"packetreceived (message() As Byte,ipaddress as String,len as int,Myip as string)"})

@Version(0.4f)
@ShortName("Multicast")
public class Wrapper {

  private BA ba;
  private String eventName;
  private MulticastSocket _socket;
  private ExecutorService _executor = Executors.newSingleThreadExecutor();
  private volatile boolean _shouldReceive = true;
  private boolean SocketInit = false;

  public void initialize(final BA ba, final String _eventName, final int port, final String host) throws Exception {
     
    this.eventName = _eventName.toLowerCase(BA.cul);
    this.ba = ba; 

    SocketInit = true;
    final InetAddress ip = InetAddress.getByName(host);
    _socket = new MulticastSocket(port);
    _socket.joinGroup(ip);
    _executor.execute(this::receiveLoop);
  }

  public void close() throws IOException {
    try {
      _shouldReceive = false;
      _executor.shutdownNow();
    } finally {
    _socket.close(); //will throw exception
    }
  }


  private void receiveLoop() {
    try {
      final byte[] receiveData = new byte[188]; // Get one TS packet from stream
      final DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
      while (_shouldReceive && !Thread.interrupted()) {
        _socket.receive(receivePacket);
        if (receivePacket.getLength() > 0) {
            final String sourceHost = receivePacket.getAddress().getHostAddress();
            //System.out.println("message: " + new String(receivePacket.getData(), StandardCharsets.UTF_8));
            ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_packetreceived", false, new Object[]{receivePacket.getData(), sourceHost, receivePacket.getLength() });
        }
      }
    } catch (final IOException e) {
      e.printStackTrace();
    }
  }
      public boolean IsInitialized()
    {
        return SocketInit;
    }
   
}
 
Upvote 0

madru

Active Member
Licensed User
Longtime User
mmh, good point - but no idea what the meaning is

Program started.
message: MCastTester 1234567890!@#$%^&*()_+

An error occurred:
(Line: 0) null
java.lang.Exception: Sub udp_packetreceived signature does not match expected signature.
public static anywheresoftware.b4a.pc.RemoteObject b4j.example.main_subs_0._udp_packetreceived(anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject) throws java.lang.Exception
 
Upvote 0

jahswant

Well-Known Member
Licensed User
Longtime User
mmh, good point - but no idea what the meaning is

Program started.
message: MCastTester 1234567890!@#$%^&*()_+

An error occurred:
(Line: 0) null
java.lang.Exception: Sub udp_packetreceived signature does not match expected signature.
public static anywheresoftware.b4a.pc.RemoteObject b4j.example.main_subs_0._udp_packetreceived(anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject) throws java.lang.Exception

Your signature does not match expected signature. You should pass exactly same params and same types in B4J As in your java library.
 
Upvote 0

madru

Active Member
Licensed User
Longtime User
oh oh, must be blind :oops:

sorry for this, I really have overseen that the return does not match the event declaration

thank you all
 
Last edited:
Upvote 0
Top