Skip to content

Commit

Permalink
Let ports know their name and the nodename for easier debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
bergie committed Sep 27, 2013
1 parent f7b3232 commit 93f5cdd
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
NoFlo ChangeLog
===============

## 0.4.2 (git master)

* Easier debugging: port errors now contain the name of the NoFlo graph node and the port

## 0.4.1 (September 25th 2013)

* NoFlo components can now implement a `shutdown` method which is called when they're removed from a network
Expand Down
18 changes: 18 additions & 0 deletions spec/Network.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ describe 'NoFlo Network', ->
chai.expect(n.processes.Merge).to.be.an 'Object'
chai.expect(n.processes.Callback).to.exist
chai.expect(n.processes.Callback).to.be.an 'Object'
it 'the ports of the processes should know the node names', ->
for name, port of n.processes.Callback.component.inPorts
chai.expect(port.name).to.equal name
chai.expect(port.node).to.equal 'Callback'
chai.expect(port.getId()).to.equal "Callback #{name.toUpperCase()}"
for name, port of n.processes.Callback.component.outPorts
chai.expect(port.name).to.equal name
chai.expect(port.node).to.equal 'Callback'
chai.expect(port.getId()).to.equal "Callback #{name.toUpperCase()}"

it 'should contain one connection', ->
chai.expect(n.connections).to.not.be.empty
Expand All @@ -129,6 +138,15 @@ describe 'NoFlo Network', ->
g.renameNode 'Callback', 'Func'
it 'shouldn\'t have the process in the old location', ->
chai.expect(n.processes.Callback).to.be.undefined
it 'should have informed the ports of their new node name', ->
for name, port of n.processes.Func.component.inPorts
chai.expect(port.name).to.equal name
chai.expect(port.node).to.equal 'Func'
chai.expect(port.getId()).to.equal "Func #{name.toUpperCase()}"
for name, port of n.processes.Func.component.outPorts
chai.expect(port.name).to.equal name
chai.expect(port.node).to.equal 'Func'
chai.expect(port.getId()).to.equal "Func #{name.toUpperCase()}"

describe "Nodes are added first, then edges, then initializers (i.e. IIPs), and in order of definition order within each", ->
g = null
Expand Down
19 changes: 16 additions & 3 deletions spec/Port.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,22 @@ describe 'Untyped port instance', ->

describe 'Port instance', ->
p = null
it 'should retain the given type', ->
p = new port.Port 'string'
chai.expect(p.type).to.equal 'string'
describe 'initially', ->
it 'should retain the given type', ->
p = new port.Port 'string'
chai.expect(p.type).to.equal 'string'
it 'should not have a name', ->
chai.expect(p.name).to.be.a 'null'
it 'should not have a node name', ->
chai.expect(p.node).to.be.a 'null'
it 'should return "Port" as identifier', ->
chai.expect(p.getId()).to.equal 'Port'

describe 'with given name and node', ->
it 'should return correct ID', ->
p.name = 'out'
p.node = 'Foo'
chai.expect(p.getId()).to.equal 'Foo OUT'

describe 'without attached socket', ->
it 'should not be attached initially', ->
Expand Down
18 changes: 9 additions & 9 deletions src/lib/ArrayPort.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ class ArrayPort extends port.Port
connect: (socketId = null) ->
if socketId is null
unless @sockets.length
throw new Error "No sockets available"
throw new Error "#{@getId()}: No connections available"
@sockets.forEach (socket) ->
socket.connect()
return

unless @sockets[socketId]
throw new Error "No socket '#{socketId}' available"
throw new Error "#{@getId()}: No connection '#{socketId}' available"

@sockets[socketId].connect()

beginGroup: (group, socketId = null) ->
if socketId is null
unless @sockets.length
throw new Error "No sockets available"
throw new Error "#{@getId()}: No connections available"
@sockets.forEach (socket, index) =>
@beginGroup group, index
return

unless @sockets[socketId]
throw new Error "No socket '#{socketId}' available"
throw new Error "#{@getId()}: No connection '#{socketId}' available"

return @sockets[socketId].beginGroup group if @isConnected socketId

Expand All @@ -49,13 +49,13 @@ class ArrayPort extends port.Port
send: (data, socketId = null) ->
if socketId is null
unless @sockets.length
throw new Error "No sockets available"
throw new Error "#{@getId()}: No connections available"
@sockets.forEach (socket, index) =>
@send data, index
return

unless @sockets[socketId]
throw new Error "No socket '#{socketId}' available"
throw new Error "#{@getId()}: No connection '#{socketId}' available"

return @sockets[socketId].send data if @isConnected socketId

Expand All @@ -66,20 +66,20 @@ class ArrayPort extends port.Port
endGroup: (socketId = null) ->
if socketId is null
unless @sockets.length
throw new Error "No sockets available"
throw new Error "#{@getId()}: No connections available"
@sockets.forEach (socket, index) =>
@endGroup index
return

unless @sockets[socketId]
throw new Error "No socket '#{socketId}' available"
throw new Error "#{@getId()}: No connection '#{socketId}' available"

do @sockets[socketId].endGroup

disconnect: (socketId = null) ->
if socketId is null
unless @sockets.length
throw new Error "No sockets available"
throw new Error "#{@getId()}: No connections available"
for socket in @sockets
socket.disconnect()
return
Expand Down
17 changes: 17 additions & 0 deletions src/lib/Network.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ class Network extends EventEmitter
instance.nodeId = node.id
process.component = instance

# Inform the ports of the node name
for name, port of process.component.inPorts
port.node = node.id
port.name = name
for name, port of process.component.outPorts
port.node = node.id
port.name = name

@subscribeSubgraph node.id, instance if instance.isSubgraph()

# Store and return the process instance
Expand All @@ -167,7 +175,16 @@ class Network extends EventEmitter
renameNode: (oldId, newId) ->
process = @getNode oldId
return unless process

# Inform the process of its ID
process.id = newId

# Inform the ports of the node name
for name, port of process.component.inPorts
port.node = newId
for name, port of process.component.outPorts
port.node = newId

@processes[newId] = process
delete @processes[oldId]

Expand Down
19 changes: 13 additions & 6 deletions src/lib/Port.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ class Port extends EventEmitter
@type = 'all' unless @type
@socket = null
@from = null
@node = null
@name = null

getId: ->
unless @node and @name
return 'Port'
"#{@node} #{@name.toUpperCase()}"

attach: (socket) ->
throw new Error "#{@name}: Socket already attached #{@socket.getId()} - #{socket.getId()}" if @isAttached()
throw new Error "#{@getId()}: Socket already attached #{@socket.getId()} - #{socket.getId()}" if @isAttached()
@socket = socket

@attachSocket socket
Expand All @@ -38,11 +45,11 @@ class Port extends EventEmitter
@emit "disconnect", socket, localId

connect: ->
throw new Error "No connection available" unless @socket
throw new Error "#{@getId()}: No connection available" unless @socket
do @socket.connect

beginGroup: (group) ->
throw new Error "No connection available" unless @socket
throw new Error "#{@getId()}: No connection available" unless @socket

return @socket.beginGroup group if @isConnected()

Expand All @@ -51,7 +58,7 @@ class Port extends EventEmitter
do @socket.connect

send: (data) ->
throw new Error "No connection available" unless @socket
throw new Error "#{@getId()}: No connection available" unless @socket

return @socket.send data if @isConnected()

Expand All @@ -60,11 +67,11 @@ class Port extends EventEmitter
do @socket.connect

endGroup: ->
throw new Error "No connection available" unless @socket
throw new Error "#{@getId()}: No connection available" unless @socket
do @socket.endGroup

disconnect: ->
throw new Error "No connection available" unless @socket
throw new Error "#{@getId()}: No connection available" unless @socket
@socket.disconnect()

detach: (socket) ->
Expand Down

0 comments on commit 93f5cdd

Please sign in to comment.