-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Vincenzo Palazzo <[email protected]>
- Loading branch information
1 parent
7282c81
commit f119439
Showing
14 changed files
with
256 additions
and
21 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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
package com.lvaccaro.lamp.Services | ||
|
||
import android.util.Log | ||
import java.io.* | ||
import java.util.logging.Logger | ||
|
||
|
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
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,98 @@ | ||
package com.lvaccaro.lamp.util | ||
|
||
import android.content.Context | ||
import android.os.FileObserver | ||
import android.util.Log | ||
import com.lvaccaro.lamp.util.hendler.IEventHandler | ||
import com.lvaccaro.lamp.util.hendler.NewChannelPayment | ||
import com.lvaccaro.lamp.util.hendler.ShutdownNode | ||
import java.io.File | ||
import java.io.LineNumberReader | ||
|
||
/** | ||
* This class is an implementation of FileObserver discussed inside the PRs XX | ||
* The ideas is from @lvaccaro | ||
* | ||
* Info about the clightning node | ||
* | ||
* ---- Node create a transaction (with fundchannel or when I receive the onchain tx)---- | ||
* Pattern log: DEBUG wallet: Owning output 1 89846sat (SEGWIT) txid 33c1f5d2df4f425898dc6eb49dae51aaab1d430ee7c0da2cab18123d5c1192f0 | ||
* ---- Node make a withdraw action | ||
* Patter log: DEBUG lightningd: sendrawtransaction | ||
* | ||
* --- Node adding block | ||
* Pattern log: DEBUG lightningd: Adding block | ||
* | ||
* --- Node sendrawtransaction | ||
* Pattern log: DEBUG plugin-esplora: sendrawtx exit 0 | ||
* | ||
* ---- Shutdown node with command close---- | ||
* Pattern log: 2020-08-03T15:38:38.812Z UNUSUAL lightningd: JSON-RPC shutdown | ||
* | ||
* ------ Node receive transaction to blockchain ---- | ||
* Pattern log: No debug log | ||
* | ||
* ---- Node receive a lightning payment (keysend, pay) | ||
* Pattern log: | ||
* | ||
* @author https://github.com/vincenzopalazzo | ||
*/ | ||
class LogObserver(val context: Context, val path: String, val nameFile: String) : FileObserver(path) { | ||
|
||
init { | ||
initHandler() | ||
} | ||
|
||
companion object { | ||
val TAG = LogObserver::class.java.canonicalName | ||
} | ||
|
||
private lateinit var actionHandler: ArrayList<IEventHandler> | ||
private lateinit var logFile: File | ||
private var actualLine = 0 | ||
private var lineNumberReader: LineNumberReader? = null | ||
|
||
|
||
fun initHandler() { | ||
actionHandler = ArrayList<IEventHandler>() | ||
actionHandler.add(NewChannelPayment(LampKeys.NODE_NOTIFICATION_FUNDCHANNEL)) | ||
actionHandler.add(ShutdownNode(LampKeys.NODE_NOTIFICATION_SHUTDOWN)) | ||
} | ||
|
||
override fun onEvent(event: Int, file: String?) { | ||
if(file == null) return | ||
if (file?.equals(nameFile)) { | ||
when (event) { | ||
FileObserver.MODIFY -> readNewLines() | ||
} | ||
} | ||
} | ||
|
||
private fun readNewLines() { | ||
if(lineNumberReader == null) | ||
initFileLog() | ||
|
||
//FIXME(vicenzopalazzo): This is real util? | ||
if(lineNumberReader == null) return | ||
lineNumberReader?.lineNumber = actualLine | ||
var line: String? = lineNumberReader?.readLine() | ||
while (line != null){ | ||
readLogLine(line) | ||
Log.d(TAG, line) | ||
line = lineNumberReader?.readLine() | ||
} | ||
} | ||
|
||
private fun readLogLine(line: String) { | ||
actionHandler.forEach { it -> it.doReceive(context, line) } | ||
actualLine++ | ||
} | ||
|
||
private fun initFileLog() { | ||
logFile = File(path, nameFile) | ||
lineNumberReader = LineNumberReader(logFile.reader()) | ||
//FIXME: level api that are enable this line about is Android nougat | ||
// for the actual version of lightning_ndk I don't need to insert the check of the version | ||
actualLine = lineNumberReader!!.lines().count().toInt() | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
app/src/main/java/com/lvaccaro/lamp/util/SimulatorPlugin.kt
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,58 @@ | ||
package com.lvaccaro.lamp.util | ||
|
||
import android.util.Log | ||
import org.json.JSONArray | ||
import org.json.JSONObject | ||
|
||
class SimulatorPlugin{ | ||
|
||
companion object{ | ||
|
||
val TAG = SimulatorPlugin::class.java.canonicalName | ||
|
||
fun funds(jsonObject: JSONObject): JSONObject{ | ||
val response = JSONObject() | ||
var onChainFunds = 0 | ||
var offChainFunds = 0 | ||
var fundsToUs = 0 | ||
if(jsonObject.has("outputs")){ | ||
val outputs: JSONArray = jsonObject["outputs"] as JSONArray | ||
for (i in 0 until outputs.length()){ | ||
val output = outputs.getJSONObject(i) | ||
onChainFunds += output["value"] as Int | ||
Log.d(TAG, "****** On chain fund ${onChainFunds}") | ||
} | ||
} | ||
//FIXME(vicenzopalazzo) maybe in this section is possible add more | ||
// information about status of channels, if the channel is ONCHAIN | ||
// is not correct calculate the sat here, right? | ||
if(jsonObject.has("channels")){ | ||
val channels: JSONArray = jsonObject["channels"] as JSONArray | ||
for(i in 0 until channels.length()){ | ||
val channel = channels.getJSONObject(i) | ||
offChainFunds = channel["channel_sat"] as Int | ||
Log.d(TAG, "****** Off chain fund ${offChainFunds}") | ||
} | ||
} | ||
response.put("on_chain", onChainFunds.toString()) | ||
response.put("off_chain", offChainFunds.toString()) | ||
return response | ||
} | ||
|
||
fun fundsInChannel(listFunds: JSONObject): Any { | ||
var fundsToUs = 0 | ||
val peers = listFunds["peers"] as JSONArray | ||
for (i in 0 until peers.length()) { | ||
val peer = peers.get(i) as? JSONObject | ||
val peerChannels = peer?.get("channels") as JSONArray | ||
for (j in 0 until peerChannels.length()) { | ||
val channel = peerChannels.get(j) as JSONObject | ||
fundsToUs += channel["msatoshi_to_us"] as Int | ||
} | ||
} | ||
val response = JSONObject() | ||
response.put("to_us", fundsToUs.toString()) | ||
return response | ||
} | ||
} | ||
} |
Oops, something went wrong.