Source

hxuv / ndll / src / nutils.c

Full commit

#include "nuv.h"


value hx_nuv_get_cpu_info () {


  uv_cpu_info_t* cpus;
  int count;

  auto err = uv_cpu_info( &cpus, &count );
  THROW_EXCEPTION( err.code == 0, "GetCPUInfo Failed" );

  value arr = alloc_array( count );

  for( int idx = 0; idx < count; idx++ ) {

    auto cpu = cpus[idx];
    value obj = val_array_ptr(arr)[idx] = alloc_object(val_null);
    alloc_field(obj, val_id("model"), alloc_string(cpu.model));
    alloc_field(obj, val_id("speed"), alloc_float(cpu.speed));
  }

  uv_free_cpu_info(cpus, count);
  return arr;
}


value hx_nuv_get_network_interfaces() {

  char buf[512];
  int count;

  uv_interface_address_t *info;

  uv_interface_addresses( &info, &count );

  value arr = alloc_array( count );

  for( int idx = 0; idx < count; idx++ ) {

    auto iface = info[idx];

    value obj = val_array_ptr(arr)[idx] = alloc_object(val_null);
    alloc_field(obj, val_id("name"), alloc_string(iface.name));
    alloc_field(obj, val_id("internal"), iface.is_internal ? val_true : val_false );

    auto addr = iface.address;
    auto fam = addr.address4.sin_family;
    if(fam == AF_INET) uv_ip4_name( &addr.address4, buf, sizeof(buf) );
    else if(fam == AF_INET6) uv_ip6_name( &addr.address6, buf, sizeof(buf) );

    alloc_field( obj, val_id("version"), alloc_int(fam == AF_INET ? 4 : 6) );
    alloc_field( obj, val_id("ip"), alloc_string(buf) );
  }

  uv_free_interface_addresses(info, count);

  return arr;
}

DEFINE_PRIM( hx_nuv_get_cpu_info, 0);
DEFINE_PRIM( hx_nuv_get_network_interfaces, 0);