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

Diff of /linuxsampler/branches/release0_5_0/src/network/lscpresultset.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 113 by senkov, Sun Jun 6 20:59:49 2004 UTC revision 397 by senkov, Mon Feb 21 04:28:50 2005 UTC
# Line 20  Line 20 
20   *   MA  02111-1307  USA                                                   *   *   MA  02111-1307  USA                                                   *
21   ***************************************************************************/   ***************************************************************************/
22    
23    /*********************************************************
24     * This class helps to constuct valid resultsets per
25     * LSCP protocol specification
26     *
27     * Valid results include:
28     * OK - to ack the request
29     * Single line to ack the requests and give status
30     * Several lines of information in the following format:
31     * LABEL0: VALUE0
32     * LABEL1: VALUE1
33     * VALELx: VALUEx
34     * .
35     *
36     * ******************************************************/
37    
38  #include "lscpresultset.h"  #include "lscpresultset.h"
 #include "../common/LinuxSamplerException.h"  
39    
40  LSCPResultSet::LSCPResultSet(void) {  //Construct an empty resultset
41    //Default index is -1 meaning the resultset doesn't have an index
42    LSCPResultSet::LSCPResultSet(int index) {
43            result_index = index;
44          count = 0;          count = 0;
45          storage = "";          storage = "";
46            result_type = result_type_success;
47  }  }
48    
49  LSCPResultSet::LSCPResultSet(String Value) {  //Construct a resultset with a single line
50    //Default index is -1 meaning the resultset doesn't have an index
51    LSCPResultSet::LSCPResultSet(String Value, int index) {
52            result_index = index;
53          count = 1;          count = 1;
54          storage = Value + "\r\n";          storage = Value + "\r\n";
55            result_type = result_type_success;
56  }  }
57    
58    //Add a label/value pair to the resultset
59    //Values could be of different types for now supports String, int and float.
60  void LSCPResultSet::Add(String Label, String Value) {  void LSCPResultSet::Add(String Label, String Value) {
61          if (count == -1)          if (count == -1)
62                  throw LinuxSamplerException("Attempting to change already produced resultset");                  throw LinuxSamplerException("Attempting to change already produced resultset");
63            if (result_type != result_type_success)
64                    throw LinuxSamplerException("Attempting to create illegal resultset");
65          storage = storage + Label + ": " + Value + "\r\n";          storage = storage + Label + ": " + Value + "\r\n";
66          count++;          count++;
67  }  }
68    
69    void LSCPResultSet::Add(String Label, const char* pValue) {
70        Add(Label, String(pValue));
71    }
72    
73    //Add SQL resultset row
74    void LSCPResultSet::Add(int columns, char** argv) {
75            for (int i = 0; i < columns; i++)
76            {
77                    storage += argv[i];
78                    if ((i+1) < columns)
79                            storage += "|";
80            }
81            storage += "\r\n";
82            count = 2; //This result is always multiline.
83    }
84    
85    void LSCPResultSet::Add(int Value) {
86            Add(ToString(Value));
87    }
88    
89  void LSCPResultSet::Add(String Label, int Value) {  void LSCPResultSet::Add(String Label, int Value) {
90          char temp[16];          Add(Label, ToString(Value));
         snprintf(temp, sizeof(temp), "%i", Value);  
         Add(Label, temp);  
91  }  }
92    
93  void LSCPResultSet::Add(String Label, float Value) {  void LSCPResultSet::Add(String Label, float Value) {
94          char temp[16];      char s[1024];
95          snprintf(temp, sizeof(temp), "%10.4f", Value);      snprintf(s, 1023, "%.3f", Value);
96          Add(Label, temp);      Add(Label, String(s));
97  }  }
98    
99    void LSCPResultSet::Add(String Label, bool Value) {
100        String s = (Value) ? "true" : "false";
101        Add(Label, s);
102    }
103    
104    //Add a single string to the resultset
105  void LSCPResultSet::Add(String Value) {  void LSCPResultSet::Add(String Value) {
106            if (result_type != result_type_success)
107                    throw LinuxSamplerException("Attempting to create illegal resultset");
108          if (count == -1)          if (count == -1)
109                  throw LinuxSamplerException("Attempting to change already produced resultset");                  throw LinuxSamplerException("Attempting to change already produced resultset");
110          if (count != 0)          if (count != 0)
# Line 61  void LSCPResultSet::Add(String Value) { Line 113  void LSCPResultSet::Add(String Value) {
113          count = 1;          count = 1;
114  }  }
115    
116    //Generate an error result set from an exception.
117    //Per LSCP spec, error result is a sinle line in the following format:
118    //ERR:<CODE>:Message text\r\n
119    //This method will be used to generate unknown errors only (code 0)
120    //To generate errors with other codes as well as warnings use other methods (below).
121    //Because this is an unknown error, this method will also print message to the stderr.
122    void LSCPResultSet::Error(LinuxSamplerException e) {
123            e.PrintMessage();
124            Error(e.Message());
125    }
126    
127    //This will construct an error with a string and error code
128    //code has a default of 0
129    //String has a default of "Undefined Error"
130    void LSCPResultSet::Error (String message, int code) {
131            //Even though this is must be a single line resultset we won't throw
132            //anything here because this is already part of exception handling.
133            //We'll just 'forget' all previous results (if any) from this resultset.
134            result_type = result_type_error;
135            storage = "ERR:" + ToString(code) + ":" + message + "\r\n";
136            count = 1;
137    }
138    
139    //This will construct a warning with a string and error code
140    //code has a default of 0
141    //String has a default of "Undefined Error"
142    void LSCPResultSet::Warning (String message, int code) {
143            //FIXME: DO we want warnings as part of the resultset or
144            //do we want them to work like errors??? For now, make them work like errors.
145            result_type = result_type_warning;
146            if (result_index == -1)
147                    storage = "WRN:" + ToString(code) + ":" + message + "\r\n";
148            else
149                    storage = "WRN[" + ToString(result_index) + "]:" + ToString(code) + ":" + message + "\r\n";
150            count = 1;
151    }
152    
153    //Produce resultset
154  String LSCPResultSet::Produce(void) {  String LSCPResultSet::Produce(void) {
155          if (count == 0)          //FIXME: I'm assuming that only a sinle like "OK" can have index
156                  return "OK\r\n";          if (count == 0) //When there is nothing in the resultset we just send "OK" to ack the request
157          if (count == 1)                  if (result_index == -1)
158                            return "OK\r\n";
159                    else
160                            return "OK[" + ToString(result_index) + "]\r\n";
161            if (count == 1) //Single line results are just that, single line
162                  return storage;                  return storage;
163            //Multiline results MUST end with a line with a single dot
164          return storage + ".\r\n";          return storage + ".\r\n";
165  }  }

Legend:
Removed from v.113  
changed lines
  Added in v.397

  ViewVC Help
Powered by ViewVC