var axon = require('axon')
var debug = require('debug')('micromono:rpc')


module.exports = {
  client: {
    send: function(data) {
      var self = this
      this.scheduleProvider(function(provider) {
        var socket = provider.socket
        var args = data.args
        var fn

        if ('function' === typeof args[args.length - 1]) {
          fn = args.pop()
          data.cid = true
        }

        var msg = self.encodeData(data)
        socket.send(msg, fn || function() {})
      })
    },

    connect: function(provider) {
      var endpoint = 'tcp://' + provider.address + ':' + provider.rpcPort
      var socket = axon.socket('req')

      var self = this
      socket.on('disconnect', function() {
        debug('axon provider %s disconnected', endpoint)
        self.onProviderDisconnect(provider)
      })

      provider.socket = socket
    }
  },

  server: {

    dispatch: function(msg, callback) {
      var data = this.decodeData(msg)
      var args = data.args || []
      var handler = this.getHandler(data.name)

      if (data.cid === true) {
        args.push(callback)
      }

      handler.apply(this, args)
    },

    startRPCServer: function(port) {
      var self = this
      var socket = axon.socket('rep')

      this.announcement.rpcPort = port
      this.announcement.rpcType = 'axon'

      var promise = new Promise(function(resolve, reject) {
        socket.bind(port, function() {
          debug('axon socket bind on port %s', port)
          resolve()
        })

        socket.on('message', function(msg, callback) {
          self.dispatch(msg, callback)
        })
      })

      return promise
    }
  }
}

@@ -72,48 +72,45 @@
 )%0A%0A%0A
-module.exports = %7B%0A  client: %7B%0A    
+exports.type = 'axon'%0A%0Aexports.
 send
-:
+ =
  fun
@@ -129,83 +129,44 @@
 %7B%0A  
-    var self = this%0A      this.scheduleProvider(function(provider) %7B%0A      
+var provider = this.scheduler.get()%0A
   va
@@ -192,22 +192,16 @@
 .socket%0A
-      
   var ar
@@ -215,22 +215,16 @@
 ta.args%0A
-      
   var fn
@@ -225,22 +225,16 @@
 var fn%0A%0A
-      
   if ('f
@@ -274,30 +274,24 @@
 gth - 1%5D) %7B%0A
-      
     fn = arg
@@ -302,22 +302,16 @@
 p()%0A    
-   
-   
 data.cid
@@ -324,63 +324,44 @@
 e%0A  
-      %7D%0A%0A        var msg = self.encodeData
+%7D%0A%0A  var msg = this.serialize
 (data)%0A
-      
   so
@@ -400,37 +400,28 @@
 %7B%7D)%0A
-      %7D)%0A    %7D,%0A%0A    
+%7D%0A%0Aexports.
 connect
-:
+ =
  fun
@@ -434,28 +434,24 @@
 provider) %7B%0A
-    
   var endpoi
@@ -508,20 +508,16 @@
 rpcPort%0A
-    
   var so
@@ -543,20 +543,16 @@
 'req')%0A%0A
-    
   var se
@@ -561,20 +561,16 @@
  = this%0A
-    
   socket
@@ -596,28 +596,24 @@
 unction() %7B%0A
-    
     debug('a
@@ -653,20 +653,16 @@
 dpoint)%0A
-    
     self
@@ -695,28 +695,20 @@
 ider)%0A  
-    %7D)%0A%0A    
+%7D)%0A%0A
   provid
@@ -730,319 +730,32 @@
 ket%0A
-    %7D%0A  %7D,%0A%0A  server: %7B%0A%0A    dispatch: function(msg, callback) %7B%0A      var data = this.decodeData(msg)%0A      var args = data.args %7C%7C %5B%5D%0A      var handler = this.getHandler(data.name)%0A%0A      if (data.cid === true) %7B%0A        args.push(callback)%0A      %7D%0A%0A      handler.apply(this, args)%0A    %7D,%0A%0A    startRPC
+%7D%0A%0Aexports.start
 Server
-:
+ =
  fun
@@ -764,24 +764,26 @@
 ion(port
+, host
 ) %7B%0A
-    
   var se
@@ -792,20 +792,16 @@
  = this%0A
-    
   var so
@@ -831,93 +831,8 @@
 ')%0A%0A
-      this.announcement.rpcPort = port%0A      this.announcement.rpcType = 'axon'%0A%0A    
   va
@@ -879,28 +879,24 @@
 , reject) %7B%0A
-    
     socket.b
@@ -904,16 +904,22 @@
 nd(port,
+ host,
  functio
@@ -930,20 +930,16 @@
 %7B%0A      
-    
 debug('a
@@ -979,20 +979,16 @@
 )%0A      
-    
 resolve(
@@ -993,28 +993,20 @@
 e()%0A    
-   
- 
 %7D)%0A%0A
-    
     sock
@@ -1054,20 +1054,16 @@
 %7B%0A      
-    
 self.dis
@@ -1091,29 +1091,17 @@
     
- 
-   
 %7D)%0A  
-    %7D)%0A%0A    
+%7D)%0A%0A
   re
@@ -1117,16 +1117,6 @@
 ise%0A
-    %7D%0A  %7D%0A
 %7D%0A

Update for new rpc and scheduler.