-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
191 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2019 Red Hat Inc | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*******************************************************************************/ | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/ctron/hot/pkg/utils" | ||
"pack.ag/amqp" | ||
) | ||
|
||
func consume(messageType string, uri string, tenant string) error { | ||
|
||
fmt.Printf("Consuming %s from %s ...", messageType, uri) | ||
fmt.Println() | ||
|
||
opts := make([]amqp.ConnOption, 0) | ||
if insecure { | ||
opts = append(opts, amqp.ConnTLSConfig(createTlsConfig())) | ||
} | ||
|
||
client, err := amqp.Dial(uri, opts...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer func() { | ||
if err := client.Close(); err != nil { | ||
log.Fatal("Failed to close client:", err) | ||
} | ||
}() | ||
|
||
var ctx = context.Background() | ||
|
||
session, err := client.NewSession() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer func() { | ||
if err := session.Close(ctx); err != nil { | ||
log.Fatal("Failed to close session:", err) | ||
} | ||
}() | ||
|
||
receiver, err := session.NewReceiver( | ||
amqp.LinkSourceAddress(fmt.Sprintf("%s/%s", messageType, tenant)), | ||
amqp.LinkCredit(10), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
ctx, cancel := context.WithTimeout(ctx, 1*time.Second) | ||
if err := receiver.Close(ctx); err != nil { | ||
log.Fatal("Failed to close receiver: ", err) | ||
} | ||
cancel() | ||
}() | ||
|
||
fmt.Printf("Consumer running, press Ctrl+C to stop...") | ||
fmt.Println() | ||
|
||
for { | ||
// Receive next message | ||
msg, err := receiver.Receive(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Accept message | ||
if err := msg.Accept(); err != nil { | ||
return nil | ||
} | ||
|
||
utils.PrintMessage(msg) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2019 Red Hat Inc | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*******************************************************************************/ | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"net/http" | ||
neturl "net/url" | ||
) | ||
|
||
func publishHttp(messageType string, uri string, tenant string, deviceId string, authId string, password string, contentType string, payload string) error { | ||
|
||
url, err := neturl.Parse(uri) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
url.Path = url.Path + neturl.PathEscape(messageType) + "/" + neturl.PathEscape(tenant) + "/" + deviceId | ||
fmt.Println("URL:", url) | ||
|
||
buf := bytes.NewBufferString(payload) | ||
|
||
tr := &http.Transport{ | ||
TLSClientConfig: createTlsConfig(), | ||
} | ||
|
||
client := &http.Client{Transport: tr} | ||
request, err := http.NewRequest("PUT", url.String(), buf) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
request.SetBasicAuth(authId+"@"+tenant, password) | ||
|
||
request.Header.Set("Content-Type", contentType) | ||
|
||
response, err := client.Do(request) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Publish result: %s", response.Status) | ||
fmt.Println() | ||
|
||
body := new(bytes.Buffer) | ||
if _, err := body.ReadFrom(response.Body); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(body.String()) | ||
|
||
if err := response.Body.Close(); err != nil { | ||
fmt.Printf("Failed to close response: %v", err) | ||
} | ||
|
||
return nil | ||
} |