   index += writeCmd buffer, index, cmdBuf
   return index
 
-cmdStreamBuildGraph = (currentNodeId, buffer, index, componentLib, graph) ->
-  messages = protocol.graphToFbpMessages graph, 'default'
-
-  nodeMap = graph.nodeMap
+clearGraph = (payload, buffer, index) ->
+  # Clear existing graph
+  index += writeCmd(buffer, index, cmdFormat.commands.Reset.id)
+  return index
+
+startNetwork = (payload, buffer, index) ->
+  index += writeCmd(buffer, index, cmdFormat.commands.StartNetwork.id)
+  return index
+
+# The following are MicroFlo specific, not part of FBP runtime protocol
+configureDebug = (payload, buffer, index) ->
+  debugLevel = payload.level
+  index += writeCmd(buffer, index, cmdFormat.commands.ConfigureDebug.id, cmdFormat.debugLevels[debugLevel].id)
+  return index
+
+openCommunication = (payload, buffer, index) ->
+  index += writeString(buffer, index, cmdFormat.magicString)
+  return index
+
+closeCommunication = (payload, buffer, index) ->
+  console.log 'b', buffer, index
+  index += writeCmd(buffer, index, cmdFormat.commands.End.id)
+  return index
+
+# TODO: implement the inverse, getting a FBP protocol message from CS
+toCommandStreamBuffer = (message, componentLib, nodeMap, componentMap, buffer, index) ->
+  console.log 'm', message.command, index
+  if message.protocol == 'graph'
+    # TODO: also support removenode/removeedge/removeinitial
+    if message.command == 'clear'
+      index = clearGraph message.payload, buffer, index
+    else if message.command == 'addnode'
+      index = addNode message.payload, componentLib, buffer, index
+    else if message.command == 'addedge'
+      index = addEdge message.payload, componentLib, nodeMap, componentMap, buffer, index
+    else if message.command == 'addinitial'
+      index = addInitial message.payload, componentLib, nodeMap, componentMap, buffer, index
+    else
+      throw new Error "Unknown FBP runtime graph command #{message.command}" 
+
+  else if message.protocol == 'network'
+    if message.command == 'start'
+      index = startNetwork message.payload, buffer, index
+    else
+      throw new Error "Unknown FBP runtime network command #{message.command}"
+
+  else if message.protocol == 'microflo'
+    if message.command == 'opencommunication'
+      index = openCommunication message.payload, buffer, index
+    else if message.command == 'closecommunication'
+      index = closeCommunication message.payload, buffer, index
+    else if message.command == 'configuredebug'
+      index = configureDebug message.payload, buffer, index
+    else
+      throw new Error "Unknown FBP runtime microflo command #{message.command}"
+  else    
+      throw new Error "Unknown FBP runtime sub-protocol #{message.protocol}"
+
+  return index
+
+# As a list of FBP runtime messages
+initialGraphMessages = (graph, graphName, debugLevel, openclose) ->
+  messages = []
+
+  # Start comm
+  if openclose
+    messages.push
+      protocol: 'microflo'
+      command: 'opencommunication'
+      payload: null
+
+  # Clear graph
+  messages.push
+    protocol: 'graph'
+    command: 'clear'
+    payload:
+      id: graphName
+
+  # Config
+  messages.push
+    protocol: 'microflo'
+    command: 'configuredebug'
+    payload:
+      level: debugLevel
+
+  # Actual graph
+  graphMessages = protocol.graphToFbpMessages graph, 'default'
+  messages = messages.concat graphMessages
+
+  # Start the network
+  messages.push
+    protocol: 'network'
+    command: 'start'
+    payload: null
+
+  # Stop comm
+  if openclose
+    messages.push
+      protocol: 'microflo'
+      command: 'closecommunication'
+      payload: null
+
+  return messages
+
+cmdStreamBuildGraph = (messages, currentNodeId, buffer, index, componentLib, nodeMap, componentMap) ->
   startIndex = index
 
-  componentMap = {}
+  # Build component/node mapping
   for message in messages
     if message.command == 'addnode'
       name = message.payload.id

  r = if parent != undefined then graph.processes[nodeNameById(graph.nodeMap, parent)].graph.processes else graph.processes
  r[nodeName]

parseReceivedCmd = (componentLib, graph, cmdData, handler) ->
  `var targetPort`
  `var targetNode`

    port: port
  }

# TODO: move all these down into "protocol", along with the inverse functions
addNode = (payload, componentLib, buffer, index) ->
  nodeName = payload.id
  componentName = payload.component
