1c696fb7c05d72d57a1e5bcee0ae7ff801136960
[debian/gnuradio] / mblock / src / lib / mb_protocol_class.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006,2008,2009 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <mblock/protocol_class.h>
27 #include <iostream>
28
29 using namespace pmt;
30
31 static pmt_t s_ALL_PROTOCOL_CLASSES = PMT_NIL;
32
33 pmt_t 
34 mb_make_protocol_class(pmt_t name, pmt_t incoming, pmt_t outgoing)
35 {
36   // (protocol-class <name> <incoming> <outgoing>)
37
38   if (!pmt_is_symbol(name))
39     throw pmt_wrong_type("mb_make_protocol_class: NAME must be symbol", name);
40   if (!(pmt_is_pair(incoming) || pmt_is_null(incoming)))
41     throw pmt_wrong_type("mb_make_protocol_class: INCOMING must be a list", name);
42   if (!(pmt_is_pair(outgoing) || pmt_is_null(outgoing)))
43     throw pmt_wrong_type("mb_make_protocol_class: OUTGOING must be a list", name);
44
45   pmt_t t = pmt_cons(pmt_intern("protocol-class"),
46                      pmt_cons(name,
47                               pmt_cons(incoming,
48                                        pmt_cons(outgoing, PMT_NIL))));
49
50   // Remember this protocol class.
51   s_ALL_PROTOCOL_CLASSES = pmt_cons(t, s_ALL_PROTOCOL_CLASSES);
52   return t;
53 }
54
55 pmt_t
56 mb_protocol_class_name(pmt_t pc)
57 {
58   return pmt_nth(1, pc);
59 }
60
61 pmt_t
62 mb_protocol_class_incoming(pmt_t pc)
63 {
64   return pmt_nth(2, pc);
65 }
66
67 pmt_t
68 mb_protocol_class_outgoing(pmt_t pc)
69 {
70   return pmt_nth(3, pc);
71 }
72
73 pmt_t
74 mb_protocol_class_lookup(pmt_t name)
75 {
76   pmt_t lst = s_ALL_PROTOCOL_CLASSES;
77
78   while (pmt_is_pair(lst)){
79     if (pmt_eq(name, mb_protocol_class_name(pmt_car(lst))))
80       return pmt_car(lst);
81     lst = pmt_cdr(lst);
82   }
83
84   return PMT_NIL;
85 }
86
87 mb_protocol_class_init::mb_protocol_class_init(const char *data, size_t len)
88 {
89   std::stringbuf sb;
90   sb.str(std::string(data, len));
91
92   while (1){
93     pmt_t obj = pmt_deserialize(sb);
94
95     if (0){
96       pmt_write(obj, std::cout);
97       std::cout << std::endl;
98     }
99
100     if (pmt_is_eof_object(obj))
101       return;
102
103     mb_make_protocol_class(pmt_nth(0, obj),   // protocol-class name
104                            pmt_nth(1, obj),   // list of incoming msg names
105                            pmt_nth(2, obj));  // list of outgoing msg names
106   }
107 }