Skip to content

Commit

Permalink
Address PR comments.
Browse files Browse the repository at this point in the history
Removed unneccesary '\n' from log messages.
  • Loading branch information
petedyerarm committed Dec 22, 2023
1 parent ea4166b commit 5b80fa4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 54 deletions.
4 changes: 2 additions & 2 deletions edge-resource-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func main() {
flag.Parse()

if configFlag != nil {
log.Infof("config file: %s\n", *configFlag)
log.Infof("config file: %s", *configFlag)
}

// Initialization starts off with reading in the entire config file,
Expand All @@ -53,7 +53,7 @@ func main() {
err := config.LoadFromFile(*configFlag)

if err != nil {
log.Errorf("Critical error. Config file parse failed --> %s\n", err.Error())
log.Errorf("Critical error. Config file parse failed --> %s", err.Error())
os.Exit(1)
}

Expand Down
56 changes: 28 additions & 28 deletions resourcemanager/jsonrpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,11 @@ func (c *Client) CallWithContext(ctx context.Context, method string, args interf

select {
case <-ctx.Done():
log.Debug("Client.CallWithContext(): context has been cancelled. Disconnect from the server and abort in-flight requests\n")
log.Debug("Client.CallWithContext(): context has been cancelled. Disconnect from the server and abort in-flight requests")

return errCancelContext
case <-time.After(requestTimeout):
log.Debug("Client.CallWithContext(): request timeout\n")
log.Debug("Client.CallWithContext(): request timeout")

return errRequestTimeout
case res := <-resp:
Expand Down Expand Up @@ -271,7 +271,7 @@ func (c *Client) CallWithTimeout(method string, args interface{}, result interfa

select {
case <-time.After(duration):
log.Debug("Client.CallWithContext(): request timeout\n")
log.Debug("Client.CallWithContext(): request timeout")

return errRequestTimeout
case res := <-resp:
Expand Down Expand Up @@ -302,11 +302,11 @@ func (c *Client) RespondWithContext(ctx context.Context, id string, res *json.Ra

select {
case <-ctx.Done():
log.Debug("Client.RespondWithContext(): context has been cancelled. Disconnect from the server and abort in-flight requests\n")
log.Debug("Client.RespondWithContext(): context has been cancelled. Disconnect from the server and abort in-flight requests")

return errCancelContext
case <-time.After(respondTimeout):
log.Debug("Client.RespondWithContext(): send response timeout\n")
log.Debug("Client.RespondWithContext(): send response timeout")

return errRespondTimeout
case err := <-op.errChan:
Expand All @@ -318,7 +318,7 @@ func (c *Client) RespondWithContext(ctx context.Context, id string, res *json.Ra
// Close terminates the connection between the client and the websocket server, aborting any in-flight calls
func (c *Client) Close() {
c.cancel()
log.Debug("Client.Close(): connection has been closed\n")
log.Debug("Client.Close(): connection has been closed")
}

// run makes sure that it intializes the connection and handles the reconnection to the server side
Expand All @@ -331,7 +331,7 @@ func (c *Client) run(ctx context.Context) {
}()

for {
log.Debugf("Client.run(): dialing into the websocket server - %s:%s\n", c.socket, c.path)
log.Debugf("Client.run(): dialing into the websocket server - %s:%s", c.socket, c.path)

var dialer websocket.Dialer
dialer.NetDial = func(network, address string) (net.Conn, error) {
Expand All @@ -344,17 +344,17 @@ func (c *Client) run(ctx context.Context) {
if err != nil {
select {
case <-ctx.Done():
log.Debug("Client.run(): parent context has been cancelled, terminate the connection. Quitting....\n")
log.Debug("Client.run(): parent context has been cancelled, terminate the connection. Quitting....")

return
case <-time.After(reconnectWaitTime):
log.Debugf("Client.run(): failed to connect to the unix domain server: %s:%s. Error: %s\n.", c.socket, c.path, err.Error())
log.Debugf("Client.run(): failed to connect to the unix domain server: %s:%s. Error: %s.", c.socket, c.path, err.Error())
}

continue
}

log.Debugf("Client.run(): successfully established a connection to the websocket server: %s:%s\n", c.socket, c.path)
log.Debugf("Client.run(): successfully established a connection to the websocket server: %s:%s", c.socket, c.path)

go c.consume(conn)

Expand All @@ -364,14 +364,14 @@ func (c *Client) run(ctx context.Context) {

if c.onConn != nil {
if err := c.onConn(c); err != nil {
log.Debugf("Client.run(): unable to execute onInit callback func successfully. Error: %s\n", err.Error())
log.Debugf("Client.run(): unable to execute onInit callback func successfully. Error: %s", err.Error())
}
}

select {
// client error sent from consume() routine, that loop would exit right after sending the client error into the channel
case err := <-c.clientErr:
log.Debugf("Client.run(): there is an exception during the connection. Ready to reconnect to the server. Error: %s\n", err.Error())
log.Debugf("Client.run(): there is an exception during the connection. Ready to reconnect to the server. Error: %s", err.Error())

conn.Close()

Expand All @@ -380,18 +380,18 @@ func (c *Client) run(ctx context.Context) {

select {
case <-ctx.Done():
log.Debug("Client.run(): parent context has been cancelled, terminate the connection. Quitting....\n")
log.Debug("Client.run(): parent context has been cancelled, terminate the connection. Quitting....")

return
case <-time.After(reconnectWaitTime):
log.Debugf("Client.run(): failed to connect to the unix domain server: %s:%s. Error: %s\n.", c.socket, c.path, err.Error())
log.Debugf("Client.run(): failed to connect to the unix domain server: %s:%s. Error: %s.", c.socket, c.path, err.Error())
}

continue
case <-ctx.Done():
// abort in-flight requests. Kill dispatch() routine
cancel()
log.Debug("Client.run(): parent context has been cancelled, terminate the connection. Quitting....\n")
log.Debug("Client.run(): parent context has been cancelled, terminate the connection. Quitting....")

return
}
Expand All @@ -418,65 +418,65 @@ func (c *Client) dispatch(ctx context.Context, conn *websocket.Conn) {
select {
case req := <-c.requestOps:
if conn == nil {
log.Debug("Client.dispatch(): failed to receive the call request since there is no active websocket connection\n")
log.Debug("Client.dispatch(): failed to receive the call request since there is no active websocket connection")

req.err <- errNilConnection
return
}

msg, err := encodeClientRequest(req.id, req.method, req.args)
if err != nil {
log.Debugf("Client.dispatch(): failed to encode the client request as a json rpc call. Error: %s\n", err.Error())
log.Debugf("Client.dispatch(): failed to encode the client request as a json rpc call. Error: %s", err.Error())

req.err <- err
continue
}

err = conn.WriteMessage(websocket.BinaryMessage, msg)
if err != nil {
log.Debugf("Client.dispatch(): failed to send the call request through the websocket connection. Error: %s\n", err.Error())
log.Debugf("Client.dispatch(): failed to send the call request through the websocket connection. Error: %s", err.Error())

req.err <- err
return
}

log.Debugf("Client.dispatch(): successfully send the call request through the websocket connection. Request:{id: %s, method: %s, params: %v}\n", req.id, req.method, req.args)
log.Debugf("Client.dispatch(): successfully send the call request through the websocket connection. Request:{id: %s, method: %s, params: %v}", req.id, req.method, req.args)
case reqSent := <-c.requestDone:
if c.deliveryMap == nil {
log.Debug("Client.dispatch(): found empty delivery map. Client should be reinialized...\n")
log.Debug("Client.dispatch(): found empty delivery map. Client should be reinialized...")

continue
}

c.deliveryMap.removeRequestOp(reqSent.id, reqSent.response)
case resp := <-c.responseOps:
if conn == nil {
log.Debug("Client.dispatch(): failed to send back the response since there is no active websocket connection\n")
log.Debug("Client.dispatch(): failed to send back the response since there is no active websocket connection")

resp.errChan <- errNilConnection
return
}

msg, err := encodeClientResponse(resp.id, resp.result, resp.err)
if err != nil {
log.Debugf("Client.dispatch(): failed to encode the client response as a json rpc call. Error: %s\n", err.Error())
log.Debugf("Client.dispatch(): failed to encode the client response as a json rpc call. Error: %s", err.Error())

resp.errChan <- err
continue
}

err = conn.WriteMessage(websocket.BinaryMessage, msg)
if err != nil {
log.Debugf("Client.dispatch(): failed to send the response through the websocket connection. Error: %s\n", err.Error())
log.Debugf("Client.dispatch(): failed to send the response through the websocket connection. Error: %s", err.Error())

resp.errChan <- err
return
}

log.Debugf("Client.dispatch(): successfully send the response through the websocket connection. Request:{id: %s, result: %s, error: %s}\n", resp.id, resp.result, resp.err)
log.Debugf("Client.dispatch(): successfully send the response through the websocket connection. Request:{id: %s, result: %s, error: %s}", resp.id, resp.result, resp.err)
resp.errChan <- nil
case <-ctx.Done():
log.Debug("Client.dispatch(): the connection has been closed. Abort the process loop...\n")
log.Debug("Client.dispatch(): the connection has been closed. Abort the process loop...")

return
}
Expand All @@ -487,23 +487,23 @@ func (c *Client) consume(conn *websocket.Conn) {
// client errors would be sent if something bad happens with the websocket connection
for {
if conn == nil {
log.Debugf("Client.consume(): the connection is not active - %s:%s\n", c.socket, c.path)
log.Debugf("Client.consume(): the connection is not active - %s:%s", c.socket, c.path)

c.clientErr <- errNilConnection
break
}

_, msg, err := conn.ReadMessage()
if err != nil {
log.Debugf("Client.consume(): there is an exception while reading message from the websocket connection. Error: %s\n", err.Error())
log.Debugf("Client.consume(): there is an exception while reading message from the websocket connection. Error: %s", err.Error())

c.clientErr <- err
break
}

id, res, err := decodeClientResponse(msg)
if err != nil {
log.Debugf("Client.consume(): unexpected client response, possibly a request message. Passing on for request processing. Error: %s.\n", err.Error())
log.Debugf("Client.consume(): unexpected client response, possibly a request message. Passing on for request processing. Error: %s.", err.Error())

c.srvReqChan.send(msg)
continue
Expand Down
48 changes: 24 additions & 24 deletions resourcemanager/resource_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,33 +61,33 @@ func Run(config *resourcemanagerconfig.ResourceManagerConfig) {
log.Info("resourceManager.Run")

if config == nil {
log.Warning("ResourceManager: No config Provided\n")
log.Warning("ResourceManager: No config Provided")
return
}
if config.EdgeCoreSocketPath == "" {
log.Warning("ResourceManager: edge_core_socketpath not provided in config file. Using default socketpath '/tmp/edge.sock'\n")
log.Warning("ResourceManager: edge_core_socketpath not provided in config file. Using default socketpath '/tmp/edge.sock'")
config.EdgeCoreSocketPath = "/tmp/edge.sock"
}
if config.EdgeResources == nil {
log.Errorf("ResourceManager: No edge resources provided\n")
log.Errorf("ResourceManager: No edge resources provided")
return
}
if config.ConfigObjectID <= 0 {
log.Errorf("ResourceManager: lwm2m objectid not provided\n")
log.Errorf("ResourceManager: lwm2m objectid not provided")
return
}
var err error
gcdConfig = config
log.Infof("ResourceManager: connecting to edge-core\n")
log.Infof("ResourceManager: connecting to edge-core")

resourceManagementClient, err = connect(config.EdgeCoreSocketPath) //connect to edge-core
if err == nil {
defer resourceManagementClient.Close()
} else {
log.Errorf("ResourceManager: could not connect to edge-core\n")
log.Errorf("ResourceManager: could not connect to edge-core")
return
}
log.Infof("ResourceManager: successfully connected to edge-core\n")
log.Infof("ResourceManager: successfully connected to edge-core")
objectinstanceid := 0
for _, edgeResource := range config.EdgeResources {
err = addResource(config.ConfigObjectID, objectinstanceid, 1, 1, "string", edgeResource.Name)
Expand Down Expand Up @@ -115,7 +115,7 @@ func Run(config *resourcemanagerconfig.ResourceManagerConfig) {
}
writeResource(config.ConfigObjectID, objectinstanceid, 3, 3, "string", *b64Config)
} else {
log.Errorf("ResourceManager: could not read %s config file %s, err: %v\n", edgeResource.Name, edgeResource.ConfigFilePath, err.Error())
log.Errorf("ResourceManager: could not read %s config file %s, err: %v", edgeResource.Name, edgeResource.ConfigFilePath, err.Error())
}
}
objectinstanceid = objectinstanceid + 1
Expand All @@ -131,14 +131,14 @@ func updateResourceLoop() {
select {
case srvreq := <-srvreqchannel:
json.Unmarshal(srvreq, &c)
log.Debugf("ResourceManager: Got request from edge-core: %v\n", c)
log.Debugf("ResourceManager: Got request from edge-core: %v", c)
if c.Method == "write" {
params := c.Params.(map[string]interface{})
uri := params["uri"].(map[string]interface{})
objectinstanceid := 0
for _, edgeResource := range gcdConfig.EdgeResources {
if uri != nil && uri["objectId"].(float64) == float64(gcdConfig.ConfigObjectID) && uri["objectInstanceId"].(float64) == float64(objectinstanceid) && uri["resourceId"].(float64) == 3 {
log.Debugf("ResourceManager: Writing %s config file\n", edgeResource.Name)
log.Debugf("ResourceManager: Writing %s config file", edgeResource.Name)
if edgeResource.ConfigFilePath != "" && writeConfigFile(edgeResource.ConfigFilePath, params["value"].(string)) == nil {
okresult := json.RawMessage(`"ok"`)
resourceManagementClient.Respond(c.ID, &okresult, nil)
Expand All @@ -155,12 +155,12 @@ func updateResourceLoop() {
objectinstanceid = objectinstanceid + 1
}
} else {
log.Debugf("ResourceManager: Unhandled request: %v\n", c.Method)
log.Debugf("ResourceManager: Unhandled request: %v", c.Method)
}
}
}
} else {
log.Errorf("ResourceManager: Could not register request receiver: %s\n", err.Error())
log.Errorf("ResourceManager: Could not register request receiver: %s", err.Error())
}
}

Expand All @@ -177,7 +177,7 @@ func readConfigFile(filepath string) (*string, error) {
func writeConfigFile(filepath string, config string) error {
file, err := os.Create(filepath)
if err != nil {
log.Errorf("ResourceManager: could not create config file: %v\n", err)
log.Errorf("ResourceManager: could not create config file: %v", err)
return err
}

Expand All @@ -194,12 +194,12 @@ func connect(edgecorescoketpath string) (*Client, error) {
var res string
err := client.Call("gw_resource_manager_register", gwResourceManagerRegisterArgs{Name: "edge_resource_manager"}, &res)

log.Debugf("ResourceManager: gw_resource_manager_register response:%v\n", res)
log.Debugf("ResourceManager: gw_resource_manager_register response:%v", res)
if err != nil {
log.Errorf("ResourceManager: Failed to connect to edge-core %s\n", err.Error())
log.Errorf("ResourceManager: Failed to connect to edge-core %s", err.Error())
return nil, err
}
log.Infof("ResourceManager: Websocket connection established with edge-core\n")
log.Infof("ResourceManager: Websocket connection established with edge-core")

return client, nil
}
Expand Down Expand Up @@ -229,18 +229,18 @@ func addResource(objectid int, objectinstanceid int, resourceid int, operationsA
}

if resourceManagementClient == nil {
log.Errorf("ResourceManager: resourceManagementClient is nil\n")
log.Errorf("ResourceManager: resourceManagementClient is nil")
return errors.New("resourceManagementClient is nil")
}

err := resourceManagementClient.Call("add_resource", addResource, &res)

log.Debugf("ResourceManager: add_resource response:%v\n", res)
log.Debugf("ResourceManager: add_resource response:%v", res)
if err != nil {
log.Errorf("ResourceManager: Failed to add resource %s\n", err.Error())
log.Errorf("ResourceManager: Failed to add resource %s", err.Error())
return err
}
log.Infof("ResourceManager: Lwm2m resource %d/%d/%d added\n", objectid, objectinstanceid, resourceid)
log.Infof("ResourceManager: Lwm2m resource %d/%d/%d added", objectid, objectinstanceid, resourceid)
return nil

}
Expand Down Expand Up @@ -270,18 +270,18 @@ func writeResource(objectid int, objectinstanceid int, resourceid int, operation
}

if resourceManagementClient == nil {
log.Errorf("ResourceManager: resourceManagementClient is nil\n")
log.Errorf("ResourceManager: resourceManagementClient is nil")
return errors.New("resourceManagementClient is nil")
}

err := resourceManagementClient.Call("write_resource_value", writeResource, &res)

log.Debugf("ResourceManager: write response:%v\n", res)
log.Debugf("ResourceManager: write response:%v", res)
if err != nil {
log.Errorf("ResourceManager: Failed to write resource %s\n", err.Error())
log.Errorf("ResourceManager: Failed to write resource %s", err.Error())
return err
}
log.Infof("ResourceManager: Lwm2m resource %d/%d/%d added\n", objectid, objectinstanceid, resourceid)
log.Infof("ResourceManager: Lwm2m resource %d/%d/%d added", objectid, objectinstanceid, resourceid)
return nil

}

0 comments on commit 5b80fa4

Please sign in to comment.