Skip to content

Commit

Permalink
move the init case into a separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
lehenbauer committed Jan 24, 2017
1 parent 164bcf6 commit 28558f0
Showing 1 changed file with 114 additions and 95 deletions.
209 changes: 114 additions & 95 deletions generic/zookeepertcl.c
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,6 @@ zootcl_EventProc (Tcl_Event *tevPtr, int flags) {
assert(0 == 1);

case INTERNAL_INIT_CALLBACK:
// break;
case WATCHER_CALLBACK:
if (evPtr->watcher.path != NULL) {
if (*evPtr->watcher.path != '\0') {
Expand Down Expand Up @@ -2080,6 +2079,118 @@ zootcl_zookeeperObjectObjCmd(ClientData clientData, Tcl_Interp *interp, int objc
return TCL_OK;
}

/*
*----------------------------------------------------------------------
*
* zootcl_init_subcommand --
*
* implement "init" subcommand of zookeeper::zookeeper command
*
* Results:
* Connects to zookeeper.
* Creates a new zookeeper object.
* A standard Tcl result.
*
*
*----------------------------------------------------------------------
*/
int
zootcl_init_subcommand(Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
{
zootcl_objectClientData *zo = NULL;
int timeout;
Tcl_Obj *callbackObj = NULL;

static CONST char *subOptions[] = {
"-async",
NULL
};

enum subOptions {
SUBOPT_ASYNC
};


if ((objc < 5) || (objc > 7)) {
Tcl_WrongNumArgs (interp, 2, objv, "cmdName hosts timeout ?-async callback?");
return TCL_ERROR;
}


if (Tcl_GetIntFromObj (interp, objv[4], &timeout) == TCL_ERROR) {
return TCL_ERROR;
}

char *cmdName = Tcl_GetString (objv[2]);
char *hosts = Tcl_GetString (objv[3]);
int suboptIndex = 0;
int i;

for (i = 5; i < objc; i++) {
if (Tcl_GetIndexFromObj (interp, objv[i], subOptions, "suboption",
TCL_EXACT, &suboptIndex) != TCL_OK) {
return TCL_ERROR;
}

switch ((enum subOptions) suboptIndex) {
case SUBOPT_ASYNC:
{
if (i + 1 >= objc) {
Tcl_WrongNumArgs (interp, 2, objv, "-async code");
return TCL_ERROR;
}
callbackObj = objv[++i];
Tcl_IncrRefCount (callbackObj);
break;
}
}
}
//
// allocate one of our zookeeper client data objects for Tcl and configure it
zo = (zootcl_objectClientData *)ckalloc (sizeof (zootcl_objectClientData));
zo->zookeeper_object_magic = ZOOKEEPER_OBJECT_MAGIC;
zo->interp = interp;
zo->threadId = Tcl_GetCurrentThread ();
zo->channel = NULL;
zo->currentFD = -1;
zo->initCallbackObj = callbackObj;

zhandle_t *zh = zookeeper_init (hosts, zootcl_init_callback, timeout, NULL, zo, 0);

if (zh == NULL) {
Tcl_SetObjResult (interp, Tcl_NewStringObj (Tcl_PosixError (interp), -1));
return TCL_ERROR;
}

zo->zh = zh;
zoo_set_context (zo->zh, (void *)zo);

// if cmdName is #auto, generate a unique name for the object
int autoGeneratedName = 0;
if (strcmp (cmdName, "#auto") == 0) {
static unsigned long nextAutoCounter = 0;
int baseNameLength;

#define OBJECT_STRING_FORMAT "zookeeper%lu"
baseNameLength = snprintf (NULL, 0, OBJECT_STRING_FORMAT, nextAutoCounter) + 1;
cmdName = ckalloc (baseNameLength);
snprintf (cmdName, baseNameLength, OBJECT_STRING_FORMAT, nextAutoCounter++);
autoGeneratedName = 1;
}

// create a Tcl command to interface to zookeeper
zo->cmdToken = Tcl_CreateObjCommand (interp, cmdName, zootcl_zookeeperObjectObjCmd, zo, zootcl_zookeeperObjectDelete);
Tcl_CreateExitHandler (zootcl_zookeeperObjectDelete, zo);
Tcl_CreateThreadExitHandler (zootcl_zookeeperObjectDelete, zo);
Tcl_SetObjResult (interp, Tcl_NewStringObj (cmdName, -1));
if (autoGeneratedName == 1) {
ckfree(cmdName);
}

Tcl_CreateEventSource (zootcl_EventSetupProc, zootcl_EventCheckProc, (ClientData) zo);
return TCL_OK;
}

/*
*----------------------------------------------------------------------
*
Expand All @@ -2103,7 +2214,6 @@ int
zootcl_zookeeperObjCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
{
int optIndex;
zootcl_objectClientData *zo = NULL;

static CONST char *options[] = {
"init",
Expand Down Expand Up @@ -2145,99 +2255,8 @@ zootcl_zookeeperObjCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_
return TCL_OK;
}

case OPT_INIT: {
int timeout;
Tcl_Obj *callbackObj = NULL;

static CONST char *subOptions[] = {
"-async",
NULL
};

enum subOptions {
SUBOPT_ASYNC
};


if ((objc < 5) || (objc > 7)) {
Tcl_WrongNumArgs (interp, 2, objv, "cmdName hosts timeout ?-async callback?");
return TCL_ERROR;
}


if (Tcl_GetIntFromObj (interp, objv[4], &timeout) == TCL_ERROR) {
return TCL_ERROR;
}

char *cmdName = Tcl_GetString (objv[2]);
char *hosts = Tcl_GetString (objv[3]);
int suboptIndex = 0;
int i;

for (i = 5; i < objc; i++) {
if (Tcl_GetIndexFromObj (interp, objv[i], subOptions, "suboption",
TCL_EXACT, &suboptIndex) != TCL_OK) {
return TCL_ERROR;
}

switch ((enum subOptions) suboptIndex) {
case SUBOPT_ASYNC:
{
if (i + 1 >= objc) {
Tcl_WrongNumArgs (interp, 2, objv, "-async code");
return TCL_ERROR;
}
callbackObj = objv[++i];
Tcl_IncrRefCount (callbackObj);
break;
}
}
}
//
// allocate one of our zookeeper client data objects for Tcl and configure it
zo = (zootcl_objectClientData *)ckalloc (sizeof (zootcl_objectClientData));
zo->zookeeper_object_magic = ZOOKEEPER_OBJECT_MAGIC;
zo->interp = interp;
zo->threadId = Tcl_GetCurrentThread ();
zo->channel = NULL;
zo->currentFD = -1;
zo->initCallbackObj = callbackObj;

zhandle_t *zh = zookeeper_init (hosts, zootcl_init_callback, timeout, NULL, zo, 0);

if (zh == NULL) {
Tcl_SetObjResult (interp, Tcl_NewStringObj (Tcl_PosixError (interp), -1));
return TCL_ERROR;
}

zo->zh = zh;
zoo_set_context (zo->zh, (void *)zo);

// if cmdName is #auto, generate a unique name for the object
int autoGeneratedName = 0;
if (strcmp (cmdName, "#auto") == 0) {
static unsigned long nextAutoCounter = 0;
int baseNameLength;

#define OBJECT_STRING_FORMAT "zookeeper%lu"
baseNameLength = snprintf (NULL, 0, OBJECT_STRING_FORMAT, nextAutoCounter) + 1;
cmdName = ckalloc (baseNameLength);
snprintf (cmdName, baseNameLength, OBJECT_STRING_FORMAT, nextAutoCounter++);
autoGeneratedName = 1;
}

// create a Tcl command to interface to zookeeper
zo->cmdToken = Tcl_CreateObjCommand (interp, cmdName, zootcl_zookeeperObjectObjCmd, zo, zootcl_zookeeperObjectDelete);
Tcl_CreateExitHandler (zootcl_zookeeperObjectDelete, zo);
Tcl_CreateThreadExitHandler (zootcl_zookeeperObjectDelete, zo);
Tcl_SetObjResult (interp, Tcl_NewStringObj (cmdName, -1));
if (autoGeneratedName == 1) {
ckfree(cmdName);
}

Tcl_CreateEventSource (zootcl_EventSetupProc, zootcl_EventCheckProc, (ClientData) zo);
break;
}
case OPT_INIT:
return zootcl_init_subcommand(interp, objc, objv);

case OPT_DEBUG_LEVEL:
{
Expand Down

0 comments on commit 28558f0

Please sign in to comment.