Android Open Source - OHC-android Receiver






From Project

Back to project page OHC-android.

License

The source code is released under:

MIT License

If you think the Android project OHC-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package io.openhc.ohc.skynet;
//from   ww  w .ja v a 2  s .  com
import android.os.AsyncTask;

import org.json.JSONObject;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.logging.Level;

import io.openhc.ohc.OHC;
import io.openhc.ohc.basestation.Basestation;

/*Using an async task to get received data transfered to the UI thread without
   having to care about synchronisation manually*/
public class Receiver extends AsyncTask<Void, JSONObject, Void>
{
  private DatagramSocket socket;
  private Basestation basestation;

  public Receiver(DatagramSocket socket, Basestation base)
  {
    this.socket = socket;
    this.basestation = base;
  }

  public Void doInBackground(Void... args)
  {
    while(true)
    {
      byte[] data = new byte[1024];
      DatagramPacket packet = new DatagramPacket(data, data.length);
      try
      {
        this.socket.receive(packet);
        String jsonStr = new String(packet.getData(), "UTF-8");
        OHC.logger.log(Level.INFO, "Packet received: " + jsonStr);
        JSONObject json = new JSONObject(jsonStr);
        //Publish received data to UI thread via progress notification
        this.publishProgress(json);
      }
      catch(Exception ex)
      {
        OHC.logger.log(Level.WARNING, "Error receiving data on udp socket: " + ex.toString());
        break;
      }
    }
    return null;
  }

  //This function is synchronized to the UI thread
  @Override
  public void onProgressUpdate(JSONObject... args)
  {
    this.basestation.handle_packet(args[0]);
  }

  public int get_port()
  {
    return this.socket.getLocalPort();
  }
}




Java Source Code List

io.openhc.ohc.ApplicationTest.java
io.openhc.ohc.OHC.java
io.openhc.ohc.OHC_ui.java
io.openhc.ohc.basestation.Basestation.java
io.openhc.ohc.basestation.device.Device.java
io.openhc.ohc.basestation.device.Field.java
io.openhc.ohc.basestation.rpc.Base_rpc.java
io.openhc.ohc.logging.OHC_Logger.java
io.openhc.ohc.skynet.Broadcaster.java
io.openhc.ohc.skynet.Network.java
io.openhc.ohc.skynet.Receiver.java
io.openhc.ohc.skynet.Sender.java
io.openhc.ohc.skynet.Socket_timeout.java
io.openhc.ohc.skynet.transaction.Transaction_generator.java
io.openhc.ohc.ui.Field_adapter.java
io.openhc.ohc.ui.input.filter.Input_filter_float.java
io.openhc.ohc.ui.input.filter.Input_filter_int.java
io.openhc.ohc.ui.input.filter.Input_filter_string.java