Skip to content

Commit

Permalink
UDP peer discovery works (?) again
Browse files Browse the repository at this point in the history
  • Loading branch information
tschudin committed Sep 2, 2021
1 parent 6fd8c8b commit 45a01f3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
48 changes: 34 additions & 14 deletions app/src/main/java/nz/scuttlebutt/tremola/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class MainActivity : Activity() {
requestWindowFeature(Window.FEATURE_NO_TITLE)
setContentView(R.layout.activity_main)
tremolaState = TremolaState(this)
mkSockets()

Log.d("IDENTITY", "is ${tremolaState.idStore.identity.toRef()}")

Expand Down Expand Up @@ -74,17 +75,15 @@ class MainActivity : Activity() {
override fun onLinkPropertiesChanged(nw: Network, prop: LinkProperties) {
Log.d("onLinkPropertiesChanged", "${nw} ${prop}")
super.onLinkPropertiesChanged(nw, prop)
try { broadcast_socket?.close() } catch (e: Exception) {}
broadcast_socket = DatagramSocket(
Constants.SSB_IPV4_UDPPORT, // where to listen
InetAddress.getByName("0.0.0.0")
)
broadcast_socket?.broadcast = true
Log.d("new bcast sock", "${broadcast_socket}, ${broadcast_socket?.port}/${broadcast_socket?.localPort}")
val wifiManager = getSystemService(Context.WIFI_SERVICE) as WifiManager
try { server_socket?.close() } catch (e: Exception) {}
server_socket = ServerSocket(Constants.SSB_IPV4_TCPPORT)
Log.d("SERVER addr", "${Formatter.formatIpAddress(wifiManager.connectionInfo.ipAddress)}:${server_socket!!.localPort}")
/*
server_socket?.let {
if (it.inetAddress in prop.linkAddresses) {
Log.d("onLinkPropertiesChanged", "no need for new sock")
return
}
}
*/
mkSockets()
}
/*
override fun onAvailable(network: Network) {
Expand All @@ -98,13 +97,22 @@ class MainActivity : Activity() {
val lck = ReentrantLock()

val t0 = thread(isDaemon=true) {
udp!!.beacon(tremolaState.idStore.identity.verifyKey, lck, Constants.SSB_IPV4_TCPPORT)
try {
udp!!.beacon(tremolaState.idStore.identity.verifyKey, lck, Constants.SSB_IPV4_TCPPORT)
} catch (e: Exception) {
Log.d("beacon thread", "died ${e}")
}
}

val t1 = thread(isDaemon=true) {
val wifi = getSystemService(WIFI_SERVICE) as WifiManager
val mLock = wifi.createMulticastLock("lock")
mLock.acquire()
udp!!.listen(lck)
try {
udp!!.listen(lck)
} catch (e: Exception) {
Log.d("listen thread", "died ${e}")
}
}
val t2 = thread(isDaemon=true) { // accept loop, robust against reassigned server_socket
while (true) {
Expand Down Expand Up @@ -193,5 +201,17 @@ class MainActivity : Activity() {
super.onDestroy()
}


private fun mkSockets() {
try { broadcast_socket?.close() } catch (e: Exception) {}
broadcast_socket = DatagramSocket(
Constants.SSB_IPV4_UDPPORT, // where to listen
InetAddress.getByName("0.0.0.0")
)
broadcast_socket?.broadcast = true
Log.d("new bcast sock", "${broadcast_socket}, UDP port ${broadcast_socket?.localPort}")
val wifiManager = getSystemService(Context.WIFI_SERVICE) as WifiManager
try { server_socket?.close() } catch (e: Exception) {}
server_socket = ServerSocket(Constants.SSB_IPV4_TCPPORT)
Log.d("SERVER TCP addr", "${Formatter.formatIpAddress(wifiManager.connectionInfo.ipAddress)}:${server_socket!!.localPort}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package nz.scuttlebutt.tremola.ssb.peering
import android.content.Context
import android.net.DhcpInfo
import android.net.wifi.WifiManager
import android.os.SystemClock
import android.text.format.Formatter.formatIpAddress
import android.util.Base64
import android.util.Log
Expand All @@ -12,7 +11,6 @@ import nz.scuttlebutt.tremola.WebAppInterface
import nz.scuttlebutt.tremola.utils.Constants
import java.lang.Thread.sleep
import java.net.DatagramPacket
import java.net.DatagramSocket
import java.net.InetAddress
import java.util.concurrent.locks.ReentrantLock

Expand All @@ -25,6 +23,7 @@ class UDPbroadcast(val context: MainActivity, val wai: WebAppInterface?) {
fun beacon(pubkey: ByteArray, lck: ReentrantLock, myTcpPort: Int) {

fun mkDgram(): DatagramPacket {
// get the current address (for each beacon msg)
val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
val dhcp = wifiManager.getDhcpInfo() // assumes IPv4?
val broadcast = dhcp.ipAddress or (0xff shl 24) // (-1 and dhcp.netmask.inv()) // dhcp.ipAddress and dhcp.netmask or dhcp.netmask.inv()
Expand All @@ -34,24 +33,25 @@ class UDPbroadcast(val context: MainActivity, val wai: WebAppInterface?) {
// val dest = InetAddress.getByName("255.255.255.255")
val bcastAddr = InetAddress.getByAddress(quads);
val myAddr = wifiManager.connectionInfo.ipAddress
Log.d("UDP BEACON", "my=${formatIpAddress(myAddr)}, broadcast=${bcastAddr}, mask=${dhcp.netmask.inv()}")
// blocking?? Log.d("UDP BEACON", "my=${formatIpAddress(myAddr)}, broadcast=${bcastAddr}, mask=${dhcp.netmask.inv()}")
myMark = "net:${formatIpAddress(myAddr)}:${myTcpPort}~shs:" +
Base64.encodeToString(pubkey, Base64.NO_WRAP)
val buf = myMark!!.encodeToByteArray()
return DatagramPacket(buf, buf.size, bcastAddr, Constants.SSB_IPV4_UDPPORT)
// Log.d("UDP BEACON", "${myMark}")
val d = DatagramPacket(buf, buf.size, bcastAddr, Constants.SSB_IPV4_UDPPORT)
return d
}

while (true) {
try {
var dgram = mkDgram()
val dgram = mkDgram()
val s = context.broadcast_socket
Log.d("beacon sock", "bound=${s?.isBound}, ${s?.inetAddress}/${s?.port}/${s?.localPort} brcast${s?.broadcast}")
context.broadcast_socket?.send(dgram);
Log.d("beacon", "sent") }
s?.send(dgram)
// blocking?? Log.d("beacon sock", "bound=${s?.isBound}, ${s?.localAddress}/${s?.port}/${s?.localPort} brcast${s?.broadcast}")
Log.d("beacon", "sent ${myMark}") }
catch (e: Exception) { // wait for better times
Log.d("Beacon exc", e.toString())
sleep(3000)
// dgram = mkDgram()
continue
}
sleep(3000)
Expand All @@ -75,11 +75,10 @@ class UDPbroadcast(val context: MainActivity, val wai: WebAppInterface?) {
val ingram = DatagramPacket(buf, buf.size)
while (true) {
val s = context.broadcast_socket
Log.d("listen", "${s}, ports=${s?.port}/${s?.localPort} closed=${s?.isClosed} bound=${s?.isBound}")
// blocks?? Log.d("listen", "${s}, ports=${s?.port}/${s?.localPort} closed=${s?.isClosed} bound=${s?.isBound}")
try { context.broadcast_socket?.receive(ingram) }
catch (e: Exception) {
Log.d("Broadcast listen", "e=${e}, bsock=${context.broadcast_socket}")
SystemClock.sleep(3000) // wait for better conditions
sleep(3000) // wait for better conditions
continue
}
val incoming = ingram.data.decodeToString(0, ingram.length)
Expand All @@ -97,6 +96,5 @@ class UDPbroadcast(val context: MainActivity, val wai: WebAppInterface?) {
}
}
}
Log.d("LISTEN", "ended")
}
}

0 comments on commit 45a01f3

Please sign in to comment.