📄 pushregistry.c
字号:
/* fprintf (stderr, "Warning: could not read alarm registration: %s\n", errStr);*/ storageFreeError(errStr); return -1; } /* End of File */ if (len <= 0){ break ; } /* End of Line */ if ((buffer[i] == '\n') || (i == MAX_LINE )) { line++; buffer[i] = 0; /* Skip comment lines which begin with '#'*/ if (buffer[0] != '#') { /* Find the alarm time field. */ for (p = buffer ; *p ; p++) { if (*p == ',') { p++; alarm = atoll(p); break; } } /* Create an alarm registry entry. */ if (pe = (AlarmEntry *) midpMalloc (sizeof(AlarmEntry))){ pe->next = alarmlist; pe->midlet = midpStrdup(buffer); pe->wakeup = alarm; pe->storagename = midpStrdup(pushstorage(pe->midlet, 2)); if ((pe->midlet == NULL) || (pe->storagename == NULL)) { midpFree(pe->midlet); midpFree(pe->storagename); midpFree(pe); pe = NULL; } } if (pe == NULL) { alarmListFree(); return -2; } /* * Add the new entry to the top of the alarm cached * list. */ alarmlist = pe ; } } /* Ignore carriage returns */ if (buffer[i] == '\r') { i-- ; } } return 0;}/* * Open the Alram Registry file, if it exists and populate * an in memory cache of the file contents. */static void alarmopen() { /* Now read the registered connections.*/ if ((pushfd = storageOpen(&errStr, alarmpathname, OPEN_READ)) != -1){ /* Read through the file one line at a time */ if (parseAlarmList() == -2) { fprintf(stderr, "Error: alarmopen out of memory " "when parsing alarm list.\n"); exit(-1); } /* Close the storage handle */ storageClose (&errStr, pushfd); } else { if (errStr != NULL) { /* fprintf (stderr, "Warning: could not open alarm registration file(%s): %s\n", alarmpathname, errStr); */ storageFreeError(errStr); } }}/* * Save the in memory cache of alarm registrations to a persistent * file for use in subsequent runs. */static void alarmsave() { AlarmEntry *alarmp; AlarmEntry *alarmtmp=NULL; if ((pushfd = storageOpen(&errStr, alarmpathname, OPEN_READ_WRITE_TRUNCATE)) != -1){ /* Write a new list of push registrations to the persistent file */ for (alarmp = alarmlist; alarmp != NULL ; alarmp = alarmtmp) { alarmtmp = alarmp->next; storageWrite(&errStr, pushfd, alarmp->midlet, strlen(alarmp->midlet)); storageWrite(&errStr, pushfd, "\n", 1); } /* Close the storage handle */ storageClose (&errStr, pushfd); } else { if (errStr != NULL) { /* fprintf (stderr, "Warning: could not write alarm registration file(%s): %s\n", alarmpathname, errStr); */ storageFreeError(errStr); return; } }}/* * Add one entry to the alarm registry. * If the entry already exists return previous alarm time. * On succesful registration, write a new copy of the file to disk. */static int alarmadd(char *str, jlong alarm, jlong *lastalarm){ AlarmEntry *alarmp; AlarmEntry *alarmtmp = NULL; AlarmEntry *lastp = alarmlist; AlarmEntry *pe = NULL; char *ptr; int len; /* Find the length of the midlet field */ for (ptr = str, len = 0; *ptr != 0 ; ptr++, len++) { if (*ptr == ',') break; } /* Check if the entry already exists? */ for (alarmp = alarmlist; alarmp != NULL ; alarmp = alarmtmp) { alarmtmp = alarmp->next; if (strncmp (str, alarmp->midlet, len) == 0) { jlong temp = alarmp->wakeup; if(alarm == 0) { /* Remove an entry. */ if (lastp == alarmlist){ alarmlist = alarmp->next; } else { lastp->next = alarmp->next; } midpFree(alarmp->midlet); midpFree(alarmp->storagename); midpFree(alarmp); alarmsave(); } else { /* Replace an entry. */ alarmp->wakeup = alarm; alarmsave(); } *lastalarm = temp; return 0; } lastp = alarmp; } /* Add a new entry. */ if (alarm && (pe = (AlarmEntry *) midpMalloc (sizeof(AlarmEntry)))){ pe->next = alarmlist ; pe->midlet = midpStrdup(str); pe->wakeup = alarm ; pe->storagename = midpStrdup(pushstorage(pe->midlet, 2)); if ((pe->midlet == NULL) || (pe->storagename == NULL)) { midpFree(pe->midlet); midpFree(pe->storagename); midpFree(pe); pe = NULL; } else { alarmlist = pe ; } } if (pe == NULL) { return -2; } alarmsave(); return 0 ; } /*========================================================================= * FUNCTION: Object del0(String) (STATIC) * CLASS: com.sun.midp.io.j2me.push.PushRegistryImpl.del0 * TYPE: virtual native function * OVERVIEW: Delete an entry from the push registry * INTERFACE (operand stack manipulation): * parameters: a string object , a string object * returns: 0 if succesfully deleted, -1 if connection was not found * -2 if connection found but belongs to another *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_INTJava_com_sun_midp_io_j2me_push_PushRegistryImpl_del0() { char *szConn = NULL; int connLen; char *szStore = NULL; int storeLen; int ret = -1; KNI_StartHandles(2); KNI_DeclareHandle(conn); KNI_DeclareHandle(storage); /* Get the connection string. */ KNI_GetParameterAsObject(1, conn); connLen = KNI_GetArrayLength(conn); if ((szConn = midpMalloc(connLen)) != NULL) { KNI_GetRawArrayRegion(conn, 0, connLen, (jbyte*)szConn); /* Get the storage name string. */ KNI_GetParameterAsObject(2, storage); storeLen = KNI_GetArrayLength(storage); if ((szStore = midpMalloc(storeLen)) != NULL) { KNI_GetRawArrayRegion(storage, 0, storeLen, (jbyte*)szStore); /* Perform the delete operation. */ ret = pushdel(szConn, szStore); midpFree(szStore); } else { KNI_ThrowNew("java/lang/OutOfMemoryError", "connection"); } midpFree(szConn); } else { KNI_ThrowNew("java/lang/OutOfMemoryError", "connection"); } KNI_EndHandles(); KNI_ReturnInt(ret);}/*========================================================================= * FUNCTION: Object checkInByName0(String) (STATIC) * CLASS: com.sun.midp.io.j2me.push.PushRegistryImpl * TYPE: virtual native function * OVERVIEW: check in a handle back into the push registry * INTERFACE (operand stack manipulation): * parameters: a string object , int flag * returns: 0 if succesfully checked in, -1 if connection was not found *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_INTJava_com_sun_midp_io_j2me_push_PushRegistryImpl_checkInByName0() { char *szConn = NULL; int connLen; int ret = -1; KNI_StartHandles(1); KNI_DeclareHandle(conn); KNI_GetParameterAsObject(1, conn); connLen = KNI_GetArrayLength(conn); szConn = midpMalloc(connLen); if (szConn != NULL) { KNI_GetRawArrayRegion(conn, 0, connLen, (jbyte*)szConn); ret = pushcheckinbyname(szConn); midpFree(szConn); } else { KNI_ThrowNew("java/lang/OutOfMemoryError", "connection"); } KNI_EndHandles(); KNI_ReturnInt(ret);}/*========================================================================= * FUNCTION: Object add0(String) (STATIC) * CLASS: com.sun.midp.io.j2me.PushRegistryImpl.add0 * TYPE: virtual native function * OVERVIEW: Add an entry from the push registry * INTERFACE (operand stack manipulation): * parameters: a string object * returns: 0 if succesfully added, -1 if connection was not found *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_INTJava_com_sun_midp_io_j2me_push_PushRegistryImpl_add0() { char *szConn = NULL; int connLen; int ret = -1; KNI_StartHandles(1); KNI_DeclareHandle(conn); KNI_GetParameterAsObject(1, conn); connLen = KNI_GetArrayLength(conn); szConn = midpMalloc(connLen); if (szConn != NULL) { KNI_GetRawArrayRegion(conn, 0, connLen, (jbyte*)szConn); ret = pushadd(szConn); midpFree(szConn); } if ((szConn == NULL) || (ret == -2)) { KNI_ThrowNew("java/lang/OutOfMemoryError", "connection"); } KNI_EndHandles(); KNI_ReturnInt(ret);}/*========================================================================= * FUNCTION: Object addAlarm0(String) (STATIC) * CLASS: com.sun.midp.io.j2me.PushRegistryImpl.addAlarm0 * TYPE: virtual native function * OVERVIEW: Add an entry from the alarm registry * INTERFACE (operand stack manipulation): * parameters: a string object , long * returns: 0 if first time alarm is set, otherwise the previous * defined alarm time *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_LONGJava_com_sun_midp_io_j2me_push_PushRegistryImpl_addAlarm0() { char *szConn = NULL; int connLen; jlong alarm = 0; jlong lastalarm = 0; int ret = 0; alarm = KNI_GetParameterAsLong(2); KNI_StartHandles(1); KNI_DeclareHandle(conn); KNI_GetParameterAsObject(1, conn); connLen = KNI_GetArrayLength(conn); szConn = midpMalloc(connLen); if (szConn != NULL) { KNI_GetRawArrayRegion(conn, 0, connLen, (jbyte*)szConn); ret = alarmadd(szConn, alarm, &lastalarm); midpFree(szConn); } if ((szConn == NULL) || (ret == -2)) { KNI_ThrowNew("java/lang/OutOfMemoryError", "connection"); } KNI_EndHandles(); KNI_ReturnLong(lastalarm);}/*========================================================================= * FUNCTION: Object list0(byte[], boolean, byte[], int) (STATIC) * CLASS: com.sun.midp.io.j2me.push.PushRegistryImpl.list0 * TYPE: virtual native function * OVERVIEW: List registry entries for specified MIDlet * INTERFACE (operand stack manipulation): * parameters: byte[], boolean, byte[], int * returns: *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_INTJava_com_sun_midp_io_j2me_push_PushRegistryImpl_list0() { char midletName[MAX_HOST_LENGTH]; int available; int connsize; int nameLength; char *conn; int ret = -1; available = KNI_GetParameterAsBoolean(2); connsize = KNI_GetParameterAsInt(4); KNI_StartHandles(2); KNI_DeclareHandle(name); KNI_DeclareHandle(connections); KNI_GetParameterAsObject(1, name); KNI_GetParameterAsObject(3, connections); nameLength = KNI_GetArrayLength(name); if (nameLength > MAX_HOST_LENGTH) { KNI_ThrowNew("java/lang/IllegalArugmentException", "MIDlet name too long"); } KNI_GetRawArrayRegion(name, 0, nameLength, (jbyte*)midletName); conn = pushfindsuite(midletName, available); if (conn != NULL) { KNI_SetRawArrayRegion(connections, 0, strlen(conn), (jbyte*)conn); midpFree(conn); ret = 0; } KNI_EndHandles(); KNI_ReturnInt(ret);}/*========================================================================= * FUNCTION: Object poll0() * CLASS: com.sun.midp.io.j2me.push.PushRegistryImpl.poll0 * TYPE: virtual native function * OVERVIEW: Check for inbound connection * INTERFACE (operand stack manipulation): * parameters: long * returns: fd of first active connection *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_LONGJava_com_sun_midp_io_j2me_push_PushRegistryImpl_poll0() { jlong ret = (jlong) pushpoll(); if(ret == -1) { /* If there is no pending I/O, check for alarms */ jlong time = KNI_GetParameterAsLong(1); ret = alarmcheck(time); } KNI_ReturnLong(ret);}/*========================================================================= * FUNCTION: Object getMIDlet0(long, byte[], int) (STATIC) * CLASS: com.sun.midp.io.j2me.push.PushRegistryImpl.getMIDlet0 * TYPE: virtual native function * OVERVIEW: Get the registered MIDlet name given the current active * file descriptor. * INTERFACE (operand stack manipulation): * parameters: fd or time, byte[], int * returns: *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_INTJava_com_sun_midp_io_j2me_push_PushRegistryImpl_getMIDlet0() { int midletsize; char *regentry; int ret = -1; jlong fd; midletsize = KNI_GetParameterAsInt(4); fd = KNI_GetParameterAsLong(1); KNI_StartHandles(1); KNI_DeclareHandle(regObject); KNI_GetParameterAsObject(3, regObject); if (regentry = pushfindfd(fd)) { KNI_SetRawArrayRegion(regObject, 0, strlen(regentry)+1, (jbyte*)regentry); midpFree(regentry); ret = 0; } KNI_EndHandles(); KNI_ReturnInt(ret);}/*========================================================================= * FUNCTION: Object getEntry0(byte[], byte[], int) (STATIC) * CLASS: com.sun.midp.io.j2me.push.PushRegistryImpl.getEntry0 * TYPE: virtual native function * OVERVIEW: Get the registered MIDlet name given the registered * connection string * INTERFACE (operand stack manipulation): * parameters: byte[], byte[], int * returns: *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_INTJava_com_sun_midp_io_j2me_push_PushRegistryImpl_getEntry0() { int midletsize; char *regentry; int regsize ; int ret = -1; int connLen; char *szConn = NULL; midletsize = KNI_GetParameterAsInt(3); KNI_StartHandles(2); KNI_DeclareHandle(conn); KNI_DeclareHandle(regObject); KNI_GetParameterAsObject(1, conn); connLen = KNI_GetArrayLength(conn); ret = -1; if ((szConn = midpMalloc(connLen)) != NULL) { KNI_GetRawArrayRegion(conn, 0, connLen, (jbyte*)szConn); KNI_GetParameterAsObject(2, regObject); if (regentry = pushfindconn(szConn)) { regsize = strlen(regentry) + 1; if (regsize < midletsize) { KNI_SetRawArrayRegion(regObject, 0, regsize, (jbyte*)regentry); ret = 0; } else { KNI_ThrowNew("java/io/IOException", "registration too long"); } } midpFree(szConn); } else { /* Insufficient memory. */ KNI_ThrowNew("java/lang/OutOfMemoryError", "connection"); } KNI_EndHandles(); KNI_ReturnInt(ret);}/*========================================================================= * FUNCTION: Object delAllForSuite0(byte[]) * CLASS: com.sun.midp.io.j2me.PushRegistryImpl * TYPE: virtual native function * OVERVIEW: Remove all the entries of a suite from the push registry * INTERFACE (operand stack manipulation): * parameters: a zero terminated ASCII string containing the storage name * returns: nothing *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_VOIDJava_com_sun_midp_io_j2me_push_PushRegistryImpl_delAllForSuite0() { char* pszStorageName; int len; KNI_StartHandles(1); KNI_DeclareHandle(name); KNI_GetParameterAsObject(1, name); len = KNI_GetArrayLength(name); pszStorageName = (char*)midpMalloc(len); if (pszStorageName == NULL) { KNI_ThrowNew("java/lang/OutOfMemoryError", "del all storage name"); } else { KNI_GetRawArrayRegion(name, 0, len, (jbyte*)pszStorageName); pushdeletesuite(pszStorageName); midpFree(pszStorageName); } KNI_EndHandles(); KNI_ReturnVoid();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -