aboutsummaryrefslogtreecommitdiffstats
path: root/gnuradio-runtime/lib/hier_block2_detail.cc
blob: feaf17f27754ea88d0249c4ab0d9e65f3983cd89 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
/* -*- c++ -*- */
/*
 * Copyright 2006,2007,2009,2013 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "hier_block2_detail.h"
#include <gnuradio/io_signature.h>
#include <gnuradio/prefs.h>
#include <boost/format.hpp>
#include <sstream>
#include <stdexcept>

namespace gr {

#define HIER_BLOCK2_DETAIL_DEBUG 0

hier_block2_detail::hier_block2_detail(hier_block2* owner)
    : d_owner(owner), d_parent_detail(0), d_fg(make_flowgraph())
{
    int min_inputs = owner->input_signature()->min_streams();
    int max_inputs = owner->input_signature()->max_streams();
    int min_outputs = owner->output_signature()->min_streams();
    int max_outputs = owner->output_signature()->max_streams();

    if (max_inputs == io_signature::IO_INFINITE ||
        max_outputs == io_signature::IO_INFINITE || (min_inputs != max_inputs) ||
        (min_outputs != max_outputs)) {
        std::stringstream msg;
        msg << "Hierarchical blocks do not yet support arbitrary or"
            << " variable numbers of inputs or outputs (" << d_owner->name() << ")";
        throw std::runtime_error(msg.str());
    }

    d_inputs = std::vector<endpoint_vector_t>(max_inputs);
    d_outputs = endpoint_vector_t(max_outputs);

    d_max_output_buffer = std::vector<size_t>(std::max(max_outputs, 1), 0);
    d_min_output_buffer = std::vector<size_t>(std::max(max_outputs, 1), 0);
}

hier_block2_detail::~hier_block2_detail()
{
    d_owner = 0; // Don't use delete, we didn't allocate
}

void hier_block2_detail::connect(basic_block_sptr block)
{
    std::stringstream msg;

    // Check if duplicate
    if (std::find(d_blocks.begin(), d_blocks.end(), block) != d_blocks.end()) {
        msg << "Block " << block << " already connected.";
        throw std::invalid_argument(msg.str());
    }

    // Check if has inputs or outputs
    if (block->input_signature()->max_streams() != 0 ||
        block->output_signature()->max_streams() != 0) {
        msg << "Block " << block << " must not have any input or output ports";
        throw std::invalid_argument(msg.str());
    }

    hier_block2_sptr hblock(cast_to_hier_block2_sptr(block));

    if (hblock && hblock.get() != d_owner) {
        if (HIER_BLOCK2_DETAIL_DEBUG)
            std::cout << "connect: block is hierarchical, setting parent to " << this
                      << std::endl;
        hblock->d_detail->d_parent_detail = this;
    }

    d_blocks.push_back(block);
}

void hier_block2_detail::connect(basic_block_sptr src,
                                 int src_port,
                                 basic_block_sptr dst,
                                 int dst_port)
{
    std::stringstream msg;

    if (HIER_BLOCK2_DETAIL_DEBUG)
        std::cout << "connecting: " << endpoint(src, src_port) << " -> "
                  << endpoint(dst, dst_port) << std::endl;

    if (src.get() == dst.get())
        throw std::invalid_argument(
            "connect: src and destination blocks cannot be the same");

    hier_block2_sptr src_block(cast_to_hier_block2_sptr(src));
    hier_block2_sptr dst_block(cast_to_hier_block2_sptr(dst));

    if (src_block && src.get() != d_owner) {
        if (HIER_BLOCK2_DETAIL_DEBUG)
            std::cout << "connect: src is hierarchical, setting parent to " << this
                      << std::endl;
        src_block->d_detail->d_parent_detail = this;
    }

    if (dst_block && dst.get() != d_owner) {
        if (HIER_BLOCK2_DETAIL_DEBUG)
            std::cout << "connect: dst is hierarchical, setting parent to " << this
                      << std::endl;
        dst_block->d_detail->d_parent_detail = this;
    }

    // Connections to block inputs or outputs
    int max_port;
    if (src.get() == d_owner) {
        max_port = src->input_signature()->max_streams();
        if ((max_port != -1 && (src_port >= max_port)) || src_port < 0) {
            msg << "source port " << src_port << " out of range for " << src;
            throw std::invalid_argument(msg.str());
        }

        return connect_input(src_port, dst_port, dst);
    }

    if (dst.get() == d_owner) {
        max_port = dst->output_signature()->max_streams();
        if ((max_port != -1 && (dst_port >= max_port)) || dst_port < 0) {
            msg << "destination port " << dst_port << " out of range for " << dst;
            throw std::invalid_argument(msg.str());
        }

        return connect_output(dst_port, src_port, src);
    }

    // Internal connections
    d_fg->connect(src, src_port, dst, dst_port);

    // TODO: connects to NC
}

void hier_block2_detail::msg_connect(basic_block_sptr src,
                                     pmt::pmt_t srcport,
                                     basic_block_sptr dst,
                                     pmt::pmt_t dstport)
{
    if (HIER_BLOCK2_DETAIL_DEBUG)
        std::cout << "connecting message port..." << std::endl;

    // add block uniquely to list to internal blocks
    if (std::find(d_blocks.begin(), d_blocks.end(), dst) == d_blocks.end()) {
        d_blocks.push_back(src);
        d_blocks.push_back(dst);
    }


    bool hier_in = false, hier_out = false;
    if (d_owner == src.get()) {
        hier_out = src->message_port_is_hier_in(srcport);
    } else if (d_owner == dst.get()) {
        hier_in = dst->message_port_is_hier_out(dstport);
        ;
    } else {
        hier_out = src->message_port_is_hier_out(srcport);
        hier_in = dst->message_port_is_hier_in(dstport);
    }

    hier_block2_sptr src_block(cast_to_hier_block2_sptr(src));
    hier_block2_sptr dst_block(cast_to_hier_block2_sptr(dst));

    if (src_block && src.get() != d_owner) {
        if (HIER_BLOCK2_DETAIL_DEBUG)
            std::cout << "msg_connect: src is hierarchical, setting parent to " << this
                      << std::endl;
        src_block->d_detail->d_parent_detail = this;
    }

    if (dst_block && dst.get() != d_owner) {
        if (HIER_BLOCK2_DETAIL_DEBUG)
            std::cout << "msg_connect: dst is hierarchical, setting parent to " << this
                      << std::endl;
        dst_block->d_detail->d_parent_detail = this;
    }

    // add edge for this message connection
    if (HIER_BLOCK2_DETAIL_DEBUG)
        std::cout << boost::format("msg_connect( (%s, %s, %d), (%s, %s, %d) )\n") % src %
                         srcport % hier_out % dst % dstport % hier_in;
    d_fg->connect(msg_endpoint(src, srcport, hier_out),
                  msg_endpoint(dst, dstport, hier_in));
}

void hier_block2_detail::msg_disconnect(basic_block_sptr src,
                                        pmt::pmt_t srcport,
                                        basic_block_sptr dst,
                                        pmt::pmt_t dstport)
{
    if (HIER_BLOCK2_DETAIL_DEBUG)
        std::cout << "disconnecting message port..." << std::endl;

    // remove edge for this message connection
    bool hier_in = false, hier_out = false;
    if (d_owner == src.get()) {
        hier_out = src->message_port_is_hier_in(srcport);
    } else if (d_owner == dst.get()) {
        hier_in = dst->message_port_is_hier_out(dstport);
        ;
    } else {
        hier_out = src->message_port_is_hier_out(srcport);
        hier_in = dst->message_port_is_hier_in(dstport);
    }

    d_fg->disconnect(msg_endpoint(src, srcport, hier_out),
                     msg_endpoint(dst, dstport, hier_in));

    hier_block2_sptr src_block(cast_to_hier_block2_sptr(src));
    hier_block2_sptr dst_block(cast_to_hier_block2_sptr(dst));

    if (src_block && src.get() != d_owner) {
        // if the source is hier, we need to resolve the endpoint before calling unsub
        msg_edge_vector_t edges = src_block->d_detail->d_fg->msg_edges();
        for (msg_edge_viter_t it = edges.begin(); it != edges.end(); ++it) {
            if ((*it).dst().block() == src) {
                src = (*it).src().block();
                srcport = (*it).src().port();
            }
        }
    }

    if (dst_block && dst.get() != d_owner) {
        // if the destination is hier, we need to resolve the endpoint before calling
        // unsub
        msg_edge_vector_t edges = dst_block->d_detail->d_fg->msg_edges();
        for (msg_edge_viter_t it = edges.begin(); it != edges.end(); ++it) {
            if ((*it).src().block() == dst) {
                dst = (*it).dst().block();
                dstport = (*it).dst().port();
            }
        }
    }

    // unregister the subscription - if already subscribed
    src->message_port_unsub(srcport, pmt::cons(dst->alias_pmt(), dstport));
}

void hier_block2_detail::disconnect(basic_block_sptr block)
{
    // Check on singleton list
    for (basic_block_viter_t p = d_blocks.begin(); p != d_blocks.end(); p++) {
        if (*p == block) {
            d_blocks.erase(p);

            hier_block2_sptr hblock(cast_to_hier_block2_sptr(block));
            if (block && block.get() != d_owner) {
                if (HIER_BLOCK2_DETAIL_DEBUG)
                    std::cout << "disconnect: block is hierarchical, clearing parent"
                              << std::endl;
                hblock->d_detail->d_parent_detail = 0;
            }

            return;
        }
    }

    // Otherwise find all edges containing block
    edge_vector_t edges, tmp = d_fg->edges();
    edge_vector_t::iterator p;
    for (p = tmp.begin(); p != tmp.end(); p++) {
        if ((*p).src().block() == block || (*p).dst().block() == block) {
            edges.push_back(*p);

            if (HIER_BLOCK2_DETAIL_DEBUG)
                std::cout << "disconnect: block found in edge " << (*p) << std::endl;
        }
    }

    if (edges.empty()) {
        std::stringstream msg;
        msg << "cannot disconnect block " << block << ", not found";
        throw std::invalid_argument(msg.str());
    }

    for (p = edges.begin(); p != edges.end(); p++) {
        disconnect(
            (*p).src().block(), (*p).src().port(), (*p).dst().block(), (*p).dst().port());
    }
}

void hier_block2_detail::disconnect(basic_block_sptr src,
                                    int src_port,
                                    basic_block_sptr dst,
                                    int dst_port)
{
    if (HIER_BLOCK2_DETAIL_DEBUG)
        std::cout << "disconnecting: " << endpoint(src, src_port) << " -> "
                  << endpoint(dst, dst_port) << std::endl;

    if (src.get() == dst.get())
        throw std::invalid_argument(
            "disconnect: source and destination blocks cannot be the same");

    hier_block2_sptr src_block(cast_to_hier_block2_sptr(src));
    hier_block2_sptr dst_block(cast_to_hier_block2_sptr(dst));

    if (src_block && src.get() != d_owner) {
        if (HIER_BLOCK2_DETAIL_DEBUG)
            std::cout << "disconnect: src is hierarchical, clearing parent" << std::endl;
        src_block->d_detail->d_parent_detail = 0;
    }

    if (dst_block && dst.get() != d_owner) {
        if (HIER_BLOCK2_DETAIL_DEBUG)
            std::cout << "disconnect: dst is hierarchical, clearing parent" << std::endl;
        dst_block->d_detail->d_parent_detail = 0;
    }

    if (src.get() == d_owner)
        return disconnect_input(src_port, dst_port, dst);

    if (dst.get() == d_owner)
        return disconnect_output(dst_port, src_port, src);

    // Internal connections
    d_fg->disconnect(src, src_port, dst, dst_port);
}

void hier_block2_detail::refresh_io_signature()
{
    int min_inputs = d_owner->input_signature()->min_streams();
    int max_inputs = d_owner->input_signature()->max_streams();
    int min_outputs = d_owner->output_signature()->min_streams();
    int max_outputs = d_owner->output_signature()->max_streams();

    if (max_inputs == io_signature::IO_INFINITE ||
        max_outputs == io_signature::IO_INFINITE || (min_inputs != max_inputs) ||
        (min_outputs != max_outputs)) {
        std::stringstream msg;
        msg << "Hierarchical blocks do not yet support arbitrary or"
            << " variable numbers of inputs or outputs (" << d_owner->name() << ")";
        throw std::runtime_error(msg.str());
    }

    // Check for # input change
    if ((signed)d_inputs.size() != max_inputs) {
        d_inputs.resize(max_inputs);
    }

    // Check for # output change
    if ((signed)d_outputs.size() != max_outputs) {
        d_outputs.resize(max_outputs);
        d_min_output_buffer.resize(max_outputs, 0);
        d_max_output_buffer.resize(max_outputs, 0);
    }
}

void hier_block2_detail::connect_input(int my_port, int port, basic_block_sptr block)
{
    std::stringstream msg;

    refresh_io_signature();

    if (my_port < 0 || my_port >= (signed)d_inputs.size()) {
        msg << "input port " << my_port << " out of range for " << block;
        throw std::invalid_argument(msg.str());
    }

    endpoint_vector_t& endps = d_inputs[my_port];
    endpoint endp(block, port);

    endpoint_viter_t p = std::find(endps.begin(), endps.end(), endp);
    if (p != endps.end()) {
        msg << "external input port " << my_port << " already wired to " << endp;
        throw std::invalid_argument(msg.str());
    }

    endps.push_back(endp);
}

void hier_block2_detail::connect_output(int my_port, int port, basic_block_sptr block)
{
    std::stringstream msg;

    refresh_io_signature();

    if (my_port < 0 || my_port >= (signed)d_outputs.size()) {
        msg << "output port " << my_port << " out of range for " << block;
        throw std::invalid_argument(msg.str());
    }

    if (d_outputs[my_port].block()) {
        msg << "external output port " << my_port << " already connected from "
            << d_outputs[my_port];
        throw std::invalid_argument(msg.str());
    }

    d_outputs[my_port] = endpoint(block, port);
}

void hier_block2_detail::disconnect_input(int my_port, int port, basic_block_sptr block)
{
    std::stringstream msg;

    refresh_io_signature();

    if (my_port < 0 || my_port >= (signed)d_inputs.size()) {
        msg << "input port number " << my_port << " out of range for " << block;
        throw std::invalid_argument(msg.str());
    }

    endpoint_vector_t& endps = d_inputs[my_port];
    endpoint endp(block, port);

    endpoint_viter_t p = std::find(endps.begin(), endps.end(), endp);
    if (p == endps.end()) {
        msg << "external input port " << my_port << " not connected to " << endp;
        throw std::invalid_argument(msg.str());
    }

    endps.erase(p);
}

void hier_block2_detail::disconnect_output(int my_port, int port, basic_block_sptr block)
{
    std::stringstream msg;

    refresh_io_signature();

    if (my_port < 0 || my_port >= (signed)d_outputs.size()) {
        msg << "output port number " << my_port << " out of range for " << block;
        throw std::invalid_argument(msg.str());
    }

    if (d_outputs[my_port].block() != block) {
        msg << "block " << block << " not assigned to output " << my_port
            << ", can't disconnect";
        throw std::invalid_argument(msg.str());
    }

    d_outputs[my_port] = endpoint();
}

endpoint_vector_t hier_block2_detail::resolve_port(int port, bool is_input)
{
    std::stringstream msg;

    if (HIER_BLOCK2_DETAIL_DEBUG)
        std::cout << "Resolving port " << port << " as an "
                  << (is_input ? "input" : "output") << " of " << d_owner->name()
                  << std::endl;

    endpoint_vector_t result;

    if (is_input) {
        if (port < 0 || port >= (signed)d_inputs.size()) {
            msg << "resolve_port: hierarchical block '" << d_owner->name() << "': input "
                << port << " is out of range";
            throw std::runtime_error(msg.str());
        }

        if (d_inputs[port].empty()) {
            msg << "resolve_port: hierarchical block '" << d_owner->name() << "': input "
                << port << " is not connected internally";
            throw std::runtime_error(msg.str());
        }

        endpoint_vector_t& endps = d_inputs[port];
        endpoint_viter_t p;
        for (p = endps.begin(); p != endps.end(); p++) {
            endpoint_vector_t tmp = resolve_endpoint(*p, true);
            std::copy(tmp.begin(), tmp.end(), back_inserter(result));
        }
    } else {
        if (port < 0 || port >= (signed)d_outputs.size()) {
            msg << "resolve_port: hierarchical block '" << d_owner->name() << "': output "
                << port << " is out of range";
            throw std::runtime_error(msg.str());
        }

        if (d_outputs[port] == endpoint()) {
            msg << "resolve_port: hierarchical block '" << d_owner->name() << "': output "
                << port << " is not connected internally";
            throw std::runtime_error(msg.str());
        }

        result = resolve_endpoint(d_outputs[port], false);
    }

    if (result.empty()) {
        msg << "resolve_port: hierarchical block '" << d_owner->name()
            << "': unable to resolve " << (is_input ? "input port " : "output port ")
            << port;
        throw std::runtime_error(msg.str());
    }

    return result;
}

void hier_block2_detail::disconnect_all()
{
    d_fg->clear();
    d_blocks.clear();

    int max_inputs = d_owner->input_signature()->max_streams();
    int max_outputs = d_owner->output_signature()->max_streams();
    d_inputs = std::vector<endpoint_vector_t>(max_inputs);
    d_outputs = endpoint_vector_t(max_outputs);
}

endpoint_vector_t hier_block2_detail::resolve_endpoint(const endpoint& endp,
                                                       bool is_input) const
{
    std::stringstream msg;
    endpoint_vector_t result;

    // Check if endpoint is a leaf node
    if (cast_to_block_sptr(endp.block())) {
        if (HIER_BLOCK2_DETAIL_DEBUG)
            std::cout << "Block " << endp.block() << " is a leaf node, returning."
                      << std::endl;
        result.push_back(endp);
        return result;
    }

    // Check if endpoint is a hierarchical block
    hier_block2_sptr hier_block2(cast_to_hier_block2_sptr(endp.block()));
    if (hier_block2) {
        if (HIER_BLOCK2_DETAIL_DEBUG)
            std::cout << "Resolving endpoint " << endp << " as an "
                      << (is_input ? "input" : "output") << ", recursing" << std::endl;
        return hier_block2->d_detail->resolve_port(endp.port(), is_input);
    }

    msg << "unable to resolve" << (is_input ? " input " : " output ") << "endpoint "
        << endp;
    throw std::runtime_error(msg.str());
}

void hier_block2_detail::flatten_aux(flat_flowgraph_sptr sfg) const
{
    if (HIER_BLOCK2_DETAIL_DEBUG)
        std::cout << " ** Flattening " << d_owner->name()
                  << " parent: " << d_parent_detail << std::endl;
    bool is_top_block = (d_parent_detail == NULL);

    // Add my edges to the flow graph, resolving references to actual endpoints
    edge_vector_t edges = d_fg->edges();
    msg_edge_vector_t msg_edges = d_fg->msg_edges();
    edge_viter_t p;
    msg_edge_viter_t q;

    // Only run setup_rpc if ControlPort config param is enabled.
    bool ctrlport_on = prefs::singleton()->get_bool("ControlPort", "on", false);

    int min_buff = 0;
    int max_buff = 0;
    // Determine how the buffers should be set
    bool set_all_min_buff = d_owner->all_min_output_buffer_p();
    bool set_all_max_buff = d_owner->all_max_output_buffer_p();
    // Get the min and max buffer length
    if (set_all_min_buff) {
        if (HIER_BLOCK2_DETAIL_DEBUG)
            std::cout << "Getting (" << (d_owner->alias()).c_str() << ") min buffer"
                      << std::endl;
        min_buff = d_owner->min_output_buffer();
    }
    if (set_all_max_buff) {
        if (HIER_BLOCK2_DETAIL_DEBUG)
            std::cout << "Getting (" << (d_owner->alias()).c_str() << ") max buffer"
                      << std::endl;
        max_buff = d_owner->max_output_buffer();
    }

    // For every block (gr::block and gr::hier_block2), set up the RPC
    // interface.
    for (p = edges.begin(); p != edges.end(); p++) {
        basic_block_sptr b;
        b = p->src().block();

        if (set_all_min_buff) {
            // sets the min buff for every block within hier_block2
            if (min_buff != 0) {
                block_sptr bb = boost::dynamic_pointer_cast<block>(b);
                if (bb != 0) {
                    if (bb->min_output_buffer(0) != min_buff) {
                        if (HIER_BLOCK2_DETAIL_DEBUG)
                            std::cout << "Block (" << (bb->alias()).c_str()
                                      << ") min_buff (" << min_buff << ")" << std::endl;
                        bb->set_min_output_buffer(min_buff);
                    }
                } else {
                    hier_block2_sptr hh = boost::dynamic_pointer_cast<hier_block2>(b);
                    if (hh != 0) {
                        if (hh->min_output_buffer(0) != min_buff) {
                            if (HIER_BLOCK2_DETAIL_DEBUG)
                                std::cout << "HBlock (" << (hh->alias()).c_str()
                                          << ") min_buff (" << min_buff << ")"
                                          << std::endl;
                            hh->set_min_output_buffer(min_buff);
                        }
                    }
                }
            }
        }
        if (set_all_max_buff) {
            // sets the max buff for every block within hier_block2
            if (max_buff != 0) {
                block_sptr bb = boost::dynamic_pointer_cast<block>(b);
                if (bb != 0) {
                    if (bb->max_output_buffer(0) != max_buff) {
                        if (HIER_BLOCK2_DETAIL_DEBUG)
                            std::cout << "Block (" << (bb->alias()).c_str()
                                      << ") max_buff (" << max_buff << ")" << std::endl;
                        bb->set_max_output_buffer(max_buff);
                    }
                } else {
                    hier_block2_sptr hh = boost::dynamic_pointer_cast<hier_block2>(b);
                    if (hh != 0) {
                        if (hh->max_output_buffer(0) != max_buff) {
                            if (HIER_BLOCK2_DETAIL_DEBUG)
                                std::cout << "HBlock (" << (hh->alias()).c_str()
                                          << ") max_buff (" << max_buff << ")"
                                          << std::endl;
                            hh->set_max_output_buffer(max_buff);
                        }
                    }
                }
            }
        }

        b = p->dst().block();
        if (set_all_min_buff) {
            // sets the min buff for every block within hier_block2
            if (min_buff != 0) {
                block_sptr bb = boost::dynamic_pointer_cast<block>(b);
                if (bb != 0) {
                    if (bb->min_output_buffer(0) != min_buff) {
                        if (HIER_BLOCK2_DETAIL_DEBUG)
                            std::cout << "Block (" << (bb->alias()).c_str()
                                      << ") min_buff (" << min_buff << ")" << std::endl;
                        bb->set_min_output_buffer(min_buff);
                    }
                } else {
                    hier_block2_sptr hh = boost::dynamic_pointer_cast<hier_block2>(b);
                    if (hh != 0) {
                        if (hh->min_output_buffer(0) != min_buff) {
                            if (HIER_BLOCK2_DETAIL_DEBUG)
                                std::cout << "HBlock (" << (hh->alias()).c_str()
                                          << ") min_buff (" << min_buff << ")"
                                          << std::endl;
                            hh->set_min_output_buffer(min_buff);
                        }
                    }
                }
            }
        }
        if (set_all_max_buff) {
            // sets the max buff for every block within hier_block2
            if (max_buff != 0) {
                block_sptr bb = boost::dynamic_pointer_cast<block>(b);
                if (bb != 0) {
                    if (bb->max_output_buffer(0) != max_buff) {
                        if (HIER_BLOCK2_DETAIL_DEBUG)
                            std::cout << "Block (" << (bb->alias()).c_str()
                                      << ") max_buff (" << max_buff << ")" << std::endl;
                        bb->set_max_output_buffer(max_buff);
                    }
                } else {
                    hier_block2_sptr hh = boost::dynamic_pointer_cast<hier_block2>(b);
                    if (hh != 0) {
                        if (hh->max_output_buffer(0) != max_buff) {
                            if (HIER_BLOCK2_DETAIL_DEBUG)
                                std::cout << "HBlock (" << (hh->alias()).c_str()
                                          << ") max_buff (" << max_buff << ")"
                                          << std::endl;
                            hh->set_max_output_buffer(max_buff);
                        }
                    }
                }
            }
        }
    }

    if (HIER_BLOCK2_DETAIL_DEBUG)
        std::cout << "Flattening stream connections: " << std::endl;

    for (p = edges.begin(); p != edges.end(); p++) {
        if (HIER_BLOCK2_DETAIL_DEBUG)
            std::cout << "Flattening edge " << (*p) << std::endl;

        endpoint_vector_t src_endps = resolve_endpoint(p->src(), false);
        endpoint_vector_t dst_endps = resolve_endpoint(p->dst(), true);

        endpoint_viter_t s, d;
        for (s = src_endps.begin(); s != src_endps.end(); s++) {
            for (d = dst_endps.begin(); d != dst_endps.end(); d++) {
                if (HIER_BLOCK2_DETAIL_DEBUG)
                    std::cout << (*s) << "->" << (*d) << std::endl;
                sfg->connect(*s, *d);
            }
        }
    }

    // loop through flattening hierarchical connections
    if (HIER_BLOCK2_DETAIL_DEBUG)
        std::cout << "Flattening msg connections: " << std::endl;

    std::vector<std::pair<msg_endpoint, bool>> resolved_endpoints;
    for (q = msg_edges.begin(); q != msg_edges.end(); q++) {
        if (HIER_BLOCK2_DETAIL_DEBUG)
            std::cout << boost::format(
                             " flattening edge ( %s, %s, %d) -> ( %s, %s, %d)\n") %
                             q->src().block() % q->src().port() % q->src().is_hier() %
                             q->dst().block() % q->dst().port() % q->dst().is_hier();


        if (q->src().is_hier() && q->src().block().get() == d_owner) {
            // connection into this block ..
            if (HIER_BLOCK2_DETAIL_DEBUG)
                std::cout << "hier incoming port: " << q->src() << std::endl;
            sfg->replace_endpoint(q->src(), q->dst(), false);
            resolved_endpoints.push_back(std::pair<msg_endpoint, bool>(q->src(), false));
        } else if (q->dst().is_hier() && q->dst().block().get() == d_owner) {
            // connection out of this block
            if (HIER_BLOCK2_DETAIL_DEBUG)
                std::cout << "hier outgoing port: " << q->dst() << std::endl;
            sfg->replace_endpoint(q->dst(), q->src(), true);
            resolved_endpoints.push_back(std::pair<msg_endpoint, bool>(q->dst(), true));
        } else {
            // internal connection only
            if (HIER_BLOCK2_DETAIL_DEBUG)
                std::cout << "internal msg connection: " << q->src() << "-->" << q->dst()
                          << std::endl;
            sfg->connect(q->src(), q->dst());
        }
    }

    for (std::vector<std::pair<msg_endpoint, bool>>::iterator it =
             resolved_endpoints.begin();
         it != resolved_endpoints.end();
         it++) {
        if (HIER_BLOCK2_DETAIL_DEBUG)
            std::cout << "sfg->clear_endpoint(" << (*it).first << ", " << (*it).second
                      << ") " << std::endl;
        sfg->clear_endpoint((*it).first, (*it).second);
    }

    /*
    // connect primitive edges in the new fg
    for(q = msg_edges.begin(); q != msg_edges.end(); q++) {
      if((!q->src().is_hier()) && (!q->dst().is_hier())) {
        sfg->connect(q->src(), q->dst());
      }
      else {
        std::cout << "not connecting hier connection!" << std::endl;
      }
    }
    */

    // Construct unique list of blocks used either in edges, inputs,
    // outputs, or by themselves.  I still hate STL.
    basic_block_vector_t blocks; // unique list of used blocks
    basic_block_vector_t tmp = d_fg->calc_used_blocks();

    // First add the list of singleton blocks
    std::vector<basic_block_sptr>::const_iterator b; // Because flatten_aux is const
    for (b = d_blocks.begin(); b != d_blocks.end(); b++) {
        tmp.push_back(*b);
    }

    // Now add the list of connected input blocks
    std::stringstream msg;
    for (unsigned int i = 0; i < d_inputs.size(); i++) {
        if (d_inputs[i].empty()) {
            msg << "In hierarchical block " << d_owner->name() << ", input " << i
                << " is not connected internally";
            throw std::runtime_error(msg.str());
        }

        for (unsigned int j = 0; j < d_inputs[i].size(); j++)
            tmp.push_back(d_inputs[i][j].block());
    }

    for (unsigned int i = 0; i < d_outputs.size(); i++) {
        basic_block_sptr blk = d_outputs[i].block();
        if (!blk) {
            msg << "In hierarchical block " << d_owner->name() << ", output " << i
                << " is not connected internally";
            throw std::runtime_error(msg.str());
        }
        // Set the buffers of only the blocks connected to the hier output
        if (!set_all_min_buff) {
            min_buff = d_owner->min_output_buffer(i);
            if (min_buff != 0) {
                block_sptr bb = boost::dynamic_pointer_cast<block>(blk);
                if (bb != 0) {
                    int bb_src_port = d_outputs[i].port();
                    if (HIER_BLOCK2_DETAIL_DEBUG)
                        std::cout << "Block (" << (bb->alias()).c_str() << ") Port ("
                                  << bb_src_port << ") min_buff (" << min_buff << ")"
                                  << std::endl;
                    bb->set_min_output_buffer(bb_src_port, min_buff);
                } else {
                    hier_block2_sptr hh = boost::dynamic_pointer_cast<hier_block2>(blk);
                    if (hh != 0) {
                        int hh_src_port = d_outputs[i].port();
                        if (HIER_BLOCK2_DETAIL_DEBUG)
                            std::cout << "HBlock (" << (hh->alias()).c_str() << ") Port ("
                                      << hh_src_port << ") min_buff (" << min_buff << ")"
                                      << std::endl;
                        hh->set_min_output_buffer(hh_src_port, min_buff);
                    }
                }
            }
        }
        if (!set_all_max_buff) {
            max_buff = d_owner->max_output_buffer(i);
            if (max_buff != 0) {
                block_sptr bb = boost::dynamic_pointer_cast<block>(blk);
                if (bb != 0) {
                    int bb_src_port = d_outputs[i].port();
                    if (HIER_BLOCK2_DETAIL_DEBUG)
                        std::cout << "Block (" << (bb->alias()).c_str() << ") Port ("
                                  << bb_src_port << ") max_buff (" << max_buff << ")"
                                  << std::endl;
                    bb->set_max_output_buffer(bb_src_port, max_buff);
                } else {
                    hier_block2_sptr hh = boost::dynamic_pointer_cast<hier_block2>(blk);
                    if (hh != 0) {
                        int hh_src_port = d_outputs[i].port();
                        if (HIER_BLOCK2_DETAIL_DEBUG)
                            std::cout << "HBlock (" << (hh->alias()).c_str() << ") Port ("
                                      << hh_src_port << ") max_buff (" << max_buff << ")"
                                      << std::endl;
                        hh->set_max_output_buffer(hh_src_port, max_buff);
                    }
                }
            }
        }
        tmp.push_back(blk);
    }
    sort(tmp.begin(), tmp.end());

    std::insert_iterator<basic_block_vector_t> inserter(blocks, blocks.begin());
    unique_copy(tmp.begin(), tmp.end(), inserter);

    // Recurse hierarchical children
    for (basic_block_viter_t p = blocks.begin(); p != blocks.end(); p++) {
        hier_block2_sptr hier_block2(cast_to_hier_block2_sptr(*p));
        if (hier_block2 && (hier_block2.get() != d_owner)) {
            if (HIER_BLOCK2_DETAIL_DEBUG)
                std::cout << "flatten_aux: recursing into hierarchical block "
                          << hier_block2->alias() << std::endl;
            hier_block2->d_detail->flatten_aux(sfg);
        }
    }

    // prune any remaining hier connections
    //   if they were not replaced with hier internal connections while in sub-calls
    //   they must remain unconnected and can be deleted...
    if (is_top_block) {
        sfg->clear_hier();
    }

    // print all primitive connections at exit
    if (HIER_BLOCK2_DETAIL_DEBUG && is_top_block) {
        std::cout << "flatten_aux finished in top_block" << std::endl;
        sfg->dump();
    }

    // if ctrlport is enabled, call setup RPC for all blocks in the flowgraph
    if (ctrlport_on) {
        for (b = blocks.begin(); b != blocks.end(); b++) {
            if (!(*b)->is_rpc_set()) {
                (*b)->setup_rpc();
                (*b)->rpc_set();
            }
        }
    }
}

void hier_block2_detail::lock()
{
    if (HIER_BLOCK2_DETAIL_DEBUG)
        std::cout << "lock: entered in " << this << std::endl;

    if (d_parent_detail)
        d_parent_detail->lock();
    else
        d_owner->lock();
}

void hier_block2_detail::unlock()
{
    if (HIER_BLOCK2_DETAIL_DEBUG)
        std::cout << "unlock: entered in " << this << std::endl;

    if (d_parent_detail)
        d_parent_detail->unlock();
    else
        d_owner->unlock();
}

void hier_block2_detail::set_processor_affinity(const std::vector<int>& mask)
{
    basic_block_vector_t tmp = d_fg->calc_used_blocks();
    for (basic_block_viter_t p = tmp.begin(); p != tmp.end(); p++) {
        (*p)->set_processor_affinity(mask);
    }
}

void hier_block2_detail::unset_processor_affinity()
{
    basic_block_vector_t tmp = d_fg->calc_used_blocks();
    for (basic_block_viter_t p = tmp.begin(); p != tmp.end(); p++) {
        (*p)->unset_processor_affinity();
    }
}

std::vector<int> hier_block2_detail::processor_affinity()
{
    basic_block_vector_t tmp = d_fg->calc_used_blocks();
    return tmp[0]->processor_affinity();
}

void hier_block2_detail::set_log_level(std::string level)
{
    basic_block_vector_t tmp = d_fg->calc_used_blocks();
    for (basic_block_viter_t p = tmp.begin(); p != tmp.end(); p++) {
        (*p)->set_log_level(level);
    }
}

std::string hier_block2_detail::log_level()
{
    // Assume that log_level was set for all hier_block2 blocks
    basic_block_vector_t tmp = d_fg->calc_used_blocks();
    return tmp[0]->log_level();
}


} /* namespace gr */