Skip to content

Commit

Permalink
Merge pull request ciao-project#205 from 01org/sameo/issues/19
Browse files Browse the repository at this point in the history
ssntp: Add SSNTP Frame Origin field
  • Loading branch information
Samuel Ortiz committed Jun 6, 2016
2 parents 07a2185 + 36298c5 commit ba9dfd9
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 10 deletions.
2 changes: 0 additions & 2 deletions ssntp/example_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ func (server *ssntpDumpServer) ErrorNotify(uuid string, error Error, frame *Fram
fmt.Printf("%s: ERROR (%s) from %s\n", server.name, error, uuid)
}

const agentUUID = "3390740c-dce9-48d6-b83a-a717417072ce"

func (server *ssntpDumpServer) CommandForward(uuid string, command Command, frame *Frame) (dest ForwardDestination) {
dest.AddRecipient(agentUUID)

Expand Down
23 changes: 15 additions & 8 deletions ssntp/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,17 @@ type FrameTrace struct {

// Frame represents an SSNTP frame structure.
type Frame struct {
Major uint8
Minor uint8
Type Type
Operand uint8
Major uint8
Minor uint8
Type Type
Operand uint8

// Origin is the frame first sender and creator UUID.
// When a SSNTP frame is forwarded by a server, the client
// then only sees a new frame coming but it can not tell
// who the frame creator and first sender is. This method
// allows to fetch such information from a frame.
Origin uuid.UUID
PayloadLength uint32
Trace *FrameTrace
Payload []byte
Expand Down Expand Up @@ -158,12 +165,12 @@ func (f Frame) String() string {
path = path + fmt.Sprintf("\n\t\tNode #%d\n\t\tUUID %s\n", i, node) + ts
}

return fmt.Sprintf("\n\tMajor %d\n\tMinor %d\n\tType %s\n\tOp %s\n\tPayload len %d\n\tPath %s\n",
f.major(), f.Minor, t, op, f.PayloadLength, path)
return fmt.Sprintf("\n\tMajor %d\n\tMinor %d\n\tType %s\n\tOp %s\n\tOrigin %s\n\tPayload len %d\n\tPath %s\n",
f.major(), f.Minor, t, op, f.Origin, f.PayloadLength, path)
}

return fmt.Sprintf("\n\tMajor %d\n\tMinor %d\n\tType %s\n\tOp %s\n\tPayload len %d\n",
f.major(), f.Minor, t, op, f.PayloadLength)
return fmt.Sprintf("\n\tMajor %d\n\tMinor %d\n\tType %s\n\tOp %s\n\tOrigin %s\n\tPayload len %d\n",
f.major(), f.Minor, t, op, f.Origin, f.PayloadLength)
}

func (f ConnectFrame) String() string {
Expand Down
4 changes: 4 additions & 0 deletions ssntp/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func (session *session) commandFrame(cmd Command, payload []byte, trace *TraceCo
Minor: minor,
Type: COMMAND,
Operand: byte(cmd),
Origin: session.src,
PayloadLength: (uint32)(len(payload)),
Payload: payload,
}
Expand All @@ -127,6 +128,7 @@ func (session *session) statusFrame(status Status, payload []byte, trace *TraceC
Minor: minor,
Type: STATUS,
Operand: byte(status),
Origin: session.src,
PayloadLength: (uint32)(len(payload)),
Payload: payload,
}
Expand All @@ -143,6 +145,7 @@ func (session *session) eventFrame(event Event, payload []byte, trace *TraceConf
Minor: minor,
Type: EVENT,
Operand: byte(event),
Origin: session.src,
PayloadLength: (uint32)(len(payload)),
Payload: payload,
}
Expand All @@ -159,6 +162,7 @@ func (session *session) errorFrame(error Error, payload []byte, trace *TraceConf
Minor: minor,
Type: ERROR,
Operand: byte(error),
Origin: session.src,
PayloadLength: (uint32)(len(payload)),
Payload: payload,
}
Expand Down
81 changes: 81 additions & 0 deletions ssntp/ssntp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ type ssntpClient struct {
staChannel chan string
evtChannel chan string
errChannel chan string
uuidChannel chan string

cmdTracedChannel chan string
cmdDurationChannel chan time.Duration
Expand Down Expand Up @@ -221,6 +222,11 @@ func (client *ssntpClient) CommandNotify(command Command, frame *Frame) {
client.cmdChannel <- command.String()
}

if client.uuidChannel != nil {
uuid := frame.Origin
client.uuidChannel <- uuid.String()
}

if client.cmdDumpChannel != nil {
trace, err := frame.DumpTrace()
if err == nil && trace.Type == COMMAND.String() {
Expand Down Expand Up @@ -1907,6 +1913,81 @@ func TestCmdFwd(t *testing.T) {
}

const controllerUUID = "3390740c-dce9-48d6-b83a-a717417072ce"
const agentUUID = "4481631c-dce9-48d6-b83a-a717417072ce"

// Test SSNTP Origin UUID
//
// Start an SSNTP server with a set of forwarding rules, an SSNTP
// agent and an SSNTP Controller.
// Then verify that the Controller receives a frame which Origin
// field is the agent UUID.
//
// Test is expected to pass.
func TestOriginUUID(t *testing.T) {
var server ssntpServer
var controller, agent ssntpClient
command := CONFIGURE

server.t = t
serverConfig, err := buildTestConfig(SCHEDULER)
if err != nil {
t.Fatalf("Could not build a test config")
}

serverConfig.ForwardRules = []FrameForwardRule{
{
Operand: command,
Dest: Controller,
},
}

controller.t = t
controller.uuidChannel = make(chan string)
controllerConfig, err := buildTestConfig(Controller)
if err != nil {
t.Fatalf("Could not build a test config")
}

agent.t = t
agentConfig, err := buildTestConfig(AGENT)
if err != nil {
t.Fatalf("Could not build a test config")
}
agentConfig.UUID = agentUUID

err = server.ssntp.ServeThreadSync(serverConfig, &server)
if err != nil {
t.Fatalf("%s", err)
}

err = controller.ssntp.Dial(controllerConfig, &controller)
if err != nil {
t.Fatalf("Controller failed to connect")
}

err = agent.ssntp.Dial(agentConfig, &agent)
if err != nil {
t.Fatalf("Agent failed to connect")
}

payload := []byte{'C', 'O', 'N', 'F', 'I', 'G'}
controller.payload = payload
agent.payload = payload
agent.ssntp.SendCommand(command, agent.payload)

select {
case uuid := <-controller.uuidChannel:
if uuid != agentUUID {
t.Fatalf("Did not receive the right origin UUID %s vs %s", uuid, agentUUID)
}
case <-time.After(time.Second):
t.Fatalf("Did not receive the uuid notification")
}

agent.ssntp.Close()
controller.ssntp.Close()
server.ssntp.Stop()
}

// Test SSNTP Command forwarder implementation
//
Expand Down

0 comments on commit ba9dfd9

Please sign in to comment.