Google
Web alhem.net
Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

EventFactory.cpp

Go to the documentation of this file.
00001 00006 /* 00007 Copyright (C) 2004 Anders Hedstrom 00008 00009 This program is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU General Public License 00011 as published by the Free Software Foundation; either version 2 00012 of the License, or (at your option) any later version. 00013 00014 This program is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 GNU General Public License for more details. 00018 00019 You should have received a copy of the GNU General Public License 00020 along with this program; if not, write to the Free Software 00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00022 */ 00023 #include <stdincl.h> 00024 00025 #include "client.h" 00026 #include "InitSDL.h" 00027 #include "WinSockInit.h" 00028 #include "Main.h" 00029 #include "commands/ClassFactory.h" 00030 #include "data/ClassFactory.h" 00031 #include "events/ClassFactory.h" 00032 #include "forms/ClassFactory.h" 00033 #include "EventFactory.h" 00034 00035 00036 #undef DEB 00037 #ifdef _DEBUG 00038 #define DEB(x) x 00039 #else 00040 #define DEB(x) 00041 #endif 00042 00043 00044 EventFactory::EventFactory() 00045 { 00046 } 00047 00048 00049 EventFactory::~EventFactory() 00050 { 00051 } 00052 00053 00054 void EventFactory::StateMachine(IOThread *pclIOT) 00055 { 00056 struct timeval tv; 00057 char c; 00058 00059 for(;;) 00060 { 00061 switch (m_state) 00062 { 00063 case 0: 00064 //DEB(printf("EventFactory state 0\n");) 00065 if (!pclIOT -> get_iq()) 00066 return; 00067 pclIOT -> read_input( &c, 1 ); 00068 if (c == 0x02) // stx 00069 m_state = 1; 00070 break; 00071 case 1: 00072 DEB(printf("EventFactory state 1\n");) 00073 if (pclIOT -> get_iq() < m_l0) 00074 return; 00075 pclIOT -> read_input( (char *)&m_id, sizeof(IO_IDTYPE) ); 00076 pclIOT -> read_input( (char *)&m_length, sizeof(IO_LENGTHTYPE) ); 00077 m_state = 2; 00078 break; 00079 case 2: 00080 DEB(printf("EventFactory state 2\n");) 00081 if (pclIOT -> get_iq() < m_length - m_l0) 00082 return; 00083 m_state = 0; 00084 if ( (m_pclMessage = GetClass(m_id)) == NULL) 00085 { 00086 fprintf(stderr,"EventFactory, GetClass() failed, id = %d\n",m_id); 00087 return; 00088 } 00089 #ifdef TRY_VAR 00090 m_pclMessage -> ReadVars(pclIOT); 00091 #else 00092 m_pclMessage -> read_message(pclIOT); 00093 #endif 00094 00095 if (m_pclMessage -> GetIOFamily() == IOF_FORM) 00096 { 00097 DEB(printf("Form received - add to input thread form list\n");) 00098 // it -> AddForm(m_pclMessage); 00099 SDL_Event e; 00100 00101 m_pclMessage -> FormInit(pclIOT); 00102 e.user.type = SDL_USEREVENT; 00103 e.user.code = SDL_USEREVENT_ADDFORM; 00104 e.user.data1 = m_pclMessage; 00105 e.user.data2 = NULL; 00106 SDL_PushEvent(&e); 00107 } 00108 else 00109 { 00110 // wait for execute time 00111 if (m_pclMessage -> GetID() != EVENT_SYNC) 00112 { 00113 tv.tv_usec = (m_pclMessage -> GetTicks() - ticks.get_sync()) * 1000; 00114 long l = tv.tv_usec / 1000; 00115 tv.tv_sec = tv.tv_usec / 1000000; 00116 tv.tv_usec %= 1000000; 00117 if (tv.tv_sec > 0 || tv.tv_usec > 0) 00118 { 00119 DEB(printf("timing event (%ld,%ld) %ld ms\n", tv.tv_sec,tv.tv_usec,l);) 00120 SDL_Delay(l); 00121 } 00122 } 00123 m_pclMessage -> Execute(pclIOT); 00124 } 00125 if (m_pclMessage -> DeleteOnExecute()) 00126 { 00127 delete m_pclMessage; 00128 } 00129 break; 00130 } 00131 } 00132 } 00133 00134 00135 IOMessage *EventFactory::GetClass(IO_IDTYPE id) 00136 { 00137 { 00138 Data::ClassFactory fac; 00139 IOMessage *pclTemp = fac.GetClass(id); 00140 if (pclTemp) 00141 { 00142 return pclTemp; 00143 } 00144 } 00145 { 00146 Event::ClassFactory fac; 00147 IOMessage *pclTemp = fac.GetClass(id); 00148 if (pclTemp) 00149 { 00150 return pclTemp; 00151 } 00152 } 00153 { 00154 Form::ClassFactory fac; 00155 IOMessage *pclTemp = fac.GetClass(id); 00156 if (pclTemp) 00157 { 00158 return pclTemp; 00159 } 00160 } 00161 fprintf(stderr,"EventFactory: Unknown event id: %d\n",id); 00162 exit(-1); 00163 }

Generated for SDL C++ GUI by doxygen 1.3.6