当前位置: 首页 > 软件库 > 手机/移动开发 > >

Near

授权协议 MIT License
开发语言 Java
所属分类 手机/移动开发
软件类型 开源软件
地区 不详
投 递 者 芮化
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Near

Near is a P2P library which allows

  • Discovery like Android NSD, though with greater reliability and easier-to-use NearDiscovery API
  • Transfers among clients through an easy-to-use NearConnect API

Sample Usage

Sample app, with the source code here is available on PlayStore.

NearDiscovery

NearDiscovery takes hostname and a bunch of settings in a builder pattern for the discovery mechanism. A NearDiscovery object allows the following discovery related self-explanatory APIs:

  • void makeDiscoverable(String hostName, String mustMatch /* optional */);
  • void makeNonDiscoverable();
  • void startDiscovery();
  • void stopDiscovery();
  • Set<Host> getAllAvailablePeers();
  • boolean isDiscoverable();
  • boolean isDiscovering();

Here's how the NearDiscovery object is created

private NearDiscovery mNearDiscovery = new NearDiscovery.Builder()
                .setContext(this)
                .setDiscoverableTimeoutMillis(DISCOVERABLE_TIMEOUT_MILLIS)
                .setDiscoveryTimeoutMillis(DISCOVERY_TIMEOUT_MILLIS)
                .setDiscoverablePingIntervalMillis(DISCOVERABLE_PING_INTERVAL_MILLIS)
                .setDiscoveryListener(getNearDiscoveryListener(), Looper.getMainLooper())
                .setPort(8989) // optional
                .setFilter(Regex("filter")) // optional, use with makeDiscoverable("hostName", "filter")
                .build();

The Looper passed as the 2nd param of NearDiscovery.Builder.setDiscoveryListener() is for the thread on which the listener, the 1st param, should be called.Sample listener:

@NonNull
    private NearDiscovery.Listener getNearDiscoveryListener() {
        return new NearDiscovery.Listener() {
            @Override
            public void onPeersUpdate(Set<Host> hosts) {
                // Handle updates of peer list here - some peer might have got removed if it wasn't reachable anymore or some new peer might have been added
            }

            @Override
            public void onDiscoveryTimeout() {
                // This is called after the discovery timeout (specified in the builder) from starting discovery using the startDiscovery()
            }

            @Override
            public void onDiscoveryFailure(Throwable e) {
                // This is called if discovery could not be started
            }

            @Override
            public void onDiscoverableTimeout() {
                // This is called after the discoverable timeout (specified in the builder) from becoming discoverable by others using the makeDiscoverable()
            }
        };
    }

NearDiscovery.Builder.setDiscoverablePingIntervalMillis() tells the interval at which each client broadcasts about its existence. The NearDiscovery.Listener.onPeersUpdate() gets called even if a peer is deemed stale, i.e. it's last broadcast received was more than twice the discoverable-ping-interval ago.

The discovery mechanism takes place in background services which do not hold any wakelocks.

NearConnect

A NearConnect object provides P2P mechanism with the following self-explanatory APIs:

  • long send(byte[] bytes, Host peer);
  • void startReceiving();
  • void stopReceiving(boolean abortCurrentTransfers);
  • Set<Host> getPeers();
  • boolean isReceiving();

NearConnect.startReceiving() only tells to start listening for any incoming transfers, similarly NearConnect.isReceiving() only tells if the client is listening for transfers or not and not if any data is currently being received.Here's how the NearConnect object is created:

private NearConnect mNearConnect = new NearConnect.Builder()
                .fromDiscovery(mNearDiscovery)
                .setContext(this)
                .setListener(getNearConnectListener(), Looper.getMainLooper())
                .setPort(8990) // optional
                .build();

The NearDiscovery object passed in NearConnect.Builder.fromDiscovery() is only to get the list of peers from. Peers can be explicitly provided as well:

private NearConnect mNearConnect = new NearConnect.Builder()
                .forPeers(peers) // Set<Host> peers
                .setContext(this)
                .setListener(getNearConnectListener(), Looper.getMainLooper())
                .setPort(8990) // optional
                .build();

Again, the NearConnect.Builder.setListener() takes the Listener as the 1st argument and the Looper on which to call the Listener as the 2nd argument. Here's what the Listener looks like:

@NonNull
    private NearConnect.Listener getNearConnectListener() {
        return new NearConnect.Listener() {
            @Override
            public void onReceive(byte[] bytes, final Host sender) {
            // Process incoming data here
            }

            @Override
            public void onSendComplete(long jobId) {
            // jobId is the same as the return value of NearConnect.send(), an approximate epoch time of the send
            }

            @Override
            public void onSendFailure(Throwable e, long jobId) {
            // handle failed sends here
            }

            @Override
            public void onStartListenFailure(Throwable e) {
            // This tells that the NearConnect.startReceiving() didn't go through properly.
            // Common cause would be that another instance of NearConnect is already listening and it's NearConnect.stopReceiving() needs to be called first
            }
        };
    }

It's required to stop listening on a previous instance of NearConnect (using NearConnect.stopReceiving()) so as to start listening on another instance (by using NearConnect.startReceiving()).This is because the same server port is used in each instance (<- could be made configurable later if deemed necessary).

on startReceiving(), and on send() partial wakelocks are held and released on stopReceiving() and on send completion/failure respectively.

Note: NearConnect should work even outside of a local network, except across NAT firewalls.

Getting Started

Add jitpack.io to your root build.gradle

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

Then add the dependency in your project build.gradle

dependencies {
    ...
    implementation 'com.github.adroitandroid:Near:v2.0'
    ...
}

You can find the latest version here.

Limitations

  • File transfers aren't easy yet. Services are background, API to take notification to start them in foreground, and listener methods to publish updates are on the TODO list.
  • Current Min SDK is 19. Tested with multiple devices and because of code limitation.

License

View full license here. In short:

The MIT License is a permissive license that is short and to the point. It lets people do anything they want with your code as long as they provide attribution back to you and don’t hold you liable.

  • 熟悉编程的通常会用到一些LP开头的数据类型来定义指针,比如以下VS里minwindef.h中的部分内容: typedef unsigned long DWORD; typedef int BOOL; typedef unsigned char BYTE; typedef unsigned short WORD; typedef fl

  • NEAR与以太坊 指南 / 作者 futured.near / 13 分钟阅读 推文 +4nLEARNs 今天,以太坊是区块链领域排名第一的智能合约平台,大多数智能合约和去中心化应用程序 (dApps) 都基于其区块链。 与以太坊一样,市场上还有其他几个智能合约平台。 然而,只是因为以太坊是智能合约和去中心化金融(DeFi)中排名第一的平台的話,并不意味着它没有问题。 以太坊存在一个明显的问题,那

  • near指针和far指针 在DOS下(实模式)地址是分段的,每一段的长度为64K字节,刚好是16位(二进制的十六位)。 near指针的长度是16位的,所以可指向的地址范围是64K字节,通常说near指针的寻址范围是64K。 far指针的长度是32位,含有一个16位的基地址和16位的偏移量,将基地址乘以16后再与偏移量相加,(所以实际上far指针是20位的长度。)即可得到far指针的1M字节的偏移量

 相关资料
  • 本文向大家介绍mysql报错:MySQL server version for the right syntax to use near type=InnoDB的解决方法,包括了mysql报错:MySQL server version for the right syntax to use near type=InnoDB的解决方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了mysql

  • 问题内容: 我无法使用猫鼬的语法查询mongodb数据库。 我有一个架构,其坐标存储为数组,索引为: 我正在使用运行猫鼬查询: 我已启用,并且可以看到生成的调试信息: 在集合中插入2个坐标为的文档: 在坐标附近搜索文档: 查询不返回 结果。 我通过搜索证明了文档已在数据库中。它是我无法使用的地理空间查询。 我正在使用和 完整的工作示例如下: 架构:(model.js) 摩卡测试:(test.ts)

  • 我最近在我的SQLite数据库中添加了一个列,由于添加了这个列,我有一个按钮,它将一个数据库列的值设置为“1”或“0”,当这个按钮尝试使用updateData()时,我的应用程序开始崩溃,所以我认为这是我的问题所在,是updateData()的代码或语法问题 显示错误= android.database.sqlite.sqliteException:near“WHERE”:语法错误(代码1 SQL

  • 本文向大家介绍ThinkPHP2.0读取MSSQL提示Incorrect syntax near the keyword 'AS'的解决方法,包括了ThinkPHP2.0读取MSSQL提示Incorrect syntax near the keyword 'AS'的解决方法的使用技巧和注意事项,需要的朋友参考一下 问题代码如下: 出现的情况是使用 query 可以正确读取到数据,而使用M方法,则无

  • 问题内容: This question already has answers here : Syntax error on print with Python 3 [duplicate] (3 answers) Closed 5 years ago. I want to round integers to the nearest 0.25 decimal value, like this: Th

  • 当使用near cache时,在缓存操作正在进行时,直到第二个客户端(可能是visor)尝试连接或断开到集群为止,所有的工作都很好。 我的问题是,这是意料之中的行为吗?我们是否可以使近缓存不被绕过,或者至少在坏客户端断开连接后使用近缓存重新建立。

  • 我们正在我们的应用程序中构建一个新的Hazelcast缓存服务器客户端模型。 我们使用的是开放JDK 14 Spring Boot-2.3.2 Hazelcast-3.12.8 服务器Hazelcast配置 客户端Hazel cast配置 要求-如果服务器和客户端都已启动并运行,则此配置可以完美工作。如果服务器关闭,Near将不工作,客户端也将被迫关闭。服务器关闭时,我们正在丢失事务消息。 即使服