00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifdef HAVE_CONFIG_H
00034 #include "config.h"
00035 #endif
00036
00037 #include <string.h>
00038
00039 #include "rdd.h"
00040 #include "rdd_internals.h"
00041 #include "msgprinter.h"
00042
00043 typedef struct _RDD_BCASTPRINTER_STATE {
00044 unsigned nprinter;
00045 RDD_MSGPRINTER **printers;
00046 } RDD_BCASTPRINTER_STATE;
00047
00048 static void bcastprinter_print(RDD_MSGPRINTER *self, rdd_message_t mesg_type, int errcode, const char *mesg);
00049
00050 static int bcastprinter_close(RDD_MSGPRINTER *self, unsigned flags);
00051
00052 static RDD_MSGPRINTER_OPS bcast_ops = {
00053 bcastprinter_print,
00054 bcastprinter_close
00055 };
00056
00057 int rdd_mp_open_bcastprinter(RDD_MSGPRINTER **self, unsigned nprinter, RDD_MSGPRINTER **printers)
00058 {
00059 RDD_MSGPRINTER *p = 0;
00060 RDD_BCASTPRINTER_STATE *state = 0;
00061 RDD_MSGPRINTER **printertab = 0;
00062 int rc;
00063 unsigned i;
00064
00065 *self = 0;
00066
00067 if(printers == NULL){
00068 return RDD_BADARG;
00069 }
00070
00071 printertab = malloc(nprinter * sizeof(RDD_MSGPRINTER *));
00072 if (printertab == 0) {
00073 return RDD_NOMEM;
00074 }
00075 for (i = 0; i < nprinter; i++) {
00076 printertab[i] = printers[i];
00077 }
00078
00079 rc = rdd_mp_open_printer(&p, &bcast_ops, sizeof(RDD_BCASTPRINTER_STATE));
00080 if (rc != RDD_OK) {
00081 free(printertab);
00082 return rc;
00083 }
00084
00085 state = (RDD_BCASTPRINTER_STATE *) p->state;
00086 state->nprinter = nprinter;
00087 state->printers = printertab;
00088
00089 *self = p;
00090 return RDD_OK;
00091 }
00092
00093 static void bcastprinter_print(RDD_MSGPRINTER *self, rdd_message_t mesg_type, int errcode, const char *mesg)
00094 {
00095 RDD_BCASTPRINTER_STATE *bcast = (RDD_BCASTPRINTER_STATE *) self->state;
00096 unsigned i;
00097
00098 for (i = 0; i < bcast->nprinter; i++) {
00099 rdd_mp_message(bcast->printers[i], mesg_type, "%s", mesg);
00100 }
00101 }
00102
00103 static int bcastprinter_close(RDD_MSGPRINTER *self, unsigned flags) {
00104 RDD_BCASTPRINTER_STATE *state = (RDD_BCASTPRINTER_STATE *) self->state;
00105 unsigned i;
00106 int rc;
00107
00108 if ((flags & RDD_MP_RECURSE) != 0) {
00109 for (i = 0; i < state->nprinter; i++) {
00110 rc = rdd_mp_close(state->printers[i], flags);
00111 if (rc != RDD_OK) {
00112 return rc;
00113 }
00114 }
00115 }
00116
00117 free(state->printers);
00118 memset(state, 0, sizeof *state);
00119 return RDD_OK;
00120 }