Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement nsd #21

Merged
merged 3 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/src/main/java/net/xcreen/restsms/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
//Set Home Fragment
val fragmentTransaction = supportFragmentManager.beginTransaction()
try {
val homeFragment: Fragment = HomeFragment::class.java.newInstance()
val homeFragment: Fragment = HomeFragment::class.java.getDeclaredConstructor().newInstance()
fragmentTransaction.replace(R.id.main_framelayout, homeFragment).commit()
} catch (ex: Exception) {
ex.printStackTrace()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class SettingsFragment : Fragment() {
val openBrowserCheckBox = rootView.findViewById<CheckBox>(R.id.settings_open_browser_checkbox)
val disableLoggingCheckBox = rootView.findViewById<CheckBox>(R.id.settings_disable_logging_checkbox)
val enableAuth = rootView.findViewById<CheckBox>(R.id.settings_enable_auth)
val enableNSD = rootView.findViewById<CheckBox>(R.id.settings_enable_nsd)
val saveBtn = rootView.findViewById<Button>(R.id.settings_save_btn)
saveBtn.setOnClickListener { v ->
var saved = false
Expand Down Expand Up @@ -58,6 +59,9 @@ class SettingsFragment : Fragment() {
//Save Enable authentication
editor.putBoolean("enable_auth", enableAuth.isChecked)
editor.apply()
//Save Enable NSD
editor.putBoolean("enable_nsd", enableNSD.isChecked)
editor.apply()
if (saved) {
Toast.makeText(v.context, resources.getText(R.string.setting_saved), Toast.LENGTH_SHORT).show()
}
Expand All @@ -78,6 +82,10 @@ class SettingsFragment : Fragment() {
if (sharedPref.getBoolean("enable_auth", false)) {
enableAuth.isChecked = true
}
//Set current "NSD"-Option
if (sharedPref.getBoolean("enable_nsd", false)) {
enableNSD.isChecked = true
}
return rootView
}
}
46 changes: 45 additions & 1 deletion app/src/main/java/net/xcreen/restsms/server/SMSServer.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package net.xcreen.restsms.server

import android.content.Context
import android.net.nsd.NsdManager
import android.net.nsd.NsdServiceInfo
import android.util.Log
import net.xcreen.restsms.AppContext
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.servlet.ServletContextHandler
import org.eclipse.jetty.servlet.ServletHolder
import javax.servlet.MultipartConfigElement

class SMSServer {
var port = 8080
var startNSD = false
var goodToken = ""
var authEnabled = false
private var jettyServer: Server? = null

private var nsdManager: NsdManager? = null
private var nsdRegistrationListener: NsdManager.RegistrationListener? = null
private val TAG = "SMSServer"
/**
* Get Server-Logger
* @return serverLogging - ServerLogging-Object
Expand Down Expand Up @@ -38,6 +46,39 @@ class SMSServer {
servletContextHandler.addServlet(smsWelcomeServletHolder, "/")
jettyServer!!.handler = servletContextHandler

//Start NSD
val nsdServiceInfo = NsdServiceInfo()
nsdServiceInfo.serviceName = "RestSMS"
nsdServiceInfo.serviceType = "_http._tcp."
nsdServiceInfo.port = port

nsdManager = AppContext.appContext.getSystemService(Context.NSD_SERVICE) as NsdManager

nsdRegistrationListener = object : NsdManager.RegistrationListener {
override fun onServiceRegistered(serviceInfo: NsdServiceInfo) {
val registeredName = serviceInfo.serviceName
Log.i(TAG, "NSD Service registered: $registeredName")
serverLogging!!.log("info", "NSD Service registered: $registeredName")
}

override fun onRegistrationFailed(serviceInfo: NsdServiceInfo, errorCode: Int) {
Log.e(TAG, "NSD Service registration failed: $errorCode")
serverLogging!!.log("error", "NSD Service registration failed: $errorCode")
}

override fun onServiceUnregistered(serviceInfo: NsdServiceInfo) {
Log.i(TAG, "NSD Service unregistered.")
serverLogging!!.log("info", "NSD Service unregistered.")
}

override fun onUnregistrationFailed(serviceInfo: NsdServiceInfo, errorCode: Int) {
Log.e(TAG, "NSD Service unregistration failed: $errorCode")
serverLogging!!.log("error", "NSD Service unregistration failed: $errorCode")
}
}
if(startNSD) {
nsdManager!!.registerService(nsdServiceInfo, NsdManager.PROTOCOL_DNS_SD, nsdRegistrationListener)
}
//Start Jetty
jettyServer!!.start()
jettyServer!!.join()
Expand All @@ -53,6 +94,9 @@ class SMSServer {
serverLogging!!.log("info", "Stopping Server...")
jettyServer!!.stop()
}
nsdRegistrationListener?.let {
nsdManager?.unregisterService(it)
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/net/xcreen/restsms/server/ServerService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class ServerService : Service() {
val sharedPref = PreferenceManager.getDefaultSharedPreferences(this)
val serverPort = sharedPref.getInt("server_port", 8080)
appContext?.smsServer?.port = serverPort
//Set NSD
appContext?.smsServer?.startNSD = sharedPref.getBoolean("enable_nsd", false)
//Set Auth params
val goodToken = sharedPref.getString("server_token", "") ?: ""
val authEnabled = sharedPref.getBoolean("enable_auth", false)
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/layout/fragment_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@
android:textColor="@color/colorWhite"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/settings_open_browser_checkbox" />

<CheckBox
android:id="@+id/settings_enable_nsd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/setting_enable_nsd"
android:textAppearance="@style/TextAppearance.MaterialComponents.Subtitle1"
android:textColor="@color/colorWhite"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/settings_token_edittext" />
</androidx.constraintlayout.widget.ConstraintLayout>

</FrameLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<string name="setting_open_website">Open Browser after Server-Start</string>
<string name="setting_disable_logging">Disable writing/creating log-files</string>
<string name="setting_enable_auth">Enable authentication</string>
<string name="setting_enable_nsd">Enable Network Service Discovery</string>
<string name="setting_save">Save</string>
<string name="setting_invalid_port">Invalid Port!</string>
<string name="setting_invalid_port_range">Port must be between 0 and 65535!</string>
Expand Down
Loading