| 1 | // This file is part of pluggable-monitor-protocol-handler. |
| 2 | // |
| 3 | // SPDX-FileCopyrightText: Arduino s.r.l. and/or its affiliated companies |
| 4 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 5 | |
| 6 | // Package monitor is a library for handling the Arduino Pluggable-Monitor protocol |
| 7 | // (https://github.com/arduino/tooling-rfcs/blob/main/RFCs/0004-pluggable-monitor.md#pluggable-monitor-api-via-stdinstdout) |
| 8 | // |
| 9 | // The library implements the state machine and the parsing logic to communicate with a pluggable-monitor client. |
| 10 | // All the commands issued by the client are conveniently translated into function calls, in particular |
| 11 | // the Monitor interface are the only functions that must be implemented to get a fully working pluggable monitor |
| 12 | // using this library. |
| 13 | // |
| 14 | // A usage example is provided in the dummy-monitor package. |
| 15 | package monitorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/ |
| 16 | |
| 17 | import ( |
| 18 | "bufioscip-go gomod github.com/golang/go/src go1.16 bufio/" |
| 19 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/" |
| 20 | "fmtscip-go gomod github.com/golang/go/src go1.16 fmt/" |
| 21 | "ioscip-go gomod github.com/golang/go/src go1.16 io/" |
| 22 | "netscip-go gomod github.com/golang/go/src go1.16 net/" |
| 23 | "regexpscip-go gomod github.com/golang/go/src go1.16 regexp/" |
| 24 | "strconvscip-go gomod github.com/golang/go/src go1.16 strconv/" |
| 25 | "stringsscip-go gomod github.com/golang/go/src go1.16 strings/" |
| 26 | "syncscip-go gomod github.com/golang/go/src go1.16 sync/" |
| 27 | |
| 28 | "github.com/hashicorp/go-multierrorscip-go gomod github.com/hashicorp/go-multierror v1.1.1 `github.com/hashicorp/go-multierror`/" |
| 29 | ) |
| 30 | |
| 31 | // Monitor is an interface that represents the business logic that |
| 32 | // a pluggable monitor must implement. The communication protocol |
| 33 | // is completely hidden and it's handled by a MonitorServer. |
| 34 | type Monitorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Monitor# interface { |
| 35 | // Hello is called once at startup to provide the userAgent string |
| 36 | // and the protocolVersion negotiated with the client. |
| 37 | Helloscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Monitor#Hello.(userAgent string, protocolVersion int) error |
| 38 | |
| 39 | // Describe is called to obtain the description of the communication port |
| 40 | Describescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Monitor#Describe.() (*PortDescriptorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/PortDescriptor#, error) |
| 41 | |
| 42 | // Configure allows to set the configuration parameters for the communication port |
| 43 | Configurescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Monitor#Configure.(parameterName string, value string) error |
| 44 | |
| 45 | // Open allows to open a communication with the board using TCP/IP |
| 46 | Openscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Monitor#Open.(boardPort string) (ioscip-go gomod github.com/golang/go/src go1.16 io/.ReadWriterscip-go gomod github.com/golang/go/src go1.16 io/ReadWriter#, error) |
| 47 | |
| 48 | // Close will close the currently open port and TCP/IP connection |
| 49 | Closescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Monitor#Close.() error |
| 50 | |
| 51 | // Quit is called just before the server terminates. This function can be |
| 52 | // used by the monitor as a last chance to gracefully close resources. |
| 53 | Quitscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Monitor#Quit.() |
| 54 | } |
| 55 | |
| 56 | // A Server is a pluggable monitor protocol handler, |
| 57 | // it must be created using the NewServer function. |
| 58 | type Serverscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server# struct { |
| 59 | implscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#impl. Monitorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Monitor# |
| 60 | outscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#out. ioscip-go gomod github.com/golang/go/src go1.16 io/.Writerscip-go gomod github.com/golang/go/src go1.16 io/Writer# |
| 61 | outMutexscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outMutex. syncscip-go gomod github.com/golang/go/src go1.16 sync/.Mutexscip-go gomod github.com/golang/go/src go1.16 sync/Mutex# |
| 62 | userAgentscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#userAgent. string |
| 63 | reqProtocolVersionscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#reqProtocolVersion. int |
| 64 | initializedscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#initialized. bool |
| 65 | clientConnscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#clientConn. netscip-go gomod github.com/golang/go/src go1.16 net/.Connscip-go gomod github.com/golang/go/src go1.16 net/Conn# |
| 66 | closeFuncMutexscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#closeFuncMutex. syncscip-go gomod github.com/golang/go/src go1.16 sync/.Mutexscip-go gomod github.com/golang/go/src go1.16 sync/Mutex# |
| 67 | } |
| 68 | |
| 69 | // NewServer creates a new monitor server backed by the |
| 70 | // provided pluggable monitor implementation. To start the server |
| 71 | // use the Run method. |
| 72 | func NewServerscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/NewServer().(impl Monitorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Monitor#) *Serverscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server# { |
| 73 | return &Serverscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#{ |
| 74 | implscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#impl.: impl, |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Run starts the protocol handling loop on the given input and |
| 79 | // output stream, usually `os.Stdin` and `os.Stdout` are used. |
| 80 | // The function blocks until the `QUIT` command is received or |
| 81 | // the input stream is closed. In case of IO error the error is |
| 82 | // returned. |
| 83 | func (d *Serverscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#) Runscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#Run().(in ioscip-go gomod github.com/golang/go/src go1.16 io/.Readerscip-go gomod github.com/golang/go/src go1.16 io/Reader#, out ioscip-go gomod github.com/golang/go/src go1.16 io/.Writerscip-go gomod github.com/golang/go/src go1.16 io/Writer#) error { |
| 84 | d.outscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#out. = out |
| 85 | reader := bufioscip-go gomod github.com/golang/go/src go1.16 bufio/.NewReaderscip-go gomod github.com/golang/go/src go1.16 bufio/NewReader().(in) |
| 86 | for { |
| 87 | fullCmd, err := reader.ReadStringscip-go gomod github.com/golang/go/src go1.16 bufio/Reader#ReadString().('\n') |
| 88 | if err != nil { |
| 89 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(messageErrorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/messageError().("command_error", err.Error())) |
| 90 | return err |
| 91 | } |
| 92 | fullCmd = stringsscip-go gomod github.com/golang/go/src go1.16 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.16 strings/TrimSpace().(fullCmd) |
| 93 | split := stringsscip-go gomod github.com/golang/go/src go1.16 strings/.Splitscip-go gomod github.com/golang/go/src go1.16 strings/Split().(fullCmd, " ") |
| 94 | cmd := stringsscip-go gomod github.com/golang/go/src go1.16 strings/.ToUpperscip-go gomod github.com/golang/go/src go1.16 strings/ToUpper().(split[0]) |
| 95 | |
| 96 | if !d.initializedscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#initialized. && cmd != "HELLO" && cmd != "QUIT" { |
| 97 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(messageErrorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/messageError().("command_error", fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().("First command must be HELLO, but got '%s'", cmd))) |
| 98 | continue |
| 99 | } |
| 100 | |
| 101 | switch cmd { |
| 102 | case "HELLO": |
| 103 | d.helloscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#hello().(fullCmd[6:]) |
| 104 | case "DESCRIBE": |
| 105 | d.describescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#describe().() |
| 106 | case "CONFIGURE": |
| 107 | d.configurescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#configure().(fullCmd[10:]) |
| 108 | case "OPEN": |
| 109 | d.openscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#open().(fullCmd[5:]) |
| 110 | case "CLOSE": |
| 111 | d.closescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#close().("") |
| 112 | case "QUIT": |
| 113 | d.implscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#impl..Quitscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Monitor#Quit.() |
| 114 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(messageOkscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/messageOk().("quit")) |
| 115 | return nil |
| 116 | default: |
| 117 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(messageErrorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/messageError().("command_error", fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().("Command %s not supported", cmd))) |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | func (d *Serverscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#) helloscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#hello().(cmd string) { |
| 123 | if d.initializedscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#initialized. { |
| 124 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(messageErrorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/messageError().("hello", "HELLO already called")) |
| 125 | return |
| 126 | } |
| 127 | re := regexpscip-go gomod github.com/golang/go/src go1.16 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.16 regexp/MustCompile().(`^(\d+) "([^"]+)"$`) |
| 128 | matches := re.FindStringSubmatchscip-go gomod github.com/golang/go/src go1.16 regexp/Regexp#FindStringSubmatch().(cmd) |
| 129 | if len(matches) != 3 { |
| 130 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(messageErrorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/messageError().("hello", "Invalid HELLO command")) |
| 131 | return |
| 132 | } |
| 133 | d.userAgentscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#userAgent. = matches[2] |
| 134 | v, err := strconvscip-go gomod github.com/golang/go/src go1.16 strconv/.ParseIntscip-go gomod github.com/golang/go/src go1.16 strconv/ParseInt().(matches[1], 10, 64) |
| 135 | if err != nil { |
| 136 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(messageErrorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/messageError().("hello", "Invalid protocol version: "+matches[2])) |
| 137 | return |
| 138 | } |
| 139 | d.reqProtocolVersionscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#reqProtocolVersion. = int(v) |
| 140 | if err := d.implscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#impl..Helloscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Monitor#Hello.(d.userAgentscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#userAgent., 1); err != nil { |
| 141 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(messageErrorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/messageError().("hello", err.Error())) |
| 142 | return |
| 143 | } |
| 144 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(&messagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/message#{ |
| 145 | EventTypescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/message#EventType.: "hello", |
| 146 | ProtocolVersionscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/message#ProtocolVersion.: 1, // Protocol version 1 is the only supported for now... |
| 147 | Messagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/message#Message.: "OK", |
| 148 | }) |
| 149 | d.initializedscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#initialized. = true |
| 150 | } |
| 151 | |
| 152 | func (d *Serverscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#) describescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#describe().() { |
| 153 | if !d.initializedscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#initialized. { |
| 154 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(messageErrorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/messageError().("describe", "Monitor not initialized")) |
| 155 | return |
| 156 | } |
| 157 | portDescription, err := d.implscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#impl..Describescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Monitor#Describe.() |
| 158 | if err != nil { |
| 159 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(messageErrorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/messageError().("describe", err.Error())) |
| 160 | return |
| 161 | } |
| 162 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(&messagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/message#{ |
| 163 | EventTypescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/message#EventType.: "describe", |
| 164 | Messagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/message#Message.: "OK", |
| 165 | PortDescriptionscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/message#PortDescription.: portDescription, |
| 166 | }) |
| 167 | } |
| 168 | |
| 169 | func (d *Serverscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#) configurescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#configure().(cmd string) { |
| 170 | if !d.initializedscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#initialized. { |
| 171 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(messageErrorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/messageError().("configure", "Monitor not initialized")) |
| 172 | return |
| 173 | } |
| 174 | re := regexpscip-go gomod github.com/golang/go/src go1.16 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.16 regexp/MustCompile().(`^([\w.-]+) (.+)$`) |
| 175 | matches := re.FindStringSubmatchscip-go gomod github.com/golang/go/src go1.16 regexp/Regexp#FindStringSubmatch().(cmd) |
| 176 | if len(matches) != 3 { |
| 177 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(messageErrorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/messageError().("configure", "Invalid CONFIGURE command")) |
| 178 | return |
| 179 | } |
| 180 | parameterName := matches[1] |
| 181 | value := matches[2] |
| 182 | if err := d.implscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#impl..Configurescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Monitor#Configure.(parameterName, value); err != nil { |
| 183 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(messageErrorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/messageError().("configure", err.Error())) |
| 184 | return |
| 185 | } |
| 186 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(&messagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/message#{ |
| 187 | EventTypescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/message#EventType.: "configure", |
| 188 | Messagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/message#Message.: "OK", |
| 189 | }) |
| 190 | } |
| 191 | |
| 192 | func (d *Serverscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#) openscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#open().(cmd string) { |
| 193 | if !d.initializedscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#initialized. { |
| 194 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(messageErrorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/messageError().("open", "Monitor not initialized")) |
| 195 | return |
| 196 | } |
| 197 | parameters := stringsscip-go gomod github.com/golang/go/src go1.16 strings/.SplitNscip-go gomod github.com/golang/go/src go1.16 strings/SplitN().(cmd, " ", 2) |
| 198 | if len(parameters) != 2 { |
| 199 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(messageErrorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/messageError().("open", "Invalid OPEN command")) |
| 200 | return |
| 201 | } |
| 202 | address := parameters[0] |
| 203 | portName := parameters[1] |
| 204 | port, err := d.implscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#impl..Openscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Monitor#Open.(portName) |
| 205 | if err != nil { |
| 206 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(messageErrorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/messageError().("open", err.Error())) |
| 207 | return |
| 208 | } |
| 209 | d.clientConnscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#clientConn., err = netscip-go gomod github.com/golang/go/src go1.16 net/.Dialscip-go gomod github.com/golang/go/src go1.16 net/Dial().("tcp", address) |
| 210 | if err != nil { |
| 211 | d.implscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#impl..Closescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Monitor#Close.() |
| 212 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(messageErrorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/messageError().("open", err.Error())) |
| 213 | return |
| 214 | } |
| 215 | // io.Copy is used to bridge the Client's TCP connection to the port one and vice versa |
| 216 | go func() { |
| 217 | _, err := ioscip-go gomod github.com/golang/go/src go1.16 io/.Copyscip-go gomod github.com/golang/go/src go1.16 io/Copy().(port, d.clientConnscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#clientConn.) // Copy is blocking, so we run it insiede a gorutine |
| 218 | if err != nil { |
| 219 | d.closescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#close().(err.Error()) |
| 220 | } else { |
| 221 | d.closescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#close().("lost TCP/IP connection with the client!") |
| 222 | } |
| 223 | }() |
| 224 | go func() { |
| 225 | _, err := ioscip-go gomod github.com/golang/go/src go1.16 io/.Copyscip-go gomod github.com/golang/go/src go1.16 io/Copy().(d.clientConnscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#clientConn., port) // Copy is blocking, so we run it insiede a gorutine |
| 226 | if err != nil { |
| 227 | d.closescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#close().(err.Error()) |
| 228 | } else { |
| 229 | d.closescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#close().("lost connection with the port") |
| 230 | } |
| 231 | }() |
| 232 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(&messagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/message#{ |
| 233 | EventTypescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/message#EventType.: "open", |
| 234 | Messagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/message#Message.: "OK", |
| 235 | }) |
| 236 | } |
| 237 | |
| 238 | func (d *Serverscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#) closescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#close().(messageErr string) { |
| 239 | d.closeFuncMutexscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#closeFuncMutex..Lockscip-go gomod github.com/golang/go/src go1.16 sync/Mutex#Lock().() |
| 240 | defer d.closeFuncMutexscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#closeFuncMutex..Unlockscip-go gomod github.com/golang/go/src go1.16 sync/Mutex#Unlock().() |
| 241 | if d.clientConnscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#clientConn. == nil { |
| 242 | if messageErr == "" { |
| 243 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(messageErrorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/messageError().("close", "port already closed")) |
| 244 | } |
| 245 | return |
| 246 | } |
| 247 | connErr := d.clientConnscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#clientConn..Closescip-go gomod github.com/golang/go/src go1.16 net/Conn#Close().() |
| 248 | portErr := d.implscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#impl..Closescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Monitor#Close.() |
| 249 | d.clientConnscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#clientConn. = nil |
| 250 | if messageErr != "" { |
| 251 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(messageErrorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/messageError().("port_closed", messageErr)) |
| 252 | return |
| 253 | } |
| 254 | if connErr != nil || portErr != nil { |
| 255 | var errs *multierrorscip-go gomod github.com/hashicorp/go-multierror v1.1.1 `github.com/hashicorp/go-multierror`/.Errorscip-go gomod github.com/hashicorp/go-multierror v1.1.1 `github.com/hashicorp/go-multierror`/Error# |
| 256 | errs = multierrorscip-go gomod github.com/hashicorp/go-multierror v1.1.1 `github.com/hashicorp/go-multierror`/.Appendscip-go gomod github.com/hashicorp/go-multierror v1.1.1 `github.com/hashicorp/go-multierror`/Append().(errs, connErr, portErr) |
| 257 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(messageErrorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/messageError().("close", errs.Errorscip-go gomod github.com/hashicorp/go-multierror v1.1.1 `github.com/hashicorp/go-multierror`/Error#Error().())) |
| 258 | return |
| 259 | } |
| 260 | d.outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(&messagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/message#{ |
| 261 | EventTypescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/message#EventType.: "close", |
| 262 | Messagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/message#Message.: "OK", |
| 263 | }) |
| 264 | } |
| 265 | |
| 266 | func (d *Serverscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#) outputMessagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outputMessage().(msg *messagescip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/message#) { |
| 267 | data, err := jsonscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/.MarshalIndentscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/MarshalIndent().(msg, "", " ") |
| 268 | if err != nil { |
| 269 | // We are certain that this will be marshalled correctly so we don't handle the error |
| 270 | data, _ = jsonscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/.MarshalIndentscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/MarshalIndent().(messageErrorscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/messageError().("command_error", err.Error()), "", " ") |
| 271 | } |
| 272 | d.outMutexscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outMutex..Lockscip-go gomod github.com/golang/go/src go1.16 sync/Mutex#Lock().() |
| 273 | fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Fprintlnscip-go gomod github.com/golang/go/src go1.16 fmt/Fprintln().(d.outscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#out., string(data)) |
| 274 | d.outMutexscip-go gomod github.com/arduino/pluggable-monitor-protocol-handler . `github.com/arduino/pluggable-monitor-protocol-handler`/Server#outMutex..Unlockscip-go gomod github.com/golang/go/src go1.16 sync/Mutex#Unlock().() |
| 275 | } |