Skip to content

Commit

Permalink
Merge pull request #19 from dilanSachi/main
Browse files Browse the repository at this point in the history
Add mutual SSL support & improve test coverage
  • Loading branch information
dilanSachi authored Jul 19, 2023
2 parents 28a99a5 + dbc80d0 commit c12816a
Show file tree
Hide file tree
Showing 32 changed files with 1,249 additions and 223 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/build-timestamped-master.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Build

on:
push:
branches:
- main
paths-ignore:
- '*.md'
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
if: github.repository_owner == 'xlibb'
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
distribution: 'temurin'
java-version: 11
- name: Change to Timestamped Version
run: |
startTime=$(TZ="Asia/Kolkata" date +'%Y%m%d-%H%M00')
latestCommit=$(git log -n 1 --pretty=format:"%h")
VERSION=$((grep -w 'version' | cut -d= -f2) < gradle.properties | rev | cut --complement -d- -f1 | rev)
updatedVersion=$VERSION-$startTime-$latestCommit
echo $updatedVersion
sed -i "s/version=\(.*\)/version=$updatedVersion/g" gradle.properties
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
env:
packageUser: ${{ secrets.BALLERINA_BOT_USERNAME }}
packagePAT: ${{ secrets.BALLERINA_BOT_TOKEN }}
publishUser: ${{ secrets.BALLERINA_BOT_USERNAME }}
publishPAT: ${{ secrets.BALLERINA_BOT_TOKEN }}
run: |
./gradlew publish --scan --no-daemon
- name: Generate Codecov Report
uses: codecov/codecov-action@v3
- name: Upload Artifact
uses: actions/upload-artifact@v2
with:
name: ballerina-runtime
path: target/ballerina-runtime/
39 changes: 39 additions & 0 deletions .github/workflows/build-with-bal-test-graalvm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: GraalVM Check

on:
workflow_dispatch:
inputs:
lang_tag:
description: Branch/Release Tag of the Ballerina Lang
required: true
default: master
lang_version:
description: Ballerina Lang Version (If given ballerina lang buid will be skipped)
required: false
default: ''
native_image_options:
description: Default native-image options
required: false
default: ''
schedule:
- cron: '30 18 * * *'
pull_request:
branches:
- master
types: [opened, synchronize, reopened, labeled, unlabeled]

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }}
cancel-in-progress: true

jobs:
call_stdlib_workflow:
name: Run StdLib Workflow
if: ${{ github.event_name != 'schedule' || (github.event_name == 'schedule' && github.repository_owner == 'ballerina-platform') }}
uses: ballerina-platform/ballerina-standard-library/.github/workflows/build-with-bal-test-graalvm-template.yml@main
with:
lang_tag: ${{ inputs.lang_tag }}
lang_version: ${{ inputs.lang_version }}
native_image_options: ${{ inputs.native_image_options }}
additional_ubuntu_build_flags: ''
additional_windows_build_flags: '-x test'
33 changes: 33 additions & 0 deletions .github/workflows/daily-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Daily build

on:
repository_dispatch:
types:
check_connector_for_breaking_changes

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Set up JDK 11
uses: actions/setup-java@v2
with:
distribution: 'temurin'
java-version: 11

- name: Set environment variable
if: github.event.action == 'check_connector_for_breaking_changes'
run: |
echo "BUILD_USING_DOCKER=-PbuildUsingDocker=nightly" >> $GITHUB_ENV
echo "GRADLE_SKIP_TASKS=" >> $GITHUB_ENV
# Build the project with Gradle
- name: Build with Gradle
env:
packageUser: ${{ github.actor }}
packagePAT: ${{ secrets.GITHUB_TOKEN }}
run: |
./gradlew clean build $GRADLE_SKIP_TASKS $BUILD_USING_DOCKER
19 changes: 19 additions & 0 deletions .github/workflows/stale_check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: 'Close stale pull requests'

on:
schedule:
- cron: '30 19 * * *'
workflow_dispatch:

jobs:
stale:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@v3
with:
stale-pr-message: 'This PR has been open for more than 15 days with no activity. This will be closed in 3 days unless the `stale` label is removed or commented.'
close-pr-message: 'Closed PR due to inactivity for more than 18 days.'
days-before-pr-stale: 15
days-before-pr-close: 3
days-before-issue-stale: -1
days-before-issue-close: -1
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Ballerina MQTT Library

This package provides an implementation to interact with MQTT servers via MQTT client and listener.
[![Build](https://github.com/xlibb/module-mqtt/actions/workflows/build-timestamped-master.yml/badge.svg)](https://github.com/xlibb/module-mqtt/actions/workflows/build-timestamped-master.yml)
[![codecov](https://codecov.io/gh/xlibb/module-mqtt/branch/master/graph/badge.svg)](https://codecov.io/gh/xlibb/module-mqtt)
[![Trivy](https://github.com/xlibb/module-mqtt/actions/workflows/trivy-scan.yml/badge.svg)](https://github.com/xlibb/module-mqtt/actions/workflows/trivy-scan.yml)
[![GraalVM Check](https://github.com/xlibb/module-mqtt/actions/workflows/build-with-bal-test-graalvm.yml/badge.svg)](https://github.com/xlibb/module-mqtt/actions/workflows/build-with-bal-test-graalvm.yml)
[![GitHub Last Commit](https://img.shields.io/github/last-commit/xlibb/module-mqtt.svg)](https://github.com/xlibb/module-mqtt/commits/master)

This Library provides an implementation to interact with MQTT servers via MQTT client and listener.

MQTT is a lightweight, publish-subscribe, machine to machine network protocol for message queue/message queuing service.

Expand Down
16 changes: 11 additions & 5 deletions ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
org = "xlibb"
name = "mqtt"
version = "0.1.4"
version = "0.2.0"
authors = ["xlibb"]
keywords = ["mqtt"]
repository = "https://github.com/xlibb/module-mqtt"
Expand All @@ -10,12 +10,18 @@ distribution = "2201.6.0"
[[platform.java11.dependency]]
groupId = "io.xlibb"
artifactId = "mqtt-native"
version = "0.1.4"
path = "../native/build/libs/mqtt-native-0.1.4.jar"
version = "0.2.0"
path = "../native/build/libs/mqtt-native-0.2.0-SNAPSHOT.jar"

# Azure dependencies
[[platform.java11.dependency]]
groupId = "org.eclipse.paho"
artifactId = "org.eclipse.paho.client.mqttv3"
artifactId = "org.eclipse.paho.mqttv5.client"
version = "1.2.5"
path = "./lib/org.eclipse.paho.client.mqttv3-1.2.5.jar"
path = "./lib/org.eclipse.paho.mqttv5.client-1.2.5.jar"

[[platform.java11.dependency]]
groupId = "org.bouncycastle"
artifactId = "bcpkix-jdk15on"
version = "1.69"
path = "./lib/bcpkix-jdk15on-1.69.jar"
8 changes: 5 additions & 3 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ distribution-version = "2201.6.0"
org = "ballerina"
name = "crypto"
version = "2.3.1"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "time"}
]
modules = [
{org = "ballerina", packageName = "crypto", moduleName = "crypto"}
]

[[package]]
org = "ballerina"
Expand Down Expand Up @@ -133,7 +135,6 @@ modules = [
org = "ballerina"
name = "time"
version = "2.2.5"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand All @@ -156,8 +157,9 @@ modules = [
[[package]]
org = "xlibb"
name = "mqtt"
version = "0.1.4"
version = "0.2.0"
dependencies = [
{org = "ballerina", name = "crypto"},
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.runtime"},
{org = "ballerina", name = "log"},
Expand Down
9 changes: 6 additions & 3 deletions ballerina/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,20 @@ configurations {

dependencies {
/* Azure dependencies */
externalJars(group: 'org.eclipse.paho', name: 'org.eclipse.paho.client.mqttv3', version: "${pahoMqttVersion}") {
externalJars(group: 'org.eclipse.paho', name: 'org.eclipse.paho.mqttv5.client', version: "${pahoMqtt5Version}") {
transitive = false
}
externalJars(group: 'org.bouncycastle', name: 'bcpkix-jdk15on', version: "${bouncycastleVersion}") {
transitive = false
}
}

task updateTomlFiles {
doLast {
def stdlibDependentPahoMqttVersion = project.pahoMqttVersion
def newConfig = ballerinaTomlFilePlaceHolder.text.replace("@project.version@", project.version)
newConfig = newConfig.replace("@toml.version@", tomlVersion)
newConfig = newConfig.replace("@paho.mqtt.version@", stdlibDependentPahoMqttVersion)
newConfig = newConfig.replace("@paho.mqtt.version@", project.pahoMqtt5Version)
newConfig = newConfig.replace("@bouncy.castle.version@", project.bouncycastleVersion)
ballerinaTomlFile.text = newConfig
}
}
Expand Down
9 changes: 8 additions & 1 deletion ballerina/errors.bal
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
# The common error type for the module.
public type Error distinct error;
public type Error distinct error<ErrorDetails>;

# The error details type for the module.
#
# + reasonCode - The reason code for the error
public type ErrorDetails record {|
int reasonCode?;
|};
32 changes: 24 additions & 8 deletions ballerina/listener.bal
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,35 @@ import ballerina/jballerina.java;
# Represents a MQTT listener endpoint.
public isolated client class Listener {

private final string[] & readonly topics;
private final Subscription[] & readonly mqttSubscriptions;

# Creates a new `mqtt:Listener`.
#
# + serverUri - The URI of the remote MQTT server
# + clientId - The unique client ID to identify the listener
# + topics - The topics to be subscribed to
# + subscriptions - The topics to be subscribed to
# + return - `mqtt:Error` if an error occurs while creating the listener
public isolated function init(string serverUri, string clientId, string|string[] topics, *ListenerConfiguration config) returns Error? {
if topics is string {
self.topics = [topics];
public isolated function init(string serverUri, string clientId, string|string[]|Subscription|Subscription[] subscriptions, *ListenerConfiguration config) returns Error? {
if subscriptions is Subscription {
self.mqttSubscriptions = [subscriptions.cloneReadOnly()];
} else if subscriptions is string {
self.mqttSubscriptions = [{
topic: subscriptions,
qos: 1
}];
} else if subscriptions is string[] {
Subscription[] mqttSubscriptions = [];
foreach string topic in subscriptions {
mqttSubscriptions.push({
topic: topic,
qos: 1
});
}
self.mqttSubscriptions = mqttSubscriptions.cloneReadOnly();
} else if subscriptions is Subscription[] {
self.mqttSubscriptions = subscriptions.cloneReadOnly();
} else {
self.topics = topics.cloneReadOnly();
panic error("Invalid topics provided");
}
check self.externInit(serverUri, clientId, config);
}
Expand All @@ -32,10 +48,10 @@ public isolated client class Listener {
#
# + return - A `error` if an error is encountered while starting the server or else `()`
public isolated function 'start() returns error? {
check self.externStart(self.topics);
check self.externStart(self.mqttSubscriptions);
};

private isolated function externStart(string[] topics) returns error? =
private isolated function externStart(Subscription[] topics) returns error? =
@java:Method {
'class: "io.xlibb.mqtt.listener.ListenerActions"
} external;
Expand Down
Loading

0 comments on commit c12816a

Please sign in to comment.