comparison utilities/vampipe-server.cpp @ 40:55d69b26d4db

Pull out CountingPluginHandleMapper; consts
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 22 Aug 2016 17:16:44 +0100
parents ba58fe5ee2dd
children f4244a2d55ac
comparison
equal deleted inserted replaced
39:183be3bc9d7b 40:55d69b26d4db
1 1
2 #include "VampnProto.h" 2 #include "VampnProto.h"
3 3
4 #include "bits/RequestOrResponse.h" 4 #include "bits/RequestOrResponse.h"
5 #include "bits/CountingPluginHandleMapper.h"
5 6
6 #include <iostream> 7 #include <iostream>
7 #include <sstream> 8 #include <sstream>
8 #include <stdexcept> 9 #include <stdexcept>
9 10
25 "and writes Vamp response messages in the same format to stdout.\n\n"; 26 "and writes Vamp response messages in the same format to stdout.\n\n";
26 27
27 exit(2); 28 exit(2);
28 } 29 }
29 30
30 class Mapper : public PluginHandleMapper 31 static CountingPluginHandleMapper mapper;
31 {
32 public:
33 Mapper() : m_nextHandle(1) { }
34
35 void addPlugin(Plugin *p) {
36 if (m_rplugins.find(p) == m_rplugins.end()) {
37 int32_t h = m_nextHandle++;
38 m_plugins[h] = p;
39 m_rplugins[p] = h;
40 }
41 }
42
43 void removePlugin(int32_t h) {
44 if (m_plugins.find(h) == m_plugins.end()) {
45 throw NotFound();
46 }
47 Plugin *p = m_plugins[h];
48 m_plugins.erase(h);
49 if (isConfigured(h)) {
50 m_configuredPlugins.erase(h);
51 m_channelCounts.erase(h);
52 }
53 m_rplugins.erase(p);
54 }
55
56 int32_t pluginToHandle(Plugin *p) {
57 if (m_rplugins.find(p) == m_rplugins.end()) {
58 throw NotFound();
59 }
60 return m_rplugins[p];
61 }
62
63 Plugin *handleToPlugin(int32_t h) {
64 if (m_plugins.find(h) == m_plugins.end()) {
65 throw NotFound();
66 }
67 return m_plugins[h];
68 }
69
70 bool isConfigured(int32_t h) {
71 return m_configuredPlugins.find(h) != m_configuredPlugins.end();
72 }
73
74 void markConfigured(int32_t h, int channelCount, int blockSize) {
75 m_configuredPlugins.insert(h);
76 m_channelCounts[h] = channelCount;
77 m_blockSizes[h] = blockSize;
78 }
79
80 int getChannelCount(int32_t h) {
81 if (m_channelCounts.find(h) == m_channelCounts.end()) {
82 throw NotFound();
83 }
84 return m_channelCounts[h];
85 }
86
87 int getBlockSize(int32_t h) {
88 if (m_blockSizes.find(h) == m_blockSizes.end()) {
89 throw NotFound();
90 }
91 return m_blockSizes[h];
92 }
93
94 private:
95 int32_t m_nextHandle; // NB plugin handle type must fit in JSON number
96 map<uint32_t, Plugin *> m_plugins;
97 map<Plugin *, uint32_t> m_rplugins;
98 set<uint32_t> m_configuredPlugins;
99 map<uint32_t, int> m_channelCounts;
100 map<uint32_t, int> m_blockSizes;
101 };
102
103 static Mapper mapper;
104 32
105 RequestOrResponse 33 RequestOrResponse
106 readRequestCapnp() 34 readRequestCapnp()
107 { 35 {
108 RequestOrResponse rr; 36 RequestOrResponse rr;