 status-message = require './status-message'
 
 class FileHandler
-    settings: {}
-
     constructor: (session) ->
         @session = session
+        @settings = {}
+        @readbytes = 0
+        @closed = false
+
+    set: (index, value) ->
+        @settings[index] = value
+        if index == "display-name"
+            @remote_address = value.split(":")[0]
+            @basename = path.basename(value.split(":")[1])
+        else if index == "data"
+            @datasize = parseInt(value,10)
+
+    get: (index) ->
+        return @settings[index]
 
     create: ->
         @tempfile = path.join(os.tmpdir(), randomstring(10), @basename)


    save: ->
        if not @session.alive
            console.log "[ratom] Error saving #{path.basename @tempfile} to #{@remoteAddress}"
            status-message.display "Error saving #{path.basename @tempfile} to #{@remoteAddress}", 2000
            return
        console.log "[ratom] saving #{path.basename @tempfile} to #{@remoteAddress}"
        status-message.display "Saving #{path.basename @tempfile} to #{@remoteAddress}", 2000
        @session.send "save"
        @session.send "token: #{@token}"
        data = fs.readFileSync(@tempfile)
        @session.send "data: " + Buffer.byteLength(data)
        @session.send data

    close: ->
        console.log "[ratom] closing #{path.basename @tempfile}"
        # try to close session
        @session.close()
        @subscriptions.dispose()

class Session
    should_parse_data: false
    readbytes: 0
    nconn: 0

    constructor: (socket) ->
        @socket = socket
        @send "Atom "+ atom.getVersion()
        @alive = true

                status-message.display "Starting remote atom server", 2000

        @server = net.createServer (socket) ->
            console.log "[ratom] received connection from #{socket.remote_address}"
            session = new Session(socket)

        port = atom.config.get "remote-atom.port"
