summaryrefslogtreecommitdiffstats
path: root/dgbuilder/core_nodes/io/31-tcpin.html
blob: c8cec59915163ecfb9c43a58b3a4b14cc184f628 (plain)
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
<!--
  Copyright 2013 IBM Corp.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<script type="text/x-red" data-template-name="tcp in">
    <div class="form-row">
        <label for="node-input-server"><i class="fa fa-dot-circle-o"></i> Type</label>
        <select id="node-input-server" style="width:120px; margin-right:5px;">
            <option value="server">Listen on</option>
            <option value="client">Connect to</option>
        </select>
        port <input type="text" id="node-input-port" style="width: 50px">
    </div>
    <div class="form-row hidden" id="node-input-host-row" style="padding-left: 110px;">
        at host <input type="text" id="node-input-host" placeholder="localhost" style="width: 60%;">
    </div>

    <div class="form-row">
        <label><i class="fa fa-sign-out"></i> Output</label>
        a
        <select id="node-input-datamode" style="width:110px;">
            <option value="stream">stream of</option>
            <option value="single">single</option>
        </select>
        <select id="node-input-datatype" style="width:140px;">
            <option value="buffer">Buffer</option>
            <option value="utf8">String</option>
            <option value="base64">Base64 String</option>
        </select>
        payload<span id="node-input-datamode-plural">s</span>
    </div>

    <div id="node-row-newline" class="form-row hidden" style="padding-left: 110px;">
        delimited by <input type="text" id="node-input-newline" style="width: 110px;">
    </div>

    <div class="form-row">
        <label for="node-input-topic"><i class="fa fa-tasks"></i> Topic</label>
        <input type="text" id="node-input-topic" placeholder="Topic">
    </div>
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
</script>

<script type="text/x-red" data-help-name="tcp in">
    <p>Provides a choice of tcp inputs. Can either connect to a remote tcp port,
       or accept incoming connections.</p>
</script>

<script type="text/javascript">
    RED.nodes.registerType('tcp in',{
        category: 'input',
        color:"Silver",
        defaults: {
            server: {value:"server",required:true},
            host: {value:"",validate:function(v) { return (this.server == "server")||v.length > 0;} },
            port: {value:"",required:true,validate:RED.validators.number()},
            datamode:{value:"stream"},
            datatype:{value:"buffer"},
            newline:{value:""},
            topic: {value:""},
            name: {value:""},
            base64: {/*deprecated*/ value:false,required:true}
        },
        inputs:0,
        outputs:1,
        icon: "bridge-dash.png",
        label: function() {
            return this.name || "tcp:"+(this.host?this.host+":":"")+this.port;
        },
        labelStyle: function() {
            return this.name?"node_label_italic":"";
        },
        oneditprepare: function() {
            var updateOptions = function() {
                var sockettype = $("#node-input-server option:selected").val();
                if (sockettype == "client") {
                    $("#node-input-host-row").show();
                } else {
                    $("#node-input-host-row").hide();
                }
                var datamode = $("#node-input-datamode option:selected").val();
                var datatype = $("#node-input-datatype option:selected").val();
                if (datamode == "stream") {
                    $("#node-input-datamode-plural").show();
                    if (datatype == "utf8") {
                        $("#node-row-newline").show();
                    } else {
                        $("#node-row-newline").hide();
                    }
                } else {
                    $("#node-input-datamode-plural").hide();
                    $("#node-row-newline").hide();
                }
            };
            updateOptions();
            $("#node-input-server").change(updateOptions);
            $("#node-input-datatype").change(updateOptions);
            $("#node-input-datamode").change(updateOptions);
        }
    });
</script>


<script type="text/x-red" data-template-name="tcp out">
    <div class="form-row">
        <label for="node-input-beserver"><i class="fa fa-dot-circle-o"></i> Type</label>
        <select id="node-input-beserver" style="width:150px; margin-right:5px;">
            <option value="server">Listen on</option>
            <option value="client">Connect to</option>
            <option value="reply">Reply to TCP</option>
        </select>
        <span id="node-input-port-row">port <input type="text" id="node-input-port" style="width: 50px"></span>
    </div>

    <div class="form-row hidden" id="node-input-host-row" style="padding-left: 110px;">
        at host <input type="text" id="node-input-host" placeholder="localhost" style="width: 60%;">
    </div>

    <div class="form-row hidden" id="node-input-end-row">
        <label>&nbsp;</label>
        <input type="checkbox" id="node-input-end" style="display: inline-block; width: auto; vertical-align: top;">
        <label for="node-input-end" style="width: 70%;">Close connection after each message is sent ?</label>
    </div>

    <div class="form-row">
        <label>&nbsp;</label>
        <input type="checkbox" id="node-input-base64" placeholder="base64" style="display: inline-block; width: auto; vertical-align: top;">
        <label for="node-input-base64" style="width: 70%;">Decode Base64 message ?</label>
    </div>

    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>

    <div class="form-tips hidden" id="fin-tip">
        <b>Note:</b> Closing the connection after each message is generally not a good thing - but is useful to indicate an end-of-file for example.
    </div>
    <div class="form-tips hidden" id="fin-tip2">
        <b>Note:</b> Closing the connection after each message is generally not a good thing - but is useful to indicate an end-of-file for example. The receiving client will need to reconnect.
    </div>
</script>

<script type="text/x-red" data-help-name="tcp out">
    <p>Provides a choice of tcp outputs. Can either connect to a remote tcp port,
    accept incoming connections, or reply to messages received from a TCP In node.</p>
    <p>Only <b>msg.payload</b> is sent.</p>
    <p>If <b>msg.payload</b> is a string containing a Base64 encoding of binary
    data, the Base64 decoding option will cause it to be converted back to binary
    before being sent.</p>
</script>

<script type="text/javascript">
    RED.nodes.registerType('tcp out',{
        category: 'output',
        color:"Silver",
        defaults: {
            host: {value:"",validate:function(v) { return (this.beserver != "client")||v.length > 0;} },
            port: {value:"",validate:function(v) { return (this.beserver == "reply")||RED.validators.number()(v) } },
            beserver: {value:"client",required:true},
            base64: {value:false,required:true},
            end: {value:false,required:true},
            name: {value:""}
        },
        inputs:1,
        outputs:0,
        icon: "bridge-dash.png",
        align: "right",
        label: function() {
            return this.name || "tcp:"+(this.host?this.host+":":"")+this.port;
        },
        labelStyle: function() {
            return (this.name)?"node_label_italic":"";
        },
        oneditprepare: function() {
            var updateOptions = function() {
                var sockettype = $("#node-input-beserver option:selected").val();
                if (sockettype == "reply") {
                    $("#node-input-port-row").hide();
                    $("#node-input-host-row").hide();
                    $("#node-input-end-row").hide();
                } else {
                    $("#node-input-port-row").show();
                    $("#node-input-end-row").show();
                }

                if (sockettype == "client") {
                    $("#node-input-host-row").show();
                    $("#fin-tip").show();
                } else {
                    $("#node-input-host-row").hide();
                    $("#fin-tip").hide();
                }

                if (sockettype == "server") {
                    $("#fin-tip2").show();
                }
                else {
                    $("#fin-tip2").hide();
                }

            };
            updateOptions();
            $("#node-input-beserver").change(updateOptions);
        }
    });
</script>


<script type="text/x-red" data-template-name="tcp request">
    <div class="form-row">
        <label for="node-input-server"><i class="fa fa-globe"></i> Server</label>
        <input type="text" id="node-input-server" placeholder="ip.address" style="width:50%">
        &nbsp;port <input type="text" id="node-input-port" style="width:50px">
    </div>
    <div class="form-row">
        <label for="node-input-out"><i class="fa fa-sign-out"></i> Return</label>
        <select type="text" id="node-input-out" style="width:52%;">
            <option value="time">after a fixed timeout of</option>
            <option value="char">when character received is</option>
            <option value="count">a fixed number of characters</option>
            <option value="sit">never. Keep connection open</option>
        </select>
        <input type="text" id="node-input-splitc"  style="width:50px;">
        <span id="node-units"></span>
    </div>
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
    <div class="form-tips"><b>Tip:</b> outputs a binary <b>Buffer</b>, so you may want to .toString() it.</div>
    <script>
        var previous = null;
        $("#node-input-out").on('focus', function () { previous = this.value; }).change(function() {
            if (previous == null) { previous = $("#node-input-out").val(); }
            if ($("#node-input-out").val() == "char") {
                if (previous != "char") $("#node-input-splitc").val("\\n");
                $("#node-units").text("");
            }
            else if ($("#node-input-out").val() == "time") {
                if (previous != "time") $("#node-input-splitc").val("0");
                $("#node-units").text("ms");
            }
            else if ($("#node-input-out").val() == "count") {
                if (previous != "count") $("#node-input-splitc").val("12");
                $("#node-units").text("chars");
            }
            else {
                if (previous != "sit") $("#node-input-splitc").val("0");
                $("#node-units").text("");
            }
        });
</script>

<script type="text/x-red" data-help-name="tcp request">
    <p>A simple TCP request node - sends the <b>msg.payload</b> to a server tcp port and expects a response.</p>
    <p>Connects, sends the "request", reads the "response". It can either count a number of
    returned characters into a fixed buffer, match a specified character before returning,
    wait a fixed timeout from first reply and then return, or just sit and wait for data.</p>
    <p>The response will be output in <b>msg.payload</b> as a buffer, so you may want to .toString() it.</p>
</script>

<script type="text/javascript">
    RED.nodes.registerType('tcp request',{
        category: 'function',
        color:"Silver",
        defaults: {
            server: {value:"",required:true},
            port: {value:"",required:true,validate:RED.validators.number()},
            out: {value:"time",required:true},
            splitc: {value:"0",required:true},
            name: {value:""}
        },
        inputs:1,
        outputs:1,
        icon: "bridge-dash.png",
        label: function() {
            return this.name || "tcp:"+(this.server?this.server+":":"")+this.port;
        },
        labelStyle: function() {
            return this.name?"node_label_italic":"";
        }
    });
</script>