-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
449 additions
and
292 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package ir.am3n.needtool.sample | ||
|
||
import android.annotation.SuppressLint | ||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
import ir.am3n.needtool.webserver.HttpHandler | ||
import ir.am3n.needtool.webserver.HttpServer | ||
import kotlinx.android.synthetic.main.activity_webserver.serverButton | ||
import kotlinx.android.synthetic.main.activity_webserver.serverTextView | ||
import org.json.JSONObject | ||
import java.io.InputStream | ||
import java.net.InetSocketAddress | ||
import java.util.Scanner | ||
import java.util.concurrent.Executors | ||
|
||
@SuppressLint("SetTextI18n") | ||
class WebServerAct : AppCompatActivity() { | ||
|
||
private var serverUp = false | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_webserver) | ||
|
||
serverButton.setOnClickListener { | ||
serverUp = if (!serverUp){ | ||
startServer(port = 5000) | ||
true | ||
} else{ | ||
stopServer() | ||
false | ||
} | ||
} | ||
|
||
} | ||
|
||
private fun streamToString(inputStream: InputStream): String { | ||
val s = Scanner(inputStream).useDelimiter("\\A") | ||
return if (s.hasNext()) s.next() else "" | ||
} | ||
|
||
private var mHttpServer: HttpServer? = null | ||
|
||
|
||
private fun startServer(port: Int) { | ||
try { | ||
mHttpServer = HttpServer.create(InetSocketAddress(port), 0) | ||
mHttpServer!!.executor = Executors.newCachedThreadPool() | ||
mHttpServer!!.createContext("/", rootHandler) | ||
mHttpServer!!.createContext("/index", rootHandler) | ||
mHttpServer!!.createContext("/messages", messageHandler) | ||
mHttpServer!!.start() | ||
serverTextView.text = "server running" | ||
serverButton.text = "stop server" | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
} | ||
|
||
} | ||
|
||
private fun stopServer() { | ||
if (mHttpServer != null) { | ||
mHttpServer!!.stop(0) | ||
serverTextView.text = "server down" | ||
serverButton.text = "start server" | ||
} | ||
} | ||
|
||
private val rootHandler = HttpHandler { exchange -> | ||
run { | ||
when (exchange!!.requestMethod) { | ||
"GET" -> { | ||
exchange.sendUTF8Response("Welcome to سلام my server po") | ||
} | ||
} | ||
} | ||
} | ||
|
||
private val messageHandler = HttpHandler { exchange -> | ||
run { | ||
when (exchange!!.requestMethod) { | ||
"GET" -> { | ||
exchange.sendUTF8Response("Would be all messages stringified json") | ||
} | ||
"POST" -> { | ||
val inputStream = exchange.requestBody | ||
val requestBody = streamToString(inputStream) | ||
val jsonBody = JSONObject(requestBody) | ||
exchange.sendUTF8JsonResponse(jsonBody) | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".MainActivity"> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:layout_gravity="center" | ||
android:gravity="center" | ||
android:orientation="vertical" | ||
app:layout_behavior="@string/appbar_scrolling_view_behavior" | ||
tools:context=".MainActivity" | ||
tools:showIn="@layout/activity_main"> | ||
|
||
<TextView | ||
android:id="@+id/serverTextView" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="center" | ||
android:layout_marginBottom="20dp" | ||
android:gravity="center" | ||
android:text="server down" | ||
android:textSize="16sp" /> | ||
|
||
<Button | ||
android:id="@+id/serverButton" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginStart="20dp" | ||
android:layout_marginEnd="20dp" | ||
android:background="@color/colorPrimary" | ||
android:text="start server" | ||
android:textAppearance="@style/Base.TextAppearance.AppCompat.Inverse" | ||
android:textColor="#ffffff" /> | ||
|
||
</LinearLayout> | ||
|
||
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ android { | |
compileSdk 33 | ||
|
||
defaultConfig { | ||
minSdk 16 | ||
minSdk 19 | ||
targetSdk 33 | ||
multiDexEnabled true | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.