1:/*--------------------------------------------------------------------------- 2: 3: FILENAME: 4: templateStates2.c 5: 6: PURPOSE: 7: Provide the 2nd template process state machine handlers. 8: 9: REVISION HISTORY: 10: Date Engineer Revision Remarks 11: 01/01/2004 Your Name 0 Original 12: 13: NOTES: 14: All references to "template" should be replaced by a meaningful 15: process name, including the file names and data structures. 16: 17:----------------------------------------------------------------------------*/ 18: 19:// System include files 20:#include <termios.h> 21: 22:// Library include files 23: 24:// Local include files 25:#include "template.h" 26: 27: 28:// global memory declarations 29: 30:// global memory referenced 31: 32:// static (local) memory declarations 33: 34:static int IsPrime (int value) 35:{ 36: int i; 37: 38: if (value <= 3) 39: { 40: return TRUE; 41: } 42: 43: // this isn't the best way to do this, but it works 44: for (i = 2; i < value/2; i ++) 45: { 46: if ((value % i) == 0) 47: { 48: return FALSE; 49: } 50: } 51: 52: return TRUE; 53:} 54: 55:static void processRequest (TEMPLATE_MSG_USER_REQUEST *request) 56:{ 57: TEMPLATE_MSG_USER_RESPONSE response; 58: int i, numPrimes = 0; 59: 60: for (i = 1; i < request->number; i ++) 61: { 62: if (IsPrime(i)) 63: { 64: numPrimes ++; 65: } 66: } 67: 68: // send the response 69: response.number = request->number; 70: response.numberOfPrimes = numPrimes; 71: radMsgRouterMessageSend (TEMPLATE_MSGID_USER_RESPONSE, &response, sizeof(response)); 72: 73: return; 74:} 75: 76: 77:// methods 78: 79:int template2IdleState (int state, void *stimulus, void *data) 80:{ 81: STIM *stim = (STIM *)stimulus; 82: TEMPLATE_WORK *work = (TEMPLATE_WORK *)data; 83: time_t ntime; 84: struct tm *locTime; 85: 86: switch (stim->type) 87: { 88: case STIM_DUMMY: 89: // this one starts this state machine; 90: // do any initialization, communication, etc. here 91: 92: 93: // return the new state for the machine 94: return TEMPLATE_STATE_RUN; 95: 96: case STIM_QMSG: 97: case STIM_EVENT: 98: case STIM_TIMER: 99: break; 100: } 101: 102: return state; 103:} 104: 105:int template2RunState (int state, void *stimulus, void *data) 106:{ 107: STIM *stim = (STIM *)stimulus; 108: TEMPLATE_WORK *work = (TEMPLATE_WORK *)data; 109: char temp[256]; 110: ssize_t retVal; 111: 112: switch (stim->type) 113: { 114: case STIM_QMSG: 115: if (stim->msgType == TEMPLATE_MSGID_USER_REQUEST) 116: { 117: // process the msg, compute the primes, send the response 118: processRequest ((TEMPLATE_MSG_USER_REQUEST *)stim->msg); 119: } 120: 121: break; 122: 123: case STIM_TIMER: 124: break; 125: 126: case STIM_IO: 127: break; 128: 129: case STIM_EVENT: 130: break; 131: } 132: 133: return state; 134:} 135: 136:int template2ErrorState (int state, void *stimulus, void *data) 137:{ 138: STIM *stim = (STIM *)stimulus; 139: TEMPLATE_WORK *work = (TEMPLATE_WORK *)data; 140: 141: 142: switch (stim->type) 143: { 144: case STIM_IO: 145: case STIM_QMSG: 146: case STIM_EVENT: 147: case STIM_TIMER: 148: radMsgLog (PRI_STATUS, 149: "%s:templateErrorState: received stimulus %d", 150: PROC_NAME_TEMPLATE, 151: stim->type); 152: break; 153: } 154: 155: return state; 156:}