Android Open Source - OHC-android Network






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 w ww  .  java  2s  .com
import android.content.Context;
import android.content.res.Resources;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.DatagramChannel;
import java.util.logging.Level;

import io.openhc.ohc.OHC;
import io.openhc.ohc.R;
import io.openhc.ohc.basestation.Basestation;
import io.openhc.ohc.skynet.transaction.Transaction_generator;

public class Network
{
  protected DatagramSocket socket;

  private Basestation station;

  private Receiver receiver;
  private int port_b_cast;

  public Network(Basestation bs) throws IOException
  {
    Resources resources = bs.get_resources();
    this.port_b_cast = resources.getInteger(R.integer.ohc_network_b_cast_port);
    OHC.logger.log(Level.INFO, "Broadcast port is " + port_b_cast);
    try
    {
      DatagramChannel channel = DatagramChannel.open();
      this.socket = channel.socket();
      //Neat trick: Setting port to 0 makes the socket pick a random unused port
      this.socket.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0));
      OHC.logger.log(Level.INFO, "Listening on port " + this.socket.getLocalPort());
    }
    catch(IOException ex)
    {
      OHC.logger.log(Level.SEVERE, "Couldn't create comm socket: " + ex.getMessage(), ex);
      throw ex;
    }
  }

  public static boolean find_basestation_lan(Context ctx, int port, Broadcaster.Broadcast_receiver receiver)
  {
    Transaction_generator gen = new Transaction_generator();
    JSONObject json = new JSONObject();
    try
    {
      json.put("method", "get_ip");
    }
    catch(JSONException ex)
    {
      //Can't happen, all prameters are static
      OHC.logger.log(Level.SEVERE, "This can't happen");
    }
    Transaction_generator.Transaction transaction = gen.generate_transaction(json);
    InetAddress broadcast_addr = Broadcaster.get_broadcast_address(ctx);
    if(broadcast_addr != null)
    {
      Broadcaster bc = new Broadcaster(broadcast_addr, port, receiver);
      bc.execute(transaction);
      return true;
    }
    return false;
  }

  public Receiver setup_receiver()
  {
    this.receiver = new Receiver(this.socket, this.station);
    return this.receiver;
  }

}




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