Fixed newline characters throughout the code
[com/gs-lite.git] / src / lib / gscphost / lappregistries.c
1 /* ------------------------------------------------
2  Copyright 2014 AT&T Intellectual Property
3  Licensed under the Apache License, Version 2.0 (the "License");
4  you may not use this file except in compliance with the License.
5  You may obtain a copy of the License at
6  
7  http://www.apache.org/licenses/LICENSE-2.0
8  
9  Unless required by applicable law or agreed to in writing, software
10  distributed under the License is distributed on an "AS IS" BASIS,
11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  See the License for the specific language governing permissions and
13  limitations under the License.
14  ------------------------------------------- */
15
16 #include <lappregistries.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19
20 /* datastructure to keep track of streamids used by the lapp */
21 struct streamregistry {
22     gs_int32_t used;
23     FTAID ftaid;
24     struct ringbuf * r;
25 };
26
27 struct streamregistry * sreg =0;
28 gs_int32_t lsreg=0;
29
30 struct ringbuf ** rbr=0;
31 gs_int32_t lrbr=0;
32
33 FTAID ** rms=0;
34 gs_int32_t lrms=0;
35
36 /* adds the remote streamid with its associated msgid and ringbuf
37  */
38 gs_retval_t streamregistry_add(FTAID ftaid,struct ringbuf * r)
39 {
40     gs_int32_t x;
41     if (lsreg == 0) {
42         if ((sreg = malloc(sizeof(struct streamregistry)*STARTSZ))==0) {
43             gslog(LOG_EMERG,"ERROR:Out of memory streanregistry\n");
44             return -1;
45         }
46         memset(sreg,0,sizeof(struct streamregistry)*STARTSZ);
47         lsreg = STARTSZ;
48     }
49     for(x=0;(x<lsreg)&&(sreg[x].used!=0);x++);
50     if (x == lsreg) {
51         gs_int32_t y;
52         lsreg = 2*lsreg;
53         if ((sreg = realloc(sreg,lsreg*sizeof(struct streamregistry)))==0) {
54             gslog(LOG_EMERG,"ERROR:Out of memory streamregistry\n");
55             return -1;
56         }
57         for (y=x;y<lsreg;y++)
58             sreg[y].used=0;
59     }
60     sreg[x].ftaid=ftaid;
61     sreg[x].r=r;
62     sreg[x].used=1;
63     return 0;
64 }
65
66 /* removes streamid from registry for specific msgid and ringbuf */
67 void streamregistry_remove(FTAID ftaid)
68 {
69     gs_int32_t x;
70     for(x=0;x<lsreg;x++) {
71         if ((sreg[x].ftaid.ip==ftaid.ip)
72             && (sreg[x].ftaid.port==ftaid.port)
73             && (sreg[x].ftaid.index==ftaid.index)
74             && (sreg[x].ftaid.streamid==ftaid.streamid)) {
75             sreg[x].used=0;
76         }
77     }
78     return;
79 }
80
81
82
83 /* the following two functions are used to cycle
84  through all ringbuffers
85  */
86 gs_retval_t streamregistry_getactiveringbuf_reset()
87 {
88     gs_int32_t x,y;
89     /* XXXOS this is not the most effective way of doing
90      this needs improvment. */
91     /* Build a list of all ringbufs make sure they
92      are unique since multiple entrys could share
93      a ringbuf
94      */
95     if (rbr!=0) (free(rbr));
96     if ((rbr=malloc(sizeof(struct ringbuf *)*lsreg))==0) {
97         gslog(LOG_EMERG,"Can't allocate memory in ftaregistry\n");
98         return -1;
99     }
100     memset(rbr,0,sizeof(struct ringbuf *)*lsreg);
101     lrbr=0;
102     for(x=0;x<lsreg;x++) {
103         if (sreg[x].used) {
104             for(y=0;(y<lrbr)&&(rbr[y]!=sreg[x].r);y++);
105             if (y>=lrbr) {
106                 rbr[y]=sreg[x].r;
107                 lrbr++;
108             }
109         }
110     }
111     return 0;
112 }
113
114 struct ringbuf * streamregistry_getactiveringbuf()
115 {
116     if (lrbr>0) {
117         lrbr --;
118         return rbr[lrbr];
119     }
120     return 0;
121 }
122
123
124 gs_retval_t streamregistry_getactiveftaid_reset()
125 {
126     gs_int32_t x,y;
127     /* XXXOS this is not the most effective way of doing
128      this needs improvment. */
129     /* Build a list of at least one ftaid per process
130      */
131     if (rms!=0) (free(rms));
132     if ((rms=malloc(sizeof(FTAID *)*lsreg))==0) {
133         gslog(LOG_EMERG,"Can't allocate memory in ftaregistry\n");
134         return -1;
135     }
136     memset(rms,0,sizeof(gs_int32_t *)*lsreg);
137     lrms=0;
138     for(x=0;x<lsreg;x++) {
139         if (sreg[x].used) {
140             for(y=0;(y<lrms)
141                 &&(rms[y]->ip!=sreg[x].ftaid.ip)
142                 &&(rms[y]->port!=sreg[x].ftaid.port);y++);
143             if (y>=lrms) {
144                 rms[y]=&(sreg[x].ftaid);
145                 lrms++;
146             }
147         }
148     }
149     return 0;
150 }
151
152 FTAID * streamregistry_getactiveftaid()
153 {
154     if (lrms>0) {
155         lrms --;
156         return rms[lrms];
157     }
158     return 0;    
159 }