00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <j2k/Fred/Basic.hpp>
00011 #include <j2k/Fred/System.hpp>
00012 #include <j2k/Fred/Boolean.hpp>
00013 #include <j2k/Fred/StdTypes.hpp>
00014
00015 #define DEBUG_ON
00016
00017
00018
00019 #define MT_TIMER 1 // Message from timer
00020 #define MT_WAIT_DATA 2 // Message from client
00021 #define MT_SEND_DATA 3 // Message from client
00022
00023
00024 #define MT_OK 0 // Message to client
00025 #define MT_TIME_OUT 1 // Message to client
00026
00027
00028 typedef struct {
00029 int messageType;
00030 int messageData;
00031 } MessageT;
00032
00033
00034 #define MAX_CLIENT 16 // Maximum number of simultaneous client
00035
00036 struct {
00037 int in_use;
00038 int pid;
00039 int timeout;
00040 } clients [MAX_CLIENT];
00041
00042 char* progname = "time1.c";
00043
00044
00045
00046 int main() {
00047 int senderPID;
00048 int proxyPID;
00049 MessageT msg;
00050
00051
00052 proxyPID = setupProxyAndTimer();
00053
00054
00055 for(;;) {
00056 senderPID = Receive( 0, &msg, sizeof( msg ) );
00057
00058
00059 if ( senderPID == proxyPID ) {
00060 fromProxy();
00061 } else {
00062 fromMessage( senderPID, &msg );
00063 }
00064 }
00065
00066
00067 return ( EXIT_SUCCESS );
00068 }
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 int setupProxyAndTimer() {
00079 pid_t pid;
00080 timer_t tid;
00081 struct sigevent event;
00082 struct itimerspec timer;
00083 MessageT proxy_message;
00084
00085
00086 proxy_message.messageType = MT_TIMER
00087 proxy_message.messageData = 0;
00088
00089 pid = qnx_proxy_attach( 0, &proxy_message, sizeof( proxy_message), 10 );
00090 if (pid == -1) {
00091 fprintf( stderr,
00092 "%s: couldn't attach a proxy, errno %d \n",
00093 progname, errno );
00094
00095 perror( NULL );
00096 exit( EXIT_FAILURE );
00097 }
00098
00099
00100 event.sigev_signo = -pid;
00101
00102
00103 tid = timer_create( CLOCK_REALTIME, &event );
00104 if (tid == -1) {
00105 fprintf( stderr,
00106 "%s: couldn't create a timer, errno %d \n",
00107 progname, errno );
00108
00109 perror( NULL );
00110 exit( EXIT_FAILURE );
00111 }
00112
00113
00114 timer.it_value.tv_sec = 1;
00115 timer.it_value.tv_nsec = 0;
00116
00117 timer.it_interval.tv_sec = 1;
00118 timer.it_interval.tv_nsec = 0;
00119
00120 timer_settime( tid, 0, &timer, NULL );
00121
00122
00123 return (pid);
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 void fromProxy() {
00137 MessageT msg;
00138 int i;
00139
00140 #ifdef DEBUG_ON
00141 time_t now;
00142 time ( &now );
00143 printf( "Got a Proxy at %s", ctime( &now ) );
00144 #endif
00145
00146
00147 msg.messageType = MT_TIME_OUT
00148
00149
00150 for( i = 0; i < MAX_CLIENT; i++ ) {
00151
00152
00153 if ( clients[i].in_use ) {
00154
00155
00156 if (--client[i].timeout == 0) {
00157
00158 Reply( clients[i].pid, &msg, sizeof( msg ) );
00159
00160
00161 clients[i].in_use = 0;
00162 }
00163 }
00164 }
00165 }
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180 void fromMessage( int pid, MessageT* msg ) {
00181 int i;
00182
00183
00184 switch( msg->messageType ) {
00185
00186
00187 case MT_WAIT_DATA:
00188
00189
00190 for( i = 0; i < MAX_CLIENT; i++ ) {
00191 if ( !clients[i].in_use ) {
00192
00193
00194 clients[i].in_use = 1;
00195 clients[i].pid = pid;
00196 clients[i].timeout = 5;
00197 return;
00198 }
00199 }
00200
00201 fprintf( stderr,
00202 "%s: Table full, message from PID %d ignored, client blocked\n",
00203 pid );
00204
00205 break;
00206
00207 case MT_SEND_DATA:
00208
00209
00210
00211 for( i = 0; i < MAX_CLIENT; i++ ) {
00212 if ( clients[i].in_use ) {
00213
00214 msg->message_type = MT_OK;
00215
00216
00217 Reply( clients[i].pid, msg, sizeof( *msg ) );
00218 Reply( pid, msg, sizeof( *msg ) );
00219
00220 clients[i].in_use = 0;
00221 return;
00222 }
00223 }
00224
00225 fprintf( stderr,
00226 "%s: Table empty, message from PID %d ignored, client blocked\n",
00227 pid );
00228
00229 break;
00230 }
00231 }
00232