Http Server / Client communication #7

Merged
faraphel merged 24 commits from test-http into main 2024-05-17 17:22:56 +02:00
3 changed files with 145 additions and 71 deletions
Showing only changes of commit 17e99248e1 - Show all commits

View file

@ -3,7 +3,40 @@
<component name="deploymentTargetDropDown"> <component name="deploymentTargetDropDown">
<value> <value>
<entry key="app"> <entry key="app">
<State /> <State>
<targetSelectedWithDropDown>
<Target>
<type value="QUICK_BOOT_TARGET" />
<deviceKey>
<Key>
<type value="VIRTUAL_DEVICE_PATH" />
<value value="$USER_HOME$/.android/avd/Very_Small_API_34_2.avd" />
</Key>
</deviceKey>
</Target>
</targetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2024-03-22T12:51:27.914541423Z" />
<targetsSelectedWithDialog>
<Target>
<type value="QUICK_BOOT_TARGET" />
<deviceKey>
<Key>
<type value="VIRTUAL_DEVICE_PATH" />
<value value="$USER_HOME$/.android/avd/Micro_API_26.avd" />
</Key>
</deviceKey>
</Target>
<Target>
<type value="QUICK_BOOT_TARGET" />
<deviceKey>
<Key>
<type value="VIRTUAL_DEVICE_PATH" />
<value value="$USER_HOME$/.android/avd/Very_Small_API_34_2.avd" />
</Key>
</deviceKey>
</Target>
</targetsSelectedWithDialog>
</State>
</entry> </entry>
</value> </value>
</component> </component>

View file

@ -2,6 +2,32 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<!-- SDK -->
<uses-sdk
android:minSdkVersion="14"
tools:ignore="GradleOverrides" />
<!-- Permissions -->
<!-- Internet -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Internet < Android 13 (API 33) -->
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"
android:maxSdkVersion="32" />
<!-- Internet >= Android 13 (API 33) -->
<uses-permission
android:name="android.permission.NEARBY_WIFI_DEVICES"
android:usesPermissionFlags="neverForLocation"
tools:targetApi="s" />
<!-- Applications -->
<application <application
android:allowBackup="true" android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules" android:dataExtractionRules="@xml/data_extraction_rules"
@ -10,6 +36,7 @@
android:label="@string/app_name" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
tools:ignore="RtlEnabled"
android:theme="@style/Theme.Tasksvalider" android:theme="@style/Theme.Tasksvalider"
tools:targetApi="31"> tools:targetApi="31">
<activity <activity

View file

@ -1,24 +1,19 @@
package com.faraphel.tasks_valider package com.faraphel.tasks_valider
import android.content.Context
import android.net.wifi.p2p.WifiP2pConfig
import android.net.wifi.p2p.WifiP2pManager
import android.net.wifi.p2p.WifiP2pManager.ActionListener
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.Column
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase
import com.faraphel.tasks_valider.database.Database import com.faraphel.tasks_valider.database.Database
import com.faraphel.tasks_valider.database.entities.Group import java.net.InetAddress
import com.faraphel.tasks_valider.database.entities.GroupStudent import java.net.InetSocketAddress
import com.faraphel.tasks_valider.database.entities.Student import java.net.ServerSocket
import com.faraphel.tasks_valider.database.entities.Task import java.net.Socket
import com.faraphel.tasks_valider.database.entities.TaskGroup
import com.faraphel.tasks_valider.database.entities.Teacher
import com.faraphel.tasks_valider.ui.widgets.WidgetTaskStudent
import java.time.Instant
class MainActivity : ComponentActivity() { class MainActivity : ComponentActivity() {
@ -31,69 +26,88 @@ class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
// Reset the database // ----- CONNECTION
this.deleteDatabase("local") // TODO: remove
// Create the database
database = Room.databaseBuilder(
this.applicationContext,
Database::class.java, "local"
)
.allowMainThreadQueries() // TODO: remove
.build()
// Create some data
val studentDao = database.studentDao()
studentDao.insert(
Student(firstName = "John", lastName = "Joe"),
Student(firstName = "Evan", lastName = "Doe"),
Student(firstName = "Xavier", lastName = "Moe"),
)
val teacherDao = database.teacherDao()
teacherDao.insert(
Teacher(firstName = "Jean", lastName = "Voe")
)
val groupDao = database.groupDao()
groupDao.insert(
Group(name = "Group 1"),
Group(name = "Group 2"),
)
val groupStudentDao = database.groupStudentDao()
groupStudentDao.insert(
GroupStudent(1, 1),
GroupStudent(1, 2),
GroupStudent(2, 3),
)
val taskDao = database.taskDao()
taskDao.insert(
Task(title = "Task 1", description = "Do something"),
Task(title = "Task 2", description = "Do something else"),
Task(title = "Task 3", description = "Do something nice"),
)
val taskGroupDao = database.taskGroupDao()
taskGroupDao.insert(
TaskGroup(1, 1),
TaskGroup(2, 2, true, 1, Instant.now()),
)
/* /*
val intentFilter = IntentFilter()
// display some data // Indicates a change in the Wi-Fi Direct status.
this.setContent { intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION)
Column { intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION)
database.taskGroupDao().getAll().forEach { taskGroup -> intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION)
WidgetTaskStudent(database, taskGroup) intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION)
this.registerReceiver(MyBroadcastReceiver(this), intentFilter)
*/
// get the WiFi direct manager
val manager: WifiP2pManager? = this.getSystemService(Context.WIFI_P2P_SERVICE) as WifiP2pManager?
if (manager == null) {
Log.e("wifi-p2p", "cannot get the manager")
return
}
val channel = manager.initialize(this, this.mainLooper, null)
// get the list of peers in the group
manager.requestPeers(channel) { devices ->
devices.deviceList.forEach { device ->
Log.d("wifi-p2p", "peer found : [${device.deviceAddress}] ${device.deviceName}")
}
}
manager.requestGroupInfo(channel) { group ->
val config = WifiP2pConfig().apply {
this.deviceAddress = group.owner.deviceAddress
}
if (group.isGroupOwner) {
// the device is the host
Log.d("wifi-p2p", "I am the owner ! (${group.owner})")
Thread {
while (true) {
// declare the server
val serverSocket = ServerSocket()
serverSocket.bind(InetSocketAddress(8888))
// accept a connection
val clientSocket = serverSocket.accept()
val data = clientSocket.getInputStream().read()
// print the data
Log.i("wifi-p2p", "Data received (${clientSocket.inetAddress}) : $data")
// close the connection
clientSocket.close()
}
}.start()
} else {
// the device is a simple peer
Log.d("wifi-p2p", "I am a peer !")
// connect to the host and send a packet
Thread {
manager.connect(channel, config, object : ActionListener {
override fun onSuccess() {
val hostAddress = InetAddress.getByName(config.deviceAddress)
val port = 8888
Log.d("wifi-p2p", "Sending packet to host")
val socket = Socket()
socket.connect(InetSocketAddress(hostAddress, port))
socket.outputStream.write("test".toByteArray())
}
override fun onFailure(reason: Int) {
Log.e("wifi-p2p", "Error when connecting to host : $reason")
}
})
} }
} }
} }
*/
} }
} }