Skip to content

Instantly share code, notes, and snippets.

@tdfischer
Last active August 29, 2015 13:59
Show Gist options
  • Save tdfischer/10570660 to your computer and use it in GitHub Desktop.
Save tdfischer/10570660 to your computer and use it in GitHub Desktop.
// A pin has been written to and its value updated
void cb_write (BigEarSensorValue *sensor_value, GValue *value)
{
gchar *name;
gchar *contents;
g_object_get (sensor_value, "name", name);
contents = g_strdup_value_contents (value);
g_print ("Write to sensor's %s: %s\n", name, contents);
g_free (contents);
g_free (name);
}
// A pin's value has changed and is ready to be read from
void cb_read (BigEarSensorValue *sensor_value, GValue *value)
{
gchar *name;
gchar *contents;
g_object_get (sensor_value, "name", name);
contents = g_strdup_value_contents (value);
g_print ("Read from sensor's %s: %s\n", name, contents);
g_free (contents);
g_free (name);
}
int main(int argc, char** argv)
{
GMainLoop *loop = NULL;
BigEarSensor sensor = NULL;
loop = g_main_loop_new (NULL, FALSE);
// Find the first sensor named "tty" in this swarm
sensor = bigear_sensor_find ("tty");
if (sensor) {
// When these pin values are ready for reading or has been written to, call the appropriate callback
bigear_sensor_watch_value ("stdin", WATCH_READ, cb_read);
bigear_sensor_watch_value ("stdout", WATCH_WRITE, cb_write);
bigear_sensor_watch_value ("stderr", WATCH_WRITE, cb_write);
// Read the current PID that we're connected to
GValue *input = bigear_sensor_read_value ("pid");
guint pid;
g_value_get_uint (input, &pid);
g_printf ("Attached to remote PID %s\n", pid);
g_main_loop_run (loop);
} else {
g_printf ("Could not find a TTY sensor\n");
}
}
#include <graviton/server.h>
#include <graviton/control.h>
#include <glib.h>
BigEarSensor *big_ear_sensor_new(gchar **name, FLAGS flags)
{
return g_object_new (BIG_EAR_SENSOR_TYPE, "name", name, "flags" flags);
}
void big_ear_sensor_init(BigEarSensor *self)
{
BigEarSensorPrivate *priv = self->priv = g_new0(BigEarSensorPrivate, 1);
priv->server = graviton_server_new ();
GravitonRootControl *root = graviton_server_get_root_control (priv->server);
GravitonControl *sensorService = graviton_control_new ("net:phrobo:bigear:sensor");
graviton_control_add_subcontrol (GRAVITON_CONTROL (root), sensorService);
graviton_control_add_method (sensorService, "wire", cb_wire, NULL, NULL);
graviton_control_add_method (sensorService, "unwire", cb_unwire, NULL, NULL);
graviton_control_add_method (sensorService, "subscribe", cb_subscribe, NULL, NULL);
graviton_control_add_method (sensorService, "unsubscribe", cb_unsubscribe, NULL, NULL);
graviton_control_add_method (sensorService, "notify", cb_notify, NULL, NULL);
return sensor;
}
struct _BigEarSensor
{
gchar *name;
GHashTable *values;
};
struct _BigEarSensorClass
{
gboolean (*start_watching_value) (BigEarSensor *self, BigEarSensorValue *value, GError **error);
void (*stop_watching_value) (BigEarSensor *self, BigEarSensorValue *value, GError **error);
GVariant *(*read_value) (BigEarSensor *self, BigEarSensorValue *value, GError **error);
GVariant *(*write_value) (BigEarSensor *self, BigEarSensorValue *value, GError **error);
};
typedef gboolean (*bigear_sensor_value_start_watching) (BigEarSensorValue *self, gpointer data, GError **error);
typedef gboolean *(*bigear_sensor_value_stop_watching) (BigEarSensorValue *self, gpointer data, GError **error);
typedef GVariant *(*bigear_sensor_value_read) (BigEarSensorValue *self, gpointer data, GError **error);
typedef gboolean (*bigear_sensor_value_write) (BigEarSensorValue *self, const GVariant *value, gpointer data, GError **error);
struct BigEarSensorValue
{
gchar *name;
};
struct BigEarSensorCallbacks
{
bigear_sensor_value_start_watching start;
bigear_sensor_value_stop_watching stop;
bigear_sensor_value_read read;
bigear_sensor_value_write write;
};
BigEarSensorValue *bigear_sensor_add_value (BigEarSensor *sensor,
const gchar* name,
BigEarSensorCallbacks);
int main(int argc, char** argv)
{
GMainLoop *loop = NULL;
BigEarSensor sensor = NULL;
loop = g_main_loop_new (NULL, FALSE);
sensor = bigear_sensor_new ("tty");
BigEarSensorCallbacks cbs = {
.start = cb_start, // Sensor should be opened for polling
.stop = cb_stop, // Sensor should be closed and polling stopped
.read = cb_read, // A read is requested
.write = cb_write // A write is requested
};
// Expose four values by mapping names to callbacks
bigear_sensor_add_value (sensor, "stdin", cbs, stdin);
bigear_sensor_add_value (sensor, "stdout", cbs, stdout);
bigear_sensor_add_value (sensor, "stderr", cbs, stderr);
bigear_sensor_add_value (sensor, "pid", cbs, NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment