test3_main.c

00001 /*   
00002  *   (c) Copyright 2008 Philipp Skadorov (philipp_s@users.sourceforge.net)
00003  *
00004  *   This file is part of FREESECS.
00005  *
00006  *   FREESECS is free software: you can redistribute it and/or modify
00007  *   it under the terms of the GNU General Public License as published by
00008  *   the Free Software Foundation, either version 3 of the License, or
00009  *   (at your option) any later version.
00010  *
00011  *   FREESECS is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *   GNU General Public License for more details.
00015  *
00016  *   You should have received a copy of the GNU General Public License
00017  *   along with FREESECS, see COPYING.
00018  *   If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <semaphore.h>
00023 #include "hsms_msgs.hpp"
00024 #include "hsmsd_cli.h"
00025 
00026 static void active_hsms_msg_handler(hsmsd_msg_t* msg);
00027 static void passive_hsms_msg_handler(hsmsd_msg_t* msg);
00028 static void active_cnx_state_handler(hsmsd_cnx_state_t state);
00029 static void passive_cnx_state_handler(hsmsd_cnx_state_t state);
00030 static void prepare_dict(void);
00031 static void start_threads(void*);
00032 static void* thread_func0(void*);
00033 static void* thread_func1(void*);
00034 static void* thread_func2(void*);
00035 static void* thread_func3(void*);
00036 static void* thread_funcx(int num, void* data);
00037 
00038 static sem_t cnx_ready_sem;
00039 
00040 
00041 static struct
00042 {
00043     sem_t sent_sem;
00044     sem_t recv_sem;
00045     sem_t err_sem;
00046     unsigned long us_timeout;
00047     unsigned int transaction_id;
00048     hsmsd_msg_t* primary;
00049     hsmsd_msg_t* secondary;
00050 }msg_dict[4] = 
00051 {
00052     {.us_timeout = 1, .transaction_id = 0 << 24, 
00053      .primary = (hsmsd_msg_t*)&s1f13_msg,   .secondary = (hsmsd_msg_t*)&s1f14_msg},
00054     {.us_timeout = 1, .transaction_id = 1 << 24, 
00055      .primary = (hsmsd_msg_t*)&s3f17_msg,   .secondary = (hsmsd_msg_t*)&s3f18_msg},
00056     {.us_timeout = 1, .transaction_id = 2 << 24, 
00057      .primary = (hsmsd_msg_t*)&s16f15_msg,  .secondary = (hsmsd_msg_t*)&s16f16_msg},
00058     {.us_timeout = 1, .transaction_id = 3 << 24, 
00059      .primary = (hsmsd_msg_t*)&s14f9_msg,   .secondary = (hsmsd_msg_t*)&s14f10_msg}
00060 };
00061 
00062 
00063 static hsmsd_handle_t active_cnx, passive_cnx;
00064 
00065 int main(int argc, char **argv)
00066 {
00067     int r, sent[4], recv[4], err[4];
00068 
00069     sem_init(&cnx_ready_sem, 0, 0);
00070 
00071     r = hsmsd_alloc_handle(&active_cnx, "hsms_local_active");
00072     printf("alloc hsms_local_active returned %d\n", r);
00073     if(r) exit(-1);
00074 
00075     r = hsmsd_alloc_handle(&passive_cnx, "hsms_local_passive");
00076     printf("alloc hsms_local_passive returned %d\n", r);
00077     if(r) exit(-2);
00078 
00079     r = hsmsd_subscribe_for_msgs(active_cnx,    (hsmsd_msg_handler_t)active_hsms_msg_handler);
00080     fprintf(stdout, "hsmsd_subscribe_for_msgs(active) returned %d\n", r);
00081     if(r) exit(-3);
00082 
00083     r = hsmsd_subscribe_for_msgs(passive_cnx,   (hsmsd_msg_handler_t)passive_hsms_msg_handler);
00084     fprintf(stdout, "hsmsd_subscribe_for_msgs(passive) returned %d\n", r);
00085     if(r) exit(-4);
00086 
00087     r = hsmsd_subscribe_for_cnx_state(active_cnx, (hsmsd_cnx_state_handler_t)active_cnx_state_handler);
00088     fprintf(stdout, "hsmsd_subscribe_for_cnx_state(active) returned %d\n", r);
00089     if(r) exit(-5);
00090 
00091     r = hsmsd_subscribe_for_cnx_state(passive_cnx, (hsmsd_cnx_state_handler_t)passive_cnx_state_handler);
00092     fprintf(stdout, "hsmsd_subscribe_for_cnx_state(passive) returned %d\n", r);
00093     if(r) exit(-6);
00094 
00095     r = hsmsd_cnx_start(passive_cnx);
00096     fprintf(stdout, "hsmsd_cnx_start(passive) returned %d\n", r);
00097     if(r) exit(-8);
00098     
00099     //sleep(1);
00100 
00101     r = hsmsd_cnx_start(active_cnx);
00102     fprintf(stdout, "hsmsd_cnx_start(active) returned %d\n", r);
00103     if(r) exit(-7);
00104 
00105     sem_wait(&cnx_ready_sem);
00106 
00107     prepare_dict();
00108     fprintf(stdout, "dict prepared\n");
00109 
00110     start_threads((void*)&active_cnx);
00111     fprintf(stdout, "threads started\n");
00112     
00113     while(1)
00114     {
00115         sleep(2);
00116 
00117         sem_getvalue(&msg_dict[0].sent_sem, &sent[0]);
00118         sem_getvalue(&msg_dict[1].sent_sem, &sent[1]);
00119         sem_getvalue(&msg_dict[2].sent_sem, &sent[2]);
00120         sem_getvalue(&msg_dict[3].sent_sem, &sent[3]);
00121 
00122         sem_getvalue(&msg_dict[0].recv_sem, &recv[0]);
00123         sem_getvalue(&msg_dict[1].recv_sem, &recv[1]);
00124         sem_getvalue(&msg_dict[2].recv_sem, &recv[2]);
00125         sem_getvalue(&msg_dict[3].recv_sem, &recv[3]);
00126 
00127         sem_getvalue(&msg_dict[0].err_sem, &err[0]);
00128         sem_getvalue(&msg_dict[1].err_sem, &err[1]);
00129         sem_getvalue(&msg_dict[2].err_sem, &err[2]);
00130         sem_getvalue(&msg_dict[3].err_sem, &err[3]);
00131         
00132         fprintf(stdout, "sent:\t%d\t|\t%d\t|\t%d\t|\t%d\n", 
00133                                  sent[0], sent[1], sent[2], sent[3]);
00134         fprintf(stdout, "recv:\t%d\t|\t%d\t|\t%d\t|\t%d\n", 
00135                                  recv[0], recv[1], recv[2], recv[3]);
00136         fprintf(stdout, "err:\t%d\t|\t%d\t|\t%d\t|\t%d\n", 
00137                                  err[0], err[1], err[2], err[3]);
00138         fflush(stdout);
00139     }
00140 
00141     return 0;
00142 }
00143 
00144 void active_hsms_msg_handler(hsmsd_msg_t* msg)
00145 {
00146     int pool_num = msg->sysbytes >> 24;
00147     if(4 > pool_num)
00148     {
00149         sem_post(&msg_dict[pool_num].recv_sem);
00150     }
00151     else
00152     {
00153         fprintf(stdout, ">>>!!!!!unknown id: %d!!!!!<<<\n",pool_num);
00154     }
00155     free(msg);
00156 
00157 }
00158 
00159 void passive_hsms_msg_handler(hsmsd_msg_t* msg)
00160 {
00161     int r;
00162     switch(msg->function)
00163     {
00164         case 9:
00165             r = hsmsd_cnx_send_msg(passive_cnx, (hsmsd_msg_t*)&s14f10_msg);
00166         break;
00167         case 13:
00168             r = hsmsd_cnx_send_msg(passive_cnx, (hsmsd_msg_t*)&s1f14_msg);
00169         break;
00170         case 15:
00171             r = hsmsd_cnx_send_msg(passive_cnx, (hsmsd_msg_t*)&s16f16_msg);
00172         break;
00173         case 17:
00174             r = hsmsd_cnx_send_msg(passive_cnx, (hsmsd_msg_t*)&s3f18_msg);
00175         break;
00176         default:
00177         break;
00178     }
00179     free(msg);
00180 }
00181 
00182 static char str_state[][32] = {"NOT CONNECTED", "NOT SELECTED", "SELECTED"};
00183 
00184 void active_cnx_state_handler(hsmsd_cnx_state_t state)
00185 {
00186     if(CNX_SELECTED == state)
00187     {
00188         sem_post(&cnx_ready_sem);
00189     }
00190     fprintf(stdout, "active_cnx_state_handler: %s\n", str_state[state]);
00191 
00192 }
00193 
00194 void passive_cnx_state_handler(hsmsd_cnx_state_t state)
00195 {
00196     fprintf(stdout, "passive_cnx_state_handler: %s\n", str_state[state]);
00197 }
00198 
00199 void prepare_dict(void)
00200 {
00201     sem_init(&msg_dict[0].sent_sem, 0, 0);
00202     sem_init(&msg_dict[1].sent_sem, 0, 0);
00203     sem_init(&msg_dict[2].sent_sem, 0, 0);
00204     sem_init(&msg_dict[3].sent_sem, 0, 0);
00205 
00206     sem_init(&msg_dict[0].recv_sem, 0, 0);
00207     sem_init(&msg_dict[1].recv_sem, 0, 0);
00208     sem_init(&msg_dict[2].recv_sem, 0, 0);
00209     sem_init(&msg_dict[3].recv_sem, 0, 0);
00210 
00211     sem_init(&(msg_dict[0].err_sem), 0, 0);
00212     sem_init(&(msg_dict[1].err_sem), 0, 0);
00213     sem_init(&(msg_dict[2].err_sem), 0, 0);
00214     sem_init(&(msg_dict[3].err_sem), 0, 0);
00215 }
00216 
00217 void start_threads(void* pdata)
00218 {
00219     pthread_t id0, id1, id2, id3;
00220     pthread_create(&id0, NULL, thread_func0, pdata);
00221     pthread_create(&id1, NULL, thread_func1, pdata);
00222     pthread_create(&id2, NULL, thread_func2, pdata);
00223     pthread_create(&id3, NULL, thread_func3, pdata);
00224 }
00225 
00226 void* thread_funcx(int num, void* data)
00227 {
00228     fprintf(stdout, "thread function %d\n", num);
00229     hsmsd_handle_t h = *((hsmsd_handle_t*)data);
00230     while(1)
00231     {
00232         usleep(msg_dict[num].us_timeout);
00233         msg_dict[num].primary->sysbytes += 1;
00234         msg_dict[num].secondary->sysbytes += 1;
00235         if(0 == hsmsd_cnx_send_msg(h, msg_dict[num].primary))
00236         {
00237             sem_post(&msg_dict[num].sent_sem);
00238         }
00239         else
00240         {
00241             fprintf(stdout, "thread function %d: exit...\n", num);
00242             sem_post(&msg_dict[num].err_sem);
00243             pthread_exit(NULL);
00244         }
00245     }
00246     fprintf(stdout, "thread function %d: exit...\n", num);
00247     return NULL;
00248 }
00249 
00250 void* thread_func0(void* data)
00251 {
00252     return thread_funcx(0, data);
00253 }
00254 
00255 void* thread_func1(void* data)
00256 {
00257     return thread_funcx(1, data);
00258 }
00259 
00260 void* thread_func2(void* data)
00261 {
00262     return thread_funcx(2, data);
00263 }
00264 
00265 void* thread_func3(void* data)
00266 {
00267     return thread_funcx(3, data);
00268 }

Generated on Fri Oct 3 15:30:01 2008 for FREESECS hsms by  doxygen 1.5.1