LOCAL int CountType(LPLIST lpPktList, COMMAND_TYPE Type) /***********************************************************************/ { LPCMDPKT lpCmdPkt; int nType = 0; lpCmdPkt = (LPCMDPKT)ListGetHead(lpPktList); while (lpCmdPkt) { if (GetCommandType(lpCmdPkt->idCommand) == Type) ++nType; lpCmdPkt = (LPCMDPKT)ListGetNext(lpCmdPkt); } return(nType); }
LOCAL int CountType(LPTSTR lpFileName, COMMAND_TYPE Type) /***********************************************************************/ { LPVOID lpParms; LPTSTR lpCommand; int i; int nType = 0; MACRO_FILE_HANDLE fh; FNAME OEMName; if (!(lpCommand = (LPTSTR)Alloc(MAX_CMD_LEN))) { Message(IDS_EMEMALLOC); return(-1); } #ifdef BUFFERED_IO AnsiToOem(lpFileName, OEMName); fh = fopen(OEMName, _T("rb")); if (fh == NULL) #else fh = FileOpen(lpFileName, FO_READ); if (fh == MACRO_FILE_HANDLE_INVALID) #endif { FreeUp((LPTR)lpCommand); Message(IDS_EOPEN, lpFileName); return(-1); } while (ReadLine(fh, lpCommand, MAX_CMD_LEN)) { i = ReadParms(fh, lpCommand, &lpParms); if (i < 0) continue; if (GetCommandType(GetCommandId(i)) == Type) ++nType; if (lpParms) FreeUpParms(GetCommandId(i), lpParms); } #ifdef BUFFERED_IO fclose(fh); #else FileClose(fh); #endif FreeUp((LPTR)lpCommand); return(nType); }
/////////////////////////////////////////////////// //This function implements the state diagram for //user inputs int ProcessUserCommand(char* command, char* result) { if (!(command && result)) return -1; memset(result, 0, MAX_STR_LEN); int cmdType = 0; char msgExtra[MAX_STR_LEN]; cmdType = GetCommandType(command, msgExtra); if (cmdType < 0) { strcpy(result,"Please use one of the following commands:\n" " invite user@host : To initiate a session with host\n" " accept : To accept a request from remote host\n" " reject : To reject a request from remote host\n" " bye : To terminate an open session\n" " exit : To terminate the program\n"); return -1; } /* change the mode only if it is none; otherwise leave it as it is */ if (g_sip_mode==SIP_NONE && cmdType==CMD_INVITE) g_sip_mode =SIP_CLIENT; /* client */ if (g_sip_mode == SIP_CLIENT) { if (g_sip_state==SIP_IDLE) { if (cmdType==CMD_INVITE) { //Client at idle receives invite user@host: createSession;SetRemote sockaddr in oSocSIP, cook buffer and send. CreateSessionData(msgExtra); if (oSocSIP.SetRemoteInfo(g_SessionData.RemoteHost, SIP_PORT)<0) { strcpy(result, "Invalid host entered"); return -1; } sprintf((char*)g___Buffer, INVITE_MSG, g_SessionData.From, (DWORD)lrand48(), (DWORD)lrand48(), g_SessionData.MyHost, g_SessionData.MyHost, SIP_PORT); int c_len = strlen((char*)g___Buffer); sprintf((char*)g_txBuffer, INVITE_HDR, g_SessionData.ToAdrs, g_SessionData.MyHost, SIP_PORT, g_SessionData.From, g_SessionData.FromAdrs, g_SessionData.To, g_SessionData.ToAdrs, g_SessionData.CallID, ++g_sip_seq, c_len); strcat((char*)g_txBuffer, (char*)g___Buffer); c_len = strlen((char*)g_txBuffer); oSocSIP.SendTo(g_txBuffer, &c_len); g_sip_state = SIP_CONX_PENDING; } else if (cmdType==CMD_ACCEPT) { strcpy(result, "Nothing to accept"); return -1; } else if (cmdType==CMD_REJECT) { strcpy(result, "Nothing to reject"); return -1; } else if (cmdType==CMD_BYE) { strcpy(result, "No active session to close"); return -1; } else if (cmdType==CMD_EXIT) { g_sip_done = true; //g_run_rtp = false; StopAudioThread(); return 0; } } else if (g_sip_state==SIP_CONX_PENDING) { if (cmdType==CMD_INVITE) { strcpy(result, "Connection already in progress. Cannot invite now."); return -1; } else if (cmdType==CMD_ACCEPT) { strcpy(result, "Nothing to accept"); return -1; } else if (cmdType==CMD_REJECT) { strcpy(result, "Nothing to reject"); return -1; } else if (cmdType==CMD_BYE) { strcpy(result, "Connection in progress. Please try again"); return -1; } else if (cmdType==CMD_EXIT) { strcpy(result, "Connection in progress. Please try again"); return -1; } } else if (g_sip_state==SIP_CONNECTED) { if (cmdType==CMD_INVITE) { strcpy(result, "You are already connected. Please close this session (with bye) first before initiating a new session."); return -1; } else if (cmdType==CMD_ACCEPT) { strcpy(result, "You are already connected. Nothing to accept"); return -1; } else if (cmdType==CMD_REJECT) { strcpy(result, "You are already connected. Nothing to reject"); return -1; } else if (cmdType==CMD_BYE) { sprintf((char*)g_txBuffer, BYE_HDR, g_SessionData.ToAdrs, g_SessionData.MyHost, SIP_PORT, g_SessionData.From, g_SessionData.FromAdrs, g_SessionData.To, g_SessionData.ToAdrs, g_SessionData.CallID, ++g_sip_seq); int c_len = strlen((char*)g_txBuffer); oSocSIP.SendTo(g_txBuffer, &c_len); //g_run_rtp = false; StopAudioThread(); //g_au_started = true; g_sip_state = SIP_DISCONX_PENDING; } else if (cmdType==CMD_EXIT) { strcpy(result, "You are currently connected. Please close this session (with bye) first, and then exit."); return -1; } } else if (g_sip_state==SIP_DISCONX_PENDING) { if (cmdType==CMD_INVITE) { strcpy(result, "Disconnection in progress. Please try again."); return -1; } else if (cmdType==CMD_ACCEPT) { strcpy(result, "Disconnection in progress. Nothing to accept."); return -1; } else if (cmdType==CMD_REJECT) { strcpy(result, "Disconnection in progress. Nothing to reject."); return -1; } else if (cmdType==CMD_BYE) { strcpy(result, "Disconnection in progress. Please try again."); return -1; } else if (cmdType==CMD_EXIT) { strcpy(result, "Disconnection in progress. Please try again."); return -1; } } } else /* server mode */ if (g_sip_mode == SIP_SERVER) { if (g_sip_state==SIP_IDLE) { if (cmdType==CMD_INVITE) { printf("Invalid condition in the state machine!\n"); return -1; } else if (cmdType==CMD_ACCEPT) { strcpy(result, "Nothing to accept"); return -1; } else if (cmdType==CMD_REJECT) { strcpy(result, "Nothing to reject"); return -1; } else if (cmdType==CMD_BYE) { strcpy(result, "No active session to close"); return -1; } else if (cmdType==CMD_EXIT) { g_sip_done = true; //g_run_rtp = false; StopAudioThread(); return 0; } } else if (g_sip_state==SIP_CONX_PENDING) { if (cmdType==CMD_INVITE) { strcpy(result, "Connection already in progress. Cannot invite now."); return -1; } else if (cmdType==CMD_ACCEPT) { sprintf((char*)g_txBuffer, RESPONSE_MSG, 200, "OK", g_SessionData.remoteRestHdr, g_SessionData.remoteRestBdy); int c_len = strlen((char*)g_txBuffer); oSocSIP.SendTo(g_txBuffer, &c_len); } else if (cmdType==CMD_REJECT) { sprintf((char*)g_txBuffer, RESPONSE_MSG, 603, "Decline", g_SessionData.remoteRestHdr, "\0"); int c_len = strlen((char*)g_txBuffer); c_len--; oSocSIP.SendTo(g_txBuffer, &c_len); g_sip_state = SIP_IDLE; g_sip_mode = SIP_NONE; } else if (cmdType==CMD_BYE) { printf("Invalid condition in the state machine!\n"); return -1; } else if (cmdType==CMD_EXIT) { strcpy(result, "Connection in progress. Please try again"); return -1; } } else if (g_sip_state==SIP_CONNECTED) { if (cmdType==CMD_INVITE) { strcpy(result, "You are already connected. Please close this session (with bye) first before initiating a new session."); return -1; } else if (cmdType==CMD_ACCEPT) { strcpy(result, "You are already connected. Nothing to accept"); return -1; } else if (cmdType==CMD_REJECT) { strcpy(result, "You are already connected. Nothing to reject"); return -1; } else if (cmdType==CMD_BYE) { /* This is what i actually need to do. but resorting to short cut @@@@ sprintf(g_txBuffer, BYE_HDR, g_SessionData.ToAdrs, g_SessionData.MyHost, g_SessionData.From, g_SessionData.FromAdrs, g_SessionData.To, g_SessionData.ToAdrs, g_SessionData.CallID, ++g_sip_seq); */ sprintf((char*)g_txBuffer, "BYE %s SIP/2.0\n%s\n", g_SessionData.uri, g_SessionData.remoteRestHdr); int c_len = strlen((char*)g_txBuffer); oSocSIP.SendTo(g_txBuffer, &c_len); // g_run_rtp = false; StopAudioThread(); //g_au_started= true; g_sip_state = SIP_DISCONX_PENDING; } else if (cmdType==CMD_EXIT) { strcpy(result, "You are currently connected. Please close this session (with bye) first, and then exit."); return -1; } } else if (g_sip_state==SIP_DISCONX_PENDING) { if (cmdType==CMD_INVITE) { strcpy(result, "Disconnection in progress. Please try again."); return -1; } else if (cmdType==CMD_ACCEPT) { strcpy(result, "Disconnection in progress. Nothing to accept."); return -1; } else if (cmdType==CMD_REJECT) { strcpy(result, "Disconnection in progress. Nothing to reject."); return -1; } else if (cmdType==CMD_BYE) { strcpy(result, "Disconnection in progress. Please try again."); return -1; } else if (cmdType==CMD_EXIT) { strcpy(result, "Disconnection in progress. Please try again."); return -1; } } } else if (g_sip_mode == SIP_NONE) { if( g_sip_state == SIP_IDLE) { if (cmdType==CMD_ACCEPT) { strcpy(result, "Nothing to accept"); return 0; } else if (cmdType==CMD_REJECT) { strcpy(result, "Nothing to accept"); return 0; } else if (cmdType==CMD_EXIT) { g_sip_done = true; //g_run_rtp = false; StopAudioThread(); return 0; } } } return 0; }
BOOL PlayMacro(LPCMDLIST lpCmdList, LPTSTR lpFileName, int nRepeat, BOOL fSequenceAll, LPLIST lpMacroList, HWND hParent, int PhotoCDResOverride, LPTSTR lpMacroName) /***********************************************************************/ { LPIMAGE lpImage; LPCMDLIST lpNewList; int nActivates, iCount; ITEMID idCommand; STRING szString, szAppName; BOOL fError; BOOL fSequence, fCmdSequence, fCopyPackets; LIST MacroList; LPCMDPKT lpCmdPkt, lpNextPkt; MACROSETUP Setup; COMMAND_TYPE CommandType; HWND hDlg = NULL; lpAbortProc = NULL; if (!hParent) hParent = PictPubApp.Get_hWndAstral(); // see if we need to copy our packets fCopyPackets = nRepeat > 1; if (!lpMacroList) { // read in the entire macro file for faster processing if (!ReadMacro(lpFileName, &MacroList)) return(FALSE); lpMacroList = &MacroList; } // count the number of activates in the macro file // because it affects sequencing nActivates = CountType(lpMacroList, CT_ACTIVATE); // turn of macro play mode and tell the world MacroMode = MM_PLAY; if ( AstralStrEx( IDS_APPNAME, szAppName, sizeof(szAppName) ) ) { if ( AstralStrEx( IDS_MACROPLAY, szString, sizeof(szString) ) ) { lstrcat( szAppName, szString ); SetWindowText( PictPubApp.Get_hWndAstral(), szAppName ); } } // reset untitled number so that if a macro is played it // can deal with untitled images the same Control.UntitledNo = 0; // If no command list passed in to work on (Macro Batch Mode) // then get command list for active image if (!lpCmdList) { if (lpImage = GetActiveImage()) lpCmdList = lpImage->lpCmdList; else lpCmdList = NULL; } // See if the macro contains any low res loads and // if so ask to user if he'd like to convert them // to hi res loads and if so do the convert if (FindCommand(lpMacroList, IDS_CMD_LOWRESLOAD)) if (AstralAffirm(IDS_CONVERTLOWRES)) if (!ConvertLowResLoad(lpMacroList)) { DestroyPacketList(lpMacroList); return(FALSE); } // disable all mouse and keyboard input during macro play EnableWindow(hParent, FALSE); // if not in a threading environment and caller wants us // to display a progress dialog, then set it up and do it if (!Control.UseThreading) { iCount = ListGetCount(lpMacroList) - nActivates; iCount *= nRepeat; Setup.iTotal = iCount; Setup.idDialog = IDD_MACRO_STATUS; Setup.lpFileName = NULL; hDlg = AstralDlgParam( YES, PictPubApp.GetResourceHandle(), hParent, IDD_MACRO_STATUS, DlgMacroStatusProc, (LPARAM)(LPVOID)&Setup ); if (hDlg) { if (lpMacroName) { STRING szString; GetWindowText(hDlg, szString, sizeof(szString)); lstrcat(szString, _T(" - ")); lstrcat(szString, lpMacroName); SetWindowText(hDlg, szString); } UpdateWindow(hDlg); } } // Repeat macro nRepeat number of times fError = FALSE; while (--nRepeat >= 0 && !fError) { if (lpAbortProc && (*lpAbortProc)()) break; // back to beginning of macro file lpNextPkt = (LPCMDPKT)ListGetHead(lpMacroList); // initialize sequencing fSequence = fSequenceAll; while (!fError && lpNextPkt) { if (lpAbortProc && (*lpAbortProc)()) { fError = TRUE; break; } // get the packet to work on if (fCopyPackets) { lpCmdPkt = CopyPacket(lpNextPkt); if (!lpCmdPkt) { fError = TRUE; break; } } else { ListUnlink(lpMacroList, lpNextPkt); lpCmdPkt = lpNextPkt; } // get command id and parms for this command idCommand = lpCmdPkt->idCommand; // Find out whether this command requires sequencing // set it here, so command can change it if needed before // we actually set fSequence fCmdSequence = GetCommandSequence(idCommand); CommandType = GetCommandType(idCommand); // Handle the different types of commands switch (CommandType) { case CT_LOAD: // create new command list for load lpNewList = CreateCommandList(); if (lpNewList) { ListAddTail(&lpNewList->PacketList, lpCmdPkt); // if we already have a command list containing commands, // kick off the execution of those commands before we // switch to the new command list if (lpCmdList && !ListIsEmpty(&lpCmdList->PacketList)) { PlaybackCommands(lpCmdList); // if some command in the command list affects // sequencing force the whole command list to // be processed if (fSequence) { FlushCommands(lpCmdList); fSequence = fSequenceAll; } } // setup new command list for us to work with lpCmdList = lpNewList; lpCmdList->PhotoCDResOverride = PhotoCDResOverride; // If there are any activates in this macro make sure a // command that creates an image processes immediately if (nActivates) { PlaybackCommands(lpCmdList); FlushCommands(lpCmdList); fSequence = fSequenceAll; fCmdSequence = NO; // already sequenced } } break; case CT_COPY: // Make sure we have a command list to work with if (!lpCmdList) { Message(IDS_NOIMAGETOWORKON); fError = TRUE; break; } // Just add this command to the command list ListAddTail(&lpCmdList->PacketList, lpCmdPkt); // If there are any activates in this macro make sure a // command that creates an image processes immediately if (nActivates) { PlaybackCommands(lpCmdList); FlushCommands(lpCmdList); fSequence = fSequenceAll; fCmdSequence = NO; // already sequenced } break; case CT_SAVE: case CT_EDIT: case CT_MASK: case CT_MODE: case CT_EDITUNDO: case CT_MASKUNDO: case CT_EDITOBJ: case CT_CLOSE: // Make sure we have a command list to work with if (!lpCmdList) { Message(IDS_NOIMAGETOWORKON); fError = TRUE; break; } if (CommandType == CT_SAVE) { PlaybackCommands(lpCmdList); if (!lpCmdList->ThreadData.lpImage) { Message(IDS_NOIMAGETOWORKON); fError = TRUE; break; } } // Just add this command to the command list ListAddTail(&lpCmdList->PacketList, lpCmdPkt); if (CommandType != CT_CLOSE) break; case CT_ACTIVATE: // Program or commands that affect window activation // are processed here switch (idCommand) { case IDS_CMD_CLOSE: case IDS_CMD_ACTIVATEWINDOW: { // if we already have a command list containing commands, // kick of the execution of those commands before we // switch to the new command list if (lpCmdList && !ListIsEmpty(&lpCmdList->PacketList)) { PlaybackCommands(lpCmdList); // if some command in the command list affects // sequencing force the whole command list to // be processed if (fSequence) { FlushCommands(lpCmdList); fSequence = fSequenceAll; } } // if this was a close command, then wack the command list pointer if (idCommand == IDS_CMD_CLOSE) lpCmdList = NULL; else { // now process the activate and get a new command list lpNewList = ProgActivateWindow( (LPACTIVATEWINDOW_PARMS)lpCmdPkt->lpParms); // setup the new command list if we got one if (lpNewList) lpCmdList = lpNewList; else { CommandError(idCommand); fError = TRUE; } // activate don't go through command processing // so we have to free it up here FreeUpPacket(lpCmdPkt); } } break; default: break; } break; default: break; } // if command just handled requires sequencing // set sequencing flag if (fCmdSequence) fSequence = YES; if (fCopyPackets) // get next command packet in macro list lpNextPkt = (LPCMDPKT)ListGetNext(lpNextPkt); else // head of list will be next one if we're not copying lpNextPkt = (LPCMDPKT)ListGetHead(lpMacroList); } } // get rid of macro list DestroyPacketList(lpMacroList); // if we already have a command list containing commands, // kick off the execution of those commands if (lpCmdList && !ListIsEmpty(&lpCmdList->PacketList)) { PlaybackCommands(lpCmdList); if (fSequenceAll) FlushCommands(lpCmdList); } // turn off the macro mode EnableWindow(hParent, TRUE); if ( AstralStrEx( IDS_APPNAME, szAppName, sizeof(szAppName) ) ) SetWindowText( PictPubApp.Get_hWndAstral(), szAppName ); // if we have a progress dialog, nuke it if (hDlg) AstralDlgEnd(hDlg, TRUE); // if we are display the macro status for another modal dialog // make sure the main app stuff is still disabled if (hParent != PictPubApp.Get_hWndAstral()) { EnableOverlappedWindow( PictPubApp.Get_hWndAstral(), FALSE ); EnableWindow(PictPubApp.Get_hWndAstral(), FALSE); } MacroMode = MM_NONE; return(TRUE); }