Changeset 24

Show
Ignore:
Timestamp:
05/31/06 05:38:41 (3 years ago)
Author:
scott
Message:

IOChannel Integration

  • Made first steps toward integrating IOChannel with the event framework
  • Added constants to IOChannel for watch types
    • IOChannel.READ for read-ready events
    • IOChannel.WRITE for write-ready events
    • IOChannel.READ_PRIORITY for read-now (buffer full type) high proprity events
    • IOChannel.ERROR for read/write error events
    • IOChannel.HANGUP for sig-hup and disconnect events
    • IOChannel.INVALID for other error types (closed filehandle, etc)
  • Added close method to IOChannel objects
  • Added watch method (currently non-functional)
  • Fixed error handling
    • undefined references to sagot_iochannel_error
    • sagot_iochannel_error and GError object dereferencing problems
  • Fixed read, readline, getc methods
    • Problem with return value assignments
    • Buffer lengths were using JSVAL_TO_DOUBLE which gives us poor behavior

Still lots of warnings at compile time

refs #45, #46

Location:
trunk/sagot/src
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/sagot/src/sagot_glib_filechannel.c

    r23 r24  
    2626    {"read",           sagot_iochannel_read,          1,0,0 }, 
    2727    {"getc",           sagot_iochannel_getc,          0,0,0 }, 
     28    {"close",          sagot_iochannel_close,         0,0,0 }, 
    2829}; 
    2930 
  • trunk/sagot/src/sagot_glib_iochannel.c

    r23 r24  
    2727    {"read",           sagot_iochannel_read,          1,0,0 }, 
    2828    {"getc",           sagot_iochannel_getc,          0,0,0 }, 
     29    {"close",          sagot_iochannel_close,         0,0,0 }, 
     30    {"watch",          sagot_iochannel_watch,         1,0,0 }, 
    2931}; 
    3032 
     
    3537JSFunctionSpec iochannel_methods_static[] = { 
    3638}; 
     39 
     40 
     41JSPropertySpec iochannel_properties_static[] = { 
     42    {"READ", G_IO_IN, JSPROP_READONLY | JSPROP_PERMANENT,  
     43        sagot_iochannel_gettype, NULL }, 
     44    {"WRITE", G_IO_OUT, JSPROP_READONLY | JSPROP_PERMANENT,  
     45        sagot_iochannel_gettype, NULL }, 
     46    {"READ_PRIORTY", G_IO_PRI, JSPROP_READONLY | JSPROP_PERMANENT,  
     47        sagot_iochannel_gettype, NULL }, 
     48    {"ERROR", G_IO_ERR, JSPROP_READONLY | JSPROP_PERMANENT,  
     49        sagot_iochannel_gettype, NULL }, 
     50    {"HANGUP", G_IO_HUP, JSPROP_READONLY | JSPROP_PERMANENT,  
     51        sagot_iochannel_gettype, NULL }, 
     52    {"INVALID", G_IO_NVAL, JSPROP_READONLY | JSPROP_PERMANENT,  
     53        sagot_iochannel_gettype, NULL }, 
     54}; 
     55 
    3756/** 
    3857 * Create a glib unix channel. 
     
    6786} 
    6887 
     88void sagot_iochannel_error (JSContext *cx, char *function, GError **error) { 
     89    char *pf = "%s unable to read stream: %s"; 
     90    size_t msglen = strlen(function) + strlen(pf) + strlen(error[0]->message); 
     91    char *message = g_malloc(msglen); 
     92 
     93    snprintf(message, msglen, pf, function, error[0]->message); 
     94    JS_ReportError(cx, message); 
     95    g_free(message); 
     96} 
     97 
    6998bool sagot_iochannel_fd_get (JSContext *cx, JSObject *object, jsval id, 
    7099        jsval *vp) { 
     
    72101     * and returning the already stored version. But I don't expect it to be 
    73102     * called often. */ 
    74     return JS_NewNumberValue(cx,  
     103    *vp = JS_NewNumberValue(cx,  
    75104            g_io_channel_unix_get_fd( JS_GetPrivate(cx, object) ), vp); 
     105 
     106    return true; 
     107} 
     108 
     109bool sagot_iochannel_gettype (JSContext *cx, JSObject *object, jsval id, 
     110        jsval *vp) { 
     111    /* This is kind of a hack...but it works... ehh ;-) */ 
     112    *vp = id; 
     113    return true; 
    76114} 
    77115 
     
    81119 
    82120    if (JSVAL_IS_NUMBER(argv[0])) { 
    83         size_t buflen = (size_t) JSVAL_TO_DOUBLE(argv[0]); 
     121        size_t buflen = (size_t) JSVAL_TO_INT(argv[0]); 
    84122        size_t bytesread; 
    85123        char *buffer = g_malloc(buflen); 
     
    100138 
    101139        JSString *rdata = JS_NewStringCopyN(cx, buffer, bytesread); 
    102         rval = STRING_TO_JSVAL(rdata); 
     140        *rval = STRING_TO_JSVAL(rdata); 
    103141        g_free(buffer); 
    104142 
     
    112150 
    113151    GError **error = NULL; 
    114     gchar *line; 
    115     g_io_channel_read_line(channel, &line, NULL, NULL, error); 
     152    size_t bytesread; 
     153    size_t tpos; 
     154    char *line; 
     155    g_io_channel_read_line(channel, &line, &bytesread, &tpos, error); 
    116156 
    117157    if (!error) { 
    118         rval = STRING_TO_JSVAL( JS_NewString(cx, line, strlen(line)) ); 
     158        *rval = STRING_TO_JSVAL( JS_NewStringCopyN(cx, line, tpos) ); 
    119159        g_free(line); 
    120160        return true; 
    121161    } 
    122162    else { 
    123         sagot_iochannel_error("readline()", error); 
     163        sagot_iochannel_error(cx, "readline()", error); 
    124164        return false; 
    125165    } 
     
    135175    if (!error) { 
    136176        jschar jsunichars[] = { in, 0 }; 
    137         rval = STRING_TO_JSVAL( JS_NewUCStringCopyN(cx, jsunichars, 1) ); 
     177        *rval = STRING_TO_JSVAL( JS_NewUCStringCopyN(cx, jsunichars, 1) ); 
    138178        return true; 
    139179    } 
    140180    else { 
    141         sagot_iochannel_error("getc()", error); 
     181        sagot_iochannel_error(cx, "getc()", error); 
    142182        return false; 
     183    } 
     184} 
     185 
     186bool sagot_iochannel_close (JSContext *cx, JSObject *self, uintN argc, 
     187        jsval *argv, jsval *rval) { 
     188    GIOChannel *channel = JS_GetPrivate(cx, self); 
     189    GError **error = NULL; 
     190    g_io_channel_shutdown(channel, true, error); 
     191 
     192    if (error) { 
     193        sagot_iochannel_error(cx, "close()", error); 
     194    } 
     195} 
     196 
     197bool sagot_iochannel_watch (JSContext *cx, JSObject *self, uintN argc, 
     198        jsval *argv, jsval *rval) { 
     199    GIOChannel *channel = JS_GetPrivate(cx, self); 
     200    GError **error = NULL; 
     201    g_io_channel_shutdown(channel, true, error); 
     202 
     203    if (error) { 
     204        sagot_iochannel_error(cx, "close()", error); 
    143205    } 
    144206} 
     
    146208JSObject * sagot_InitIOChannelClass (JSContext *cx, JSObject *object) { 
    147209    JSObject *proto = JS_InitClass(cx, object, NULL, &iochannel_class, 
    148             IOChannel, 2, NULL, iochannel_methods, NULL, 
    149             iochannel_methods_static); 
     210            IOChannel, 2, iochannel_properties, iochannel_methods, 
     211            iochannel_properties_static, iochannel_methods_static); 
    150212 
    151213    return !proto ? NULL : proto; 
  • trunk/sagot/src/sagot_glib_iochannel.h

    r23 r24  
    2626        jsval *vp); 
    2727 
     28extern bool sagot_iochannel_gettype (JSContext *cx, JSObject *object, jsval id, 
     29        jsval *vp); 
     30 
    2831extern bool sagot_iochannel_read (JSContext *cx, JSObject *object, uintN argc,  
    2932        jsval *argv, jsval *rval); 
     
    3538        jsval *argv, jsval *rval); 
    3639 
     40extern bool sagot_iochannel_close (JSContext *cx, JSObject *self, uintN argc, 
     41        jsval *argv, jsval *rval); 
     42 
     43extern bool sagot_iochannel_watch (JSContext *cx, JSObject *self, uintN argc, 
     44        jsval *argv, jsval *rval); 
     45 
    3746JSObject * sagot_InitIOChannelClass (JSContext *cx, JSObject *object);