oonimkall

package
v0.20.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2020 License: BSD-3-Clause Imports: 14 Imported by: 0

README

Package github.com/ooni/probe-engine/oonimkall

Package oonimkall implements APIs used by OONI mobile apps. We expose these APIs to mobile apps using gomobile.

We expose two APIs: the task API, which is derived from the API originally exposed by Measurement Kit, and the session API, which is a Go API that mobile apps can use via gomobile.

This package is named oonimkall because it contains a partial reimplementation of the mkall API implemented by Measurement Kit in, e.g., mkall-ios.

The basic tenet of the task API is that you define an experiment task you wanna run using a JSON, then you start a task for it, and you receive events as serialized JSONs. In addition to this functionality, we also include extra APIs used by OONI mobile.

The basic tenet of the session API is that you create an instance of Session and use it to perform the operations you need.

Documentation

Overview

Package oonimkall implements APIs used by OONI mobile apps. We expose these APIs to mobile apps using gomobile.

We expose two APIs: the task API, which is derived from the API originally exposed by Measurement Kit, and the session API, which is a Go API that mobile apps can use via `gomobile`.

This package is named oonimkall because it contains a partial reimplementation of the mkall API implemented by Measurement Kit in, e.g., https://github.com/measurement-kit/mkall-ios.

Task API

The basic tenet of the task API is that you define an experiment task you wanna run using a JSON, then you start a task for it, and you receive events as serialized JSONs. In addition to this functionality, we also include extra APIs used by OONI mobile.

The task API was first defined in Measurement Kit v0.9.0. In this context, it was called "the FFI API". The API we expose here is not strictly an FFI API, but is close enough for the purpose of using OONI from Android and iOS. See https://git.io/Jv4Rv (measurement-kit/measurement-kit@v0.10.9) for a comprehensive description of MK's FFI API.

See also https://github.com/ooni/probe-engine/pull/347 for the design document describing the task API.

See also https://github.com/ooni/probe-engine/blob/master/DESIGN.md, which explains why we implemented the oonimkall API.

Session API

The Session API is a Go API that can be exported to mobile apps using the gomobile tool. The latest design document for this API is at https://github.com/ooni/probe-engine/pull/954.

The basic tenet of the session API is that you create an instance of `Session` and use it to perform the operations you need.

Index

Constants

This section is empty.

Variables

View Source
var (
	ActiveSessions = &AtomicInt64{atomicx.NewInt64()}
	ActiveContexts = &AtomicInt64{atomicx.NewInt64()}
)

The following two variables contain metrics pertaining to the number of Sessions and Contexts that are currently being used.

Functions

func NewUUID4 added in v0.15.0

func NewUUID4() string

NewUUID4 generates a new UUID4 string. This functionality is typically used by mobile apps to generate random unique identifiers.

Types

type AtomicInt64 added in v0.19.0

type AtomicInt64 struct {
	*atomicx.Int64
}

AtomicInt64 allows us to export atomicx.Int64 variables to mobile libraries so we can use them in testing.

type Context added in v0.17.0

type Context struct {
	// contains filtered or unexported fields
}

Context is the context of an operation. You use this context to cancel a long running operation by calling Cancel(). Because you create a Context from a Session and because the Session is keeping track of the Context instances it owns, you do don't need to call the Cancel method when you're done.

func (*Context) Cancel added in v0.17.0

func (ctx *Context) Cancel()

Cancel cancels pending operations using this context.

type GeolocateResults added in v0.17.0

type GeolocateResults struct {
	// ASN is the autonomous system number.
	ASN string

	// Country is the country code.
	Country string

	// IP is the IP address.
	IP string

	// Org is the commercial name of the ASN.
	Org string
}

GeolocateResults contains the GeolocateTask results.

type Logger added in v0.17.0

type Logger interface {
	Debug(msg string)
	Info(msg string)
	Warn(msg string)
}

Logger is the logger used by a Session. You should implement a class compatible with this interface in Java/ObjC and then save a reference to this instance in the SessionConfig object. All log messages that the Session will generate will be routed to this Logger.

type Session added in v0.17.0

type Session struct {
	// contains filtered or unexported fields
}

Session contains shared state for running experiments and/or other OONI related task (e.g. geolocation). Note that the Session isn't mean to be a long living object. The workflow is to create a Session, do the operations you need to do with it now, then make sure it is not referenced by other variables, so the Go GC can finalize it.

Future directions

We will eventually rewrite the code for running new experiments such that a Task will be created from a Session, such that experiments could share the same Session and save geolookups, etc. For now, we are in the suboptimal situations where Tasks create, use, and close their own session, thus running more lookups than needed.

func NewSession added in v0.17.0

func NewSession(config *SessionConfig) (*Session, error)

NewSession creates a new session. You should use a session for running a set of operations in a relatively short time frame. You SHOULD NOT create a single session and keep it all alive for the whole app lifecyle, since the Session code is not specifically designed for this use case.

func (*Session) Geolocate added in v0.17.0

func (sess *Session) Geolocate(ctx *Context) (*GeolocateResults, error)

Geolocate performs a geolocate operation and returns the results. This method is (in Java terminology) synchronized with the session instance.

func (*Session) MaybeUpdateResources added in v0.17.0

func (sess *Session) MaybeUpdateResources(ctx *Context) error

MaybeUpdateResources ensures that resources are up to date.

func (*Session) NewContext added in v0.17.0

func (sess *Session) NewContext() *Context

NewContext creates an new interruptible Context.

func (*Session) NewContextWithTimeout added in v0.17.0

func (sess *Session) NewContextWithTimeout(timeout int64) *Context

NewContextWithTimeout creates an new interruptible Context that will automatically cancel itself after the given timeout. Setting a zero or negative timeout implies there is no actual timeout configured for the Context.

func (*Session) Submit added in v0.17.0

func (sess *Session) Submit(ctx *Context, measurement string) (*SubmitMeasurementResults, error)

Submit submits the given measurement and returns the results. This method is (in Java terminology) synchronized with the Session instance.

type SessionConfig added in v0.17.0

type SessionConfig struct {
	// AssetsDir is the mandatory directory where to store assets
	// required by a Session, e.g. MaxMind DB files.
	AssetsDir string

	// Logger is the optional logger that will receive all the
	// log messages generated by a Session. If this field is nil
	// then the session will not emit any log message.
	Logger Logger

	// ProbeServicesURL allows you to optionally force the
	// usage of an alternative probe service instance. This setting
	// should only be used for implementing integration tests.
	ProbeServicesURL string

	// SoftwareName is the mandatory name of the application
	// that will be using the new Session.
	SoftwareName string

	// SoftwareVersion is the mandatory version of the application
	// that will be using the new Session.
	SoftwareVersion string

	// StateDir is the mandatory directory where to store state
	// information required by a Session.
	StateDir string

	// TempDir is the mandatory directory where the Session shall
	// store temporary files. Among other tasks, Session.Close will
	// remove any temporary file created within this Session.
	TempDir string

	// Verbose is optional. If there is a non-null Logger and this
	// field is true, then the Logger will also receive Debug messages,
	// otherwise it will not receive such messages.
	Verbose bool
}

SessionConfig contains configuration for a Session. You should fill all the mandatory fields and could also optionally fill some of the optional fields. Then pass this struct to NewSession.

type SubmitMeasurementResults added in v0.17.0

type SubmitMeasurementResults struct {
	UpdatedMeasurement string
	UpdatedReportID    string
}

SubmitMeasurementResults contains the results of a single measurement submission to the OONI backends using the OONI collector API.

type Task

type Task struct {
	// contains filtered or unexported fields
}

Task is an asynchronous task running an experiment. It mimics the namesake concept initially implemented in Measurement Kit.

Future directions

Currently Task and Session are two unrelated APIs. As part of evolving the APIs with which apps interact with the engine, we will modify Task to run in the context of a Session. We will do that to save extra lookups and to allow several experiments running as subsequent Tasks to reuse the Session connections created with the OONI probe services backends.

func StartTask

func StartTask(input string) (*Task, error)

StartTask starts an asynchronous task. The input argument is a serialized JSON conforming to MK v0.10.9's API.

func (*Task) Interrupt

func (t *Task) Interrupt()

Interrupt interrupts the task.

func (*Task) IsDone

func (t *Task) IsDone() bool

IsDone returns true if the task is done.

func (*Task) WaitForNextEvent

func (t *Task) WaitForNextEvent() string

WaitForNextEvent blocks until the next event occurs. The returned string is a serialized JSON following MK v0.10.9's API.

Directories

Path Synopsis
Package tasks implements tasks run using the oonimkall API.
Package tasks implements tasks run using the oonimkall API.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL