/[svn]/linuxsampler/trunk/src/network/lscpresultset.cpp
ViewVC logotype

Annotation of /linuxsampler/trunk/src/network/lscpresultset.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1765 - (hide annotations) (download)
Sat Sep 6 16:44:42 2008 UTC (15 years, 7 months ago) by persson
File size: 6584 byte(s)
* refactoring: extracted lscp notification from main() to a separate
  function
* added helper function MidiInputPort::DispatchRaw for midi device
  implementations with raw byte data
* fixed a win32 build error (atomic.h is working on windows too)
* code cleanup: moved lscp classes into LinuxSampler namespace

1 senkov 113 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5     * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 persson 1765 * Copyright (C) 2005 - 2008 Christian Schoenebeck *
7 senkov 113 * *
8     * This program is free software; you can redistribute it and/or modify *
9     * it under the terms of the GNU General Public License as published by *
10     * the Free Software Foundation; either version 2 of the License, or *
11     * (at your option) any later version. *
12     * *
13     * This program is distributed in the hope that it will be useful, *
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16     * GNU General Public License for more details. *
17     * *
18     * You should have received a copy of the GNU General Public License *
19     * along with this program; if not, write to the Free Software *
20     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21     * MA 02111-1307 USA *
22     ***************************************************************************/
23    
24 senkov 116 /*********************************************************
25     * This class helps to constuct valid resultsets per
26     * LSCP protocol specification
27     *
28     * Valid results include:
29     * OK - to ack the request
30     * Single line to ack the requests and give status
31     * Several lines of information in the following format:
32     * LABEL0: VALUE0
33     * LABEL1: VALUE1
34     * VALELx: VALUEx
35     * .
36     *
37     * ******************************************************/
38    
39 senkov 113 #include "lscpresultset.h"
40 persson 1349 #include <iomanip>
41 schoenebeck 1424 #include "../common/global_private.h"
42 senkov 113
43 persson 1765
44     namespace LinuxSampler {
45    
46 senkov 116 //Construct an empty resultset
47 senkov 120 //Default index is -1 meaning the resultset doesn't have an index
48     LSCPResultSet::LSCPResultSet(int index) {
49     result_index = index;
50 senkov 113 count = 0;
51     storage = "";
52 senkov 120 result_type = result_type_success;
53 senkov 113 }
54    
55 senkov 116 //Construct a resultset with a single line
56 senkov 120 //Default index is -1 meaning the resultset doesn't have an index
57     LSCPResultSet::LSCPResultSet(String Value, int index) {
58     result_index = index;
59 senkov 113 count = 1;
60     storage = Value + "\r\n";
61 senkov 120 result_type = result_type_success;
62 senkov 113 }
63    
64 senkov 116 //Add a label/value pair to the resultset
65     //Values could be of different types for now supports String, int and float.
66 senkov 113 void LSCPResultSet::Add(String Label, String Value) {
67     if (count == -1)
68 schoenebeck 880 throw Exception("Attempting to change already produced resultset");
69 senkov 120 if (result_type != result_type_success)
70 schoenebeck 880 throw Exception("Attempting to create illegal resultset");
71 senkov 113 storage = storage + Label + ": " + Value + "\r\n";
72 schoenebeck 1009 count = 2; // results in form of "Label: Value" should always be handled as multi line responses
73 senkov 113 }
74    
75 schoenebeck 225 void LSCPResultSet::Add(String Label, const char* pValue) {
76     Add(Label, String(pValue));
77     }
78    
79 senkov 397 //Add SQL resultset row
80     void LSCPResultSet::Add(int columns, char** argv) {
81     for (int i = 0; i < columns; i++)
82     {
83     storage += argv[i];
84     if ((i+1) < columns)
85     storage += "|";
86     }
87     storage += "\r\n";
88     count = 2; //This result is always multiline.
89     }
90    
91 senkov 120 void LSCPResultSet::Add(int Value) {
92     Add(ToString(Value));
93     }
94    
95 senkov 113 void LSCPResultSet::Add(String Label, int Value) {
96 senkov 120 Add(Label, ToString(Value));
97 senkov 113 }
98    
99     void LSCPResultSet::Add(String Label, float Value) {
100 persson 1349 std::stringstream ss;
101     ss.imbue(std::locale::classic());
102     ss << std::fixed << std::setprecision(3) << Value;
103     Add(Label, ss.str());
104 senkov 113 }
105    
106 schoenebeck 223 void LSCPResultSet::Add(String Label, bool Value) {
107     String s = (Value) ? "true" : "false";
108     Add(Label, s);
109     }
110    
111 senkov 116 //Add a single string to the resultset
112 senkov 113 void LSCPResultSet::Add(String Value) {
113 senkov 120 if (result_type != result_type_success)
114 schoenebeck 880 throw Exception("Attempting to create illegal resultset");
115 senkov 113 if (count == -1)
116 schoenebeck 880 throw Exception("Attempting to change already produced resultset");
117 senkov 113 if (count != 0)
118 schoenebeck 880 throw Exception("Attempting to create illegal resultset");
119 senkov 113 storage = Value + "\r\n";
120     count = 1;
121     }
122    
123 senkov 120 //Generate an error result set from an exception.
124     //Per LSCP spec, error result is a sinle line in the following format:
125     //ERR:<CODE>:Message text\r\n
126     //This method will be used to generate unknown errors only (code 0)
127     //To generate errors with other codes as well as warnings use other methods (below).
128     //Because this is an unknown error, this method will also print message to the stderr.
129 schoenebeck 880 void LSCPResultSet::Error(Exception e) {
130 senkov 120 e.PrintMessage();
131     Error(e.Message());
132     }
133    
134     //This will construct an error with a string and error code
135     //code has a default of 0
136     //String has a default of "Undefined Error"
137     void LSCPResultSet::Error (String message, int code) {
138     //Even though this is must be a single line resultset we won't throw
139     //anything here because this is already part of exception handling.
140     //We'll just 'forget' all previous results (if any) from this resultset.
141     result_type = result_type_error;
142     storage = "ERR:" + ToString(code) + ":" + message + "\r\n";
143     count = 1;
144     }
145    
146     //This will construct a warning with a string and error code
147     //code has a default of 0
148     //String has a default of "Undefined Error"
149     void LSCPResultSet::Warning (String message, int code) {
150     //FIXME: DO we want warnings as part of the resultset or
151     //do we want them to work like errors??? For now, make them work like errors.
152     result_type = result_type_warning;
153     if (result_index == -1)
154     storage = "WRN:" + ToString(code) + ":" + message + "\r\n";
155     else
156     storage = "WRN[" + ToString(result_index) + "]:" + ToString(code) + ":" + message + "\r\n";
157     count = 1;
158     }
159    
160 senkov 116 //Produce resultset
161 senkov 113 String LSCPResultSet::Produce(void) {
162 senkov 120 //FIXME: I'm assuming that only a sinle like "OK" can have index
163 senkov 116 if (count == 0) //When there is nothing in the resultset we just send "OK" to ack the request
164 senkov 120 if (result_index == -1)
165     return "OK\r\n";
166     else
167     return "OK[" + ToString(result_index) + "]\r\n";
168 senkov 116 if (count == 1) //Single line results are just that, single line
169 senkov 113 return storage;
170 senkov 116 //Multiline results MUST end with a line with a single dot
171 senkov 113 return storage + ".\r\n";
172     }
173 persson 1765
174     }

  ViewVC Help
Powered by ViewVC