source: OpenRLabs-Git/web2py/applications/rlabs/static/js/guacamole-common-js/modules/Parser.js

main
Last change on this file was 42bd667, checked in by David Fuertes <dfuertes@…>, 4 years ago

Historial Limpio

  • Property mode set to 100644
File size: 4.8 KB
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20var Guacamole = Guacamole || {};
21
22/**
23 * Simple Guacamole protocol parser that invokes an oninstruction event when
24 * full instructions are available from data received via receive().
25 *
26 * @constructor
27 */
28Guacamole.Parser = function() {
29
30    /**
31     * Reference to this parser.
32     * @private
33     */
34    var parser = this;
35
36    /**
37     * Current buffer of received data. This buffer grows until a full
38     * element is available. After a full element is available, that element
39     * is flushed into the element buffer.
40     *
41     * @private
42     */
43    var buffer = "";
44
45    /**
46     * Buffer of all received, complete elements. After an entire instruction
47     * is read, this buffer is flushed, and a new instruction begins.
48     *
49     * @private
50     */
51    var element_buffer = [];
52
53    // The location of the last element's terminator
54    var element_end = -1;
55
56    // Where to start the next length search or the next element
57    var start_index = 0;
58
59    /**
60     * Appends the given instruction data packet to the internal buffer of
61     * this Guacamole.Parser, executing all completed instructions at
62     * the beginning of this buffer, if any.
63     *
64     * @param {String} packet The instruction data to receive.
65     */
66    this.receive = function(packet) {
67
68        // Truncate buffer as necessary
69        if (start_index > 4096 && element_end >= start_index) {
70
71            buffer = buffer.substring(start_index);
72
73            // Reset parse relative to truncation
74            element_end -= start_index;
75            start_index = 0;
76
77        }
78
79        // Append data to buffer
80        buffer += packet;
81
82        // While search is within currently received data
83        while (element_end < buffer.length) {
84
85            // If we are waiting for element data
86            if (element_end >= start_index) {
87
88                // We now have enough data for the element. Parse.
89                var element = buffer.substring(start_index, element_end);
90                var terminator = buffer.substring(element_end, element_end+1);
91
92                // Add element to array
93                element_buffer.push(element);
94
95                // If last element, handle instruction
96                if (terminator == ";") {
97
98                    // Get opcode
99                    var opcode = element_buffer.shift();
100
101                    // Call instruction handler.
102                    if (parser.oninstruction != null)
103                        parser.oninstruction(opcode, element_buffer);
104
105                    // Clear elements
106                    element_buffer.length = 0;
107
108                }
109                else if (terminator != ',')
110                    throw new Error("Illegal terminator.");
111
112                // Start searching for length at character after
113                // element terminator
114                start_index = element_end + 1;
115
116            }
117
118            // Search for end of length
119            var length_end = buffer.indexOf(".", start_index);
120            if (length_end != -1) {
121
122                // Parse length
123                var length = parseInt(buffer.substring(element_end+1, length_end));
124                if (isNaN(length))
125                    throw new Error("Non-numeric character in element length.");
126
127                // Calculate start of element
128                start_index = length_end + 1;
129
130                // Calculate location of element terminator
131                element_end = start_index + length;
132
133            }
134           
135            // If no period yet, continue search when more data
136            // is received
137            else {
138                start_index = buffer.length;
139                break;
140            }
141
142        } // end parse loop
143
144    };
145
146    /**
147     * Fired once for every complete Guacamole instruction received, in order.
148     *
149     * @event
150     * @param {String} opcode The Guacamole instruction opcode.
151     * @param {Array} parameters The parameters provided for the instruction,
152     *                           if any.
153     */
154    this.oninstruction = null;
155
156};
Note: See TracBrowser for help on using the repository browser.