<commit_before>package nl.sison.android.nodejs.sensors

import android.app.Service
import android.content.Intent

import android.net.LocalSocket

import android.net.LocalSocketAddress
import android.net.LocalServerSocket
import android.text.TextUtils

import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager

import org.json.JSONArray
import org.json.JSONObject

import android.os.Looper
import android.os.Handler
import android.util.Log

import android.system.Os
import android.system.OsConstants
import java.io.FileDescriptor

import java.io.File
import java.io.IOException

import nl.sison.android.nodejs.repl.NodeJNI


// Gradle bug, after project clean, MIA, specific for android
import nl.sison.android.nodejs.BuildConfig

import org.xtendroid.annotations.AddLogTag

import java.lang.reflect.Method


@AddLogTag
class NodeSensorBaseService extends Service implements SensorEventListener {

    Handler mainHandler = new Handler(Looper.getMainLooper())

    override onBind(Intent intent)
    {
        return null // we will not bind this
    }

    protected Sensor mSensor
    protected SensorManager mSensorManager
    protected val SENSOR_TYPE = Sensor.TYPE_ALL
    protected val SENSOR_DELAY = SensorManager.SENSOR_DELAY_NORMAL
    protected def void startSensor()
    {
        // Caveat: there could be multiple sensors of a type,
        // this implementation assumes there is one of each type
        // feel free to override :)
        // /data/data/nl.sison.android.nodejs.repl/cache/sensor_sockets.TYPE/ALL
        Log.d(TAG, 'Getting sensor service: ' + SENSOR_TYPE_NAME)
        mSensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager

        var sensorList = mSensorManager.getSensorList(SENSOR_TYPE)

        if (sensorList.size > 0)
        {
            //mainHandler.post[ Log.i(TAG, String.format('There are multiple sensors for the %s sensor', SENSOR_TYPE_NAME)) ]
            sensorList.forEach[ s |
                Log.i(TAG, String.format('available %s sensor: %s', SENSOR_TYPE_NAME, s.name))
            ]
        }

        mSensor = mSensorManager.getDefaultSensor(SENSOR_TYPE)

        if (mSensor != null)
        {
            mSensorManager.registerListener(this, mSensor, SENSOR_DELAY)
            Log.d(TAG, 'Running sensor service: ' + SENSOR_TYPE_NAME)
        }else
            {stopSelf} // harakiri
    }

    val bufSize = 1024 // hopefully this is more than enuff
    LocalServerSocket mLocalServerSocket
    LocalSocket       mLocalSocketSender

    /**
     * Must overload to determine the type of sensor being started
     * and differentiate the domain socket (abstract or not)
     */
    protected val SENSOR_TYPE_NAME = 'ALL'
    val SOCKET_STREAM = 2
    val BACK_LOG = 50
    var FileDescriptor mFileDescriptor
    def startLocalServerSocket()
    {
        // /data/data/nl.sison.android.nodejs.repl/cache/sensor_sockets.TYPE/ALL
        var location = TextUtils.concat(cacheDir.toString, '/sensor_sockets.TYPE/', SENSOR_TYPE_NAME).toString
        try {
            val addr = new LocalSocketAddress(location, LocalSocketAddress.Namespace.FILESYSTEM)
/*
            // LocalSocketServer::new
            val clazz = Class.forName('android.net.LocalSocketImpl')
            val ctors = clazz.declaredConstructors

            ctors.forEach[ c | Log.d(TAG, c.toString) ]

            // Change the accessible property of the constructor.
            val ctor = ctors.get(0)
            ctor.accessible = true
            val impl = ctor.newInstance(null)

            clazz.methods.forEach[ m | Log.d(TAG, m.toString) ]

            val create = clazz.getDeclaredMethod('create', #[ Integer.TYPE ])
            create.invoke(impl, #[ SOCKET_STREAM ]) // 4.4: IOException no fd.

            Thread.sleep(2000)

            val bind = clazz.getDeclaredMethod('bind', #[ LocalSocketAddress ])
            bind.invoke(impl, #[ addr ])

            val listen = clazz.getDeclaredMethod('listen', #[ Integer.TYPE ])
            listen.invoke(impl, #[ BACK_LOG ])

            // LocalSocketServer::accept
            val impl2 = ctor.newInstance(null)
            val accept = clazz.getDeclaredMethod('accept', #[ clazz ])
            accept.invoke(impl, #[ impl2 ])

            //var ls = new LocalSocket(impl2, 2)
            val lsCtors = Class.forName('android.net.LocalSocket').declaredConstructors

            val lsCtor = lsCtors.get(3)
            lsCtor.accessible = true

            // The following will block, so no funky busy loops with sleep necessary
            mLocalSocketSender = lsCtor.newInstance(impl2, SOCKET_STREAM) as LocalSocket
*/
            // First attempt, starting the 2nd...
            // phrack, android.system.Os is only available from api level 21
/*
            if (android.os.Build.VERSION.SDK_INT == 21)
            {
                val AF_UNIX = 1
                mFileDescriptor = Os.socket(AF_UNIX, SOCKET_STREAM, 0)

                //mLocalServerSocket = new LocalServerSocket(mFileDescriptor) // dead end, no bind

                // LocalSocketServer::new
                val clazz = Class.forName('android.net.LocalSocketImpl')
                val ctors = clazz.declaredConstructors

                // Change the accessible property of the constructor.
                val ctor = ctors.get(1)
                ctor.accessible = true
                val impl = ctor.newInstance(mFileDescriptor)

                val bind = clazz.getDeclaredMethod('bind', #[ LocalSocketAddress ])
                bind.invoke(impl, #[ addr ])

                val listen = clazz.getDeclaredMethod('listen', #[ Integer.TYPE ])
                listen.invoke(impl, #[ BACK_LOG ])
            }else
            {
                // use own NDK JNI solution... fragmentation blues...
            }
*/
            // finally...
            // LocalSocketServer::new
            /*
            mLocalServerSocket =
                new LocalServerSocket(NodeJNI.createLocalSocket(location).createFileDescriptor)

            // The following will block, so no funky busy loops with sleep necessary
            mLocalSocketSender = mLocalServerSocket.accept
            */
        }catch (IOException e)
        {
            Log.e(TAG, e.message)
            stopSelf
        }
    }
<commit_msg>at least the REPL is connecting again (still no feedback), the SensorService is dying after: nl.sison.android.nodejs.repl:SensorService E/NodeSensorBaseService﹕ Unknown error -1<commit_after>package nl.sison.android.nodejs.sensors

import android.app.Service
import android.content.Intent

import android.net.LocalSocket

import android.net.LocalSocketAddress
import android.net.LocalServerSocket
import android.text.TextUtils

import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager

import org.json.JSONArray
import org.json.JSONObject

import android.os.Looper
import android.os.Handler
import android.util.Log

import android.system.Os
import android.system.OsConstants
import java.io.FileDescriptor

import java.io.File
import java.io.IOException

import nl.sison.android.nodejs.repl.NodeJNI


// Gradle bug, after project clean, MIA, specific for android
import nl.sison.android.nodejs.BuildConfig

import org.xtendroid.annotations.AddLogTag

import java.lang.reflect.Method


@AddLogTag
class NodeSensorBaseService extends Service implements SensorEventListener {

    Handler mainHandler = new Handler(Looper.getMainLooper())

    override onBind(Intent intent)
    {
        return null // we will not bind this
    }

    protected Sensor mSensor
    protected SensorManager mSensorManager
    protected val SENSOR_TYPE = Sensor.TYPE_ALL
    protected val SENSOR_DELAY = SensorManager.SENSOR_DELAY_NORMAL
    protected def void startSensor()
    {
        // Caveat: there could be multiple sensors of a type,
        // this implementation assumes there is one of each type
        // feel free to override :)
        // /data/data/nl.sison.android.nodejs.repl/cache/sensor_sockets.TYPE/ALL
        Log.d(TAG, 'Getting sensor service: ' + SENSOR_TYPE_NAME)
        mSensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager

        var sensorList = mSensorManager.getSensorList(SENSOR_TYPE)

        if (sensorList.size > 0)
        {
            //mainHandler.post[ Log.i(TAG, String.format('There are multiple sensors for the %s sensor', SENSOR_TYPE_NAME)) ]
            sensorList.forEach[ s |
                Log.i(TAG, String.format('available %s sensor: %s', SENSOR_TYPE_NAME, s.name))
            ]
        }

        mSensor = mSensorManager.getDefaultSensor(SENSOR_TYPE)

        if (mSensor != null)
        {
            mSensorManager.registerListener(this, mSensor, SENSOR_DELAY)
            Log.d(TAG, 'Running sensor service: ' + SENSOR_TYPE_NAME)
        }else
            {stopSelf} // harakiri
    }

    val bufSize = 1024 // hopefully this is more than enuff
    LocalServerSocket mLocalServerSocket
    LocalSocket       mLocalSocketSender

    /**
     * Must overload to determine the type of sensor being started
     * and differentiate the domain socket (abstract or not)
     */
    protected val SENSOR_TYPE_NAME = 'ALL'
    val SOCKET_STREAM = 2
    val BACK_LOG = 50
    var FileDescriptor mFileDescriptor
    def startLocalServerSocket()
    {
        // /data/data/nl.sison.android.nodejs.repl/cache/sensor_sockets.TYPE/ALL
        var location = TextUtils.concat(cacheDir.toString, '/sensor_sockets.TYPE/', SENSOR_TYPE_NAME).toString
        try {
            val addr = new LocalSocketAddress(location, LocalSocketAddress.Namespace.FILESYSTEM)
/*
            // LocalSocketServer::new
            val clazz = Class.forName('android.net.LocalSocketImpl')
            val ctors = clazz.declaredConstructors

            ctors.forEach[ c | Log.d(TAG, c.toString) ]

            // Change the accessible property of the constructor.
            val ctor = ctors.get(0)
            ctor.accessible = true
            val impl = ctor.newInstance(null)

            clazz.methods.forEach[ m | Log.d(TAG, m.toString) ]

            val create = clazz.getDeclaredMethod('create', #[ Integer.TYPE ])
            create.invoke(impl, #[ SOCKET_STREAM ]) // 4.4: IOException no fd.

            Thread.sleep(2000)

            val bind = clazz.getDeclaredMethod('bind', #[ LocalSocketAddress ])
            bind.invoke(impl, #[ addr ])

            val listen = clazz.getDeclaredMethod('listen', #[ Integer.TYPE ])
            listen.invoke(impl, #[ BACK_LOG ])

            // LocalSocketServer::accept
            val impl2 = ctor.newInstance(null)
            val accept = clazz.getDeclaredMethod('accept', #[ clazz ])
            accept.invoke(impl, #[ impl2 ])

            //var ls = new LocalSocket(impl2, 2)
            val lsCtors = Class.forName('android.net.LocalSocket').declaredConstructors

            val lsCtor = lsCtors.get(3)
            lsCtor.accessible = true

            // The following will block, so no funky busy loops with sleep necessary
            mLocalSocketSender = lsCtor.newInstance(impl2, SOCKET_STREAM) as LocalSocket
*/
            // First attempt, starting the 2nd...
            // phrack, android.system.Os is only available from api level 21
/*
            if (android.os.Build.VERSION.SDK_INT == 21)
            {
                val AF_UNIX = 1
                mFileDescriptor = Os.socket(AF_UNIX, SOCKET_STREAM, 0)

                //mLocalServerSocket = new LocalServerSocket(mFileDescriptor) // dead end, no bind

                // LocalSocketServer::new
                val clazz = Class.forName('android.net.LocalSocketImpl')
                val ctors = clazz.declaredConstructors

                // Change the accessible property of the constructor.
                val ctor = ctors.get(1)
                ctor.accessible = true
                val impl = ctor.newInstance(mFileDescriptor)

                val bind = clazz.getDeclaredMethod('bind', #[ LocalSocketAddress ])
                bind.invoke(impl, #[ addr ])

                val listen = clazz.getDeclaredMethod('listen', #[ Integer.TYPE ])
                listen.invoke(impl, #[ BACK_LOG ])
            }else
            {
                // use own NDK JNI solution... fragmentation blues...
            }
*/
            // finally...
            // LocalSocketServer::new
            mLocalServerSocket =
                new LocalServerSocket(NodeJNI.createLocalSocket(location).createFileDescriptor)

            // The following will block, so no funky busy loops with sleep necessary
            mLocalSocketSender = mLocalServerSocket.accept
        }catch (IOException e)
        {
            Log.e(TAG, e.message)
            stopSelf
        }
    }

{'fraction_non_alphanumeric': 0.08760024676125848, 'fraction_numerical': 0.004549660703269586, 'mean_word_length': 2.294969512195122, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3873631', 'n_tokens_mistral': 3689, 'n_tokens_neox': 3575, 'n_words': 865}