📄 pushregistry.c
字号:
* Lookup the midlet name given the file descriptor */static char *pushfindfd(jlong fd) { PushEntry *pushp; PushEntry *pushtmp; int temp_state = AVAILABLE; AlarmEntry *alarmp; AlarmEntry *alarmtmp; char *alarmentry = NULL; char *ipnumber = NULL; struct in_addr addr; /* Find the entry to pass off the open file descriptor. */ for (pushp = pushlist; pushp != NULL ; pushp = pushtmp) { pushtmp = pushp->next; if ((pushp->fd == (int)fd) && (pushp->state != LAUNCH_PENDING)) { temp_state = pushp->state; pushp->state = LAUNCH_PENDING; /* * Check the push filter, to see if this connection * is acceptable. */ if(strncmp(pushp->value,"datagram://:",12) == 0) { /* * Read the datagram and save it til the application reads it. * This is a one datagram message queue. */ if ((pushp->dg = (DatagramEntry*) midpMalloc(MAX_DATAGRAM_LENGTH)) != NULL) { if ((pushp->dg->length = prim_com_sun_midp_io_j2me_datagram_Protocol_receive0( pushp->fd, &(pushp->dg->ipAddress), &(pushp->dg->port), pushp->dg->buffer, MAX_DATAGRAM_LENGTH)) != -1 ) { addr.s_addr = pushp->dg->ipAddress; ipnumber = inet_ntoa(addr); } else { /* Receive failed - no data available. */ midpFree(pushp->dg); pushp->dg = NULL; pushp->state = temp_state; } } } else if(strncmp(pushp->value,"socket://:",10) == 0) { /* * For a server socket connection, accept the inbound * socket connection so the end point filter can be checked. */ if ((pushp->fdsock = prim_com_sun_midp_io_j2me_serversocket_Protocol_accept(pushp->fd)) != -1) { ipnumber = prim_com_sun_midp_io_j2me_socket_Protocol_getipnumber1 (pushp->fdsock, 0); } else { /* Connection is not available, cancel the launch pending. */ pushp->state = temp_state; } } /* Check the IP filter. */ if (checkfilter(pushp->filter, ipnumber)) { return midpStrdup(pushp->value); } else { /* Dispose of the filtered push request. */ /* Close any accepted socket not accessed, yet. */ if (pushp->fdsock != -1) { prim_com_sun_midp_io_j2me_socket_Protocol_close0(pushp->fdsock); pushp->fdsock = -1; } /* Remove any cached datagrams. */ if (pushp->dg != NULL) { midpFree(pushp->dg); pushp->dg = NULL; } pushp->state = temp_state; return NULL; } } } /* * If the file descriptor was not found, it could be * an alarm time. If found, clear the entry so it will * not fire again. */ for (alarmp = alarmlist; alarmp != NULL ; alarmp = alarmtmp) { alarmtmp = alarmp->next; if (alarmp->wakeup == fd) { alarmentry = midpStrdup(alarmp->midlet); if (alarmentry) { jlong lastalarm; alarmadd(alarmentry, 0, &lastalarm); return alarmentry; } else { alarmp->wakeup = 0; } } } return NULL; }/* * Lookup the midlet name given the file descriptor */static char *pushfindconn(char *str) { PushEntry *p; PushEntry *tmp; /* Find the entry to pass off the open file descriptor. */ for (p = pushlist; p != NULL ; p = tmp) { tmp = p->next; if (strncmp (str, p->value, strlen(str)) == 0) { return p->value; } } return NULL; }/* * Lookup the midlet name given the storagename */static char *pushfindsuite(char *store, int available) { PushEntry *p; PushEntry *tmp; char *ret = NULL; char *ptr; int len=0; char *connlist = NULL; /* Find the entry to pass off the open file descriptor. */ for (p = pushlist; p != NULL ; p = tmp) { tmp = p->next; if (strcmp(store, p->storagename) == 0) { ret = midpStrdup(p->value); for (ptr = ret, len=0; *ptr; ptr++, len++) { if (*ptr == ',') { *ptr = '\0'; break; } } /* * Check if there is pending I/O on the * current file descriptor. e.g. an accepted socket * or a cache datagram. */ if (available && (p->fd != -1)) { if ((p->fdsock == -1) && (p->dg == NULL)) { midpFree(ret); ret = NULL; continue; } } /* * Append the entries together in a single list. */ if (connlist == NULL) { connlist= ret; } else { strcat(connlist, ","); strcat(connlist, ret); midpFree(ret); } ret = NULL; } } return connlist;}/* * Parse the persistent push registry from disk into the * in memory cache representation. */static int parsePushList () { char buffer[MAX_LINE+1]; int i; int line = 0; int eol = 0; int len; char *errStr = NULL ; PushEntry * pe; /* Read a line at a time */ for (i= 0 ; i < MAX_LINE ; i++) { /* Reset the buffer pointer at the start of a new line. */ if(eol) { i = 0; eol = 0; } len = storageRead(&errStr, pushfd, &buffer[i], 1); if (errStr != NULL) { /* fprintf (stderr, "Warning: could not read push 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] != '#') { if (pe = (PushEntry *) midpMalloc (sizeof(PushEntry))) { pe->next = pushlist; pe->value = midpStrdup(buffer); pe->storagename = midpStrdup(pushstorage(pe->value, 3)); if ((pe->value == NULL) || (pe->storagename == NULL)) { midpFree(pe->value); midpFree(pe->storagename); midpFree(pe); return -2; } else { pe->filter = pushfilter(pe->value); pe->fdsock = -1; pe->state = AVAILABLE ; pe->dg = NULL; pushProcessPort(buffer, &(pe->fd), &(pe->port)); } } /* * Add the new entry to the top of the push cached * list. */ pushlist = pe ; pushlength++; } /* Reset to beginning of next line. */ eol = 1; } /* Ignore carriage returns */ if (buffer[i] == '\r') { i-- ; } } return 0;}/* * Parse the port number from the connection field * and use it for the connection appropriate open * call. The file descriptor will be included in * the connection registry until needed by the * application level connection open request. */static void pushProcessPort(char *buffer, int *fd, int *port){ char *p; int colon_found; char *exception = NULL; /* * Open the file descriptor so it can be monitored * for inbound connection requests. For MIDP 2.0 * only socket and datagram connections are supported. */ p = buffer; colon_found = 0; *port = -1; for (; *p != '\0' ; p++) { if (*p == ':') { colon_found++ ; } if(colon_found == 2) { p++ ; /* * Parse the port number from the * connection string */ *port = atoi(p); if(strncmp(buffer,"datagram://:",12) == 0) { /* Open the datagram socket */ *fd = prim_com_sun_midp_io_j2me_datagram_Protocol_open0(*port, &exception);#ifndef BLOCKING_NET_IO /* For non-blocking systems only */ prim_com_sun_midp_io_j2me_datagram_Protocol_setNonBlocking(*fd);#endif } else if(strncmp(buffer,"socket://:",10) == 0) { /* Open the server socket */ *fd = prim_com_sun_midp_io_j2me_serversocket_Protocol_open0(*port, &exception);#ifndef BLOCKING_NET_IO /* For non-blocking systems only */ prim_com_sun_midp_io_j2me_socket_Protocol_setNonBlocking(*fd);#endif } return; } } return;}static int checkfilter(char *filter, char *ip) { char *p1 = NULL; char *p2 = NULL; if ((ip == NULL) || (filter == NULL)) return 0; /* Filter is exactly "*", then all IP numbers are allowed. */ if (strcmp(filter, "*") == 0) return 1; /* * Otherwise walk through the filter string looking for character * matches and wildcard matches for each period separated field. * The filter pointer is incremented in the main loop and the * IP address pointer is incremented as characters and wildcards * are matched. */ for (p1=filter, p2=ip; *p1; p1++) { if (*p1 == '*') { char nextp = *(p1+1); /* Match the rest of the IP field. */ while (*p2++) { /* * Break the wild card at the next field separator * or the next character that matches from the * pattern string. */ if(*p2 == '.' || *p2 == nextp) { break; } } } else if (*p1 == '?') { p2 ++; } else if (*p1 != *p2) { /* If characters do not match, filter failed. */ return 0; } else { p2 ++; } } return 1;}static jlong alarmcheck(jlong time) { AlarmEntry *alarmp; AlarmEntry *alarmnext; /* Find any alarm that has expired. */ for (alarmp = alarmlist; alarmp != NULL; alarmp = alarmnext) { alarmnext = alarmp->next; if ((time - alarmp->wakeup) > 0) { return alarmp->wakeup; } } return -1 ;}static void alarmListFree() { AlarmEntry *alarmp, *alarmtmp; /* clean up the list */ for (alarmp = alarmlist; alarmp != NULL; alarmp = alarmtmp) { alarmtmp = alarmp->next; midpFree(alarmp->midlet); midpFree(alarmp->storagename); midpFree(alarmp); }}static void pushListFree() { PushEntry *pushp, *pushtmp; /* clean up the list */ for (pushp = pushlist; pushp != NULL; pushp = pushtmp) { pushtmp = pushp->next; /* Close the socket if it's currently open.*/ if (pushp->fd != -1){ prim_com_sun_midp_io_j2me_socket_Protocol_close0(pushp->fd); } midpFree(pushp->value); midpFree(pushp->filter); midpFree(pushp->storagename); midpFree(pushp); }}static int pushpoll() { int i; int ret; int fd = -1; PushEntry * pe; if (pushlength > 0 ) {#ifdef UNIX struct pollfd *pfd = (struct pollfd*) midpMalloc(sizeof(struct pollfd) * pushlength); if (pfd != NULL) { for(i = 0, pe = pushlist; i < pushlength && pe != NULL; i++) { pfd[i].fd = pe->fd; pfd[i].events = POLLIN; pfd[i].revents = 0; pe = pe->next; } if ((ret = poll(pfd, pushlength, 0)) > 0 ){ for (i = 0 ; i < pushlength; i++) { if (pfd[i].revents != 0) { if ((pfd[i].revents & POLLERR) == POLLERR) { fprintf(stdout, "Error on fd=%d\n", pfd[i].fd); } else if ((pfd[i].revents & POLLHUP) == POLLHUP) { fprintf(stdout, "Hang up on fd=%d\n", pfd[i].fd); } else if ((pfd[i].revents & POLLNVAL) == POLLNVAL) { fprintf(stdout, "Invalid fd=%d\n", pfd[i].fd); } else { fd = pfd[i].fd; } } } } midpFree(pfd); pfd = NULL; }#else /* WINDOWS */ fd_set socks; int highsock=0; int count =0; struct timeval timeout; FD_ZERO(&socks); for (pe = pushlist; pe != NULL; pe = pe->next) { FD_SET(pe->fd, &socks); if (pe->fd > highsock) { highsock = pe->fd; } } timeout.tv_sec = 0; timeout.tv_usec=10; count = select(highsock+1, &socks,(fd_set *) NULL, (fd_set *) NULL, &timeout); if (count > 0 ){ for (pe = pushlist; pe != NULL; pe = pe->next) { if (FD_ISSET(pe->fd, &socks)) { fd = pe->fd; break; } } }#endif } return fd;}/* * Delete all of connections for a suite, using the storage name */static void pushdeletesuite(char *store) { PushEntry *pushp; PushEntry **pPrevNext = &pushlist; PushEntry *pushnext; AlarmEntry *alarmp; AlarmEntry **alarmpPrevNext = &alarmlist; AlarmEntry *alarmnext; /* Find all of the entries to remove */ for (pushp = pushlist; pushp != NULL; pushp = pushnext) { pushnext = pushp->next; if (strcmp(store, pushp->storagename) == 0) { *pPrevNext = pushp->next; pushlength-- ; /* Close the socket if it's currently open.*/ if (pushp->fd != -1){ prim_com_sun_midp_io_j2me_socket_Protocol_close0(pushp->fd); } midpFree(pushp->value); pushp->value = NULL; midpFree(pushp->filter); pushp->filter = NULL; midpFree(pushp->storagename); pushp->storagename = NULL; midpFree(pushp); continue; } pPrevNext = &pushp->next; } pushsave(); /* Find all of the alarm entries to remove */ for (alarmp = alarmlist; alarmp != NULL; alarmp = alarmnext) { alarmnext = alarmp->next; if (strcmp(store, alarmp->storagename) == 0) { *alarmpPrevNext = alarmp->next; midpFree(alarmp->midlet); alarmp->midlet = NULL; midpFree(alarmp->storagename); alarmp->storagename = NULL; midpFree(alarmp); continue; } alarmpPrevNext = &alarmp->next; } alarmsave();}/* * Parse the persistent alaram registry from disk into the * in memory cache representation. */static int parseAlarmList () { char buffer[MAX_LINE+1]; int i; int line = 0; int len; char *errStr = NULL ; jlong alarm = 0; AlarmEntry * pe; char *p; /* Read a line at a time */ for (i= 0 ; i < MAX_LINE ; i++) { len = storageRead(&errStr, pushfd, &buffer[i], 1); if (errStr != NULL) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -