core/implementation/Connection.tcl


@implementation Connection {
  - init {} {
      $super init
      set port 4000
      set host localhost
      set encrypted 1
    }

  - host: anHost {
      set host $anHost
    }

  - host {} {
      return $host
    }

  - port: aPort {
      set port $aPort
    }

  - port {} {
      return $port
    }

  - socket {} {
      return $socket
    }

  - isEncrypted {} {
      return $encrypted
    }

  - enableEncryption {} {
      set encrypted 1
    }

  - disableEncryption {} {
      set encrypted 0
    }

  - send: msg {
      if {$encrypted} {
        msg encrypt
      }
      puts $socket $msg
      flush $socket
    }
  - receive {} {
      set msg [gets $socket]
      if {$encrypted} {
        msg decrypt
      }
      return $msg
    }

  - remoteInvocation: { anInvocation } {
      set cmd {@RMI }
      append cmd $anInvocation
      if [$self isAlive] {
        $self send: $cmd
        return [$self receive]
      } else {
        return
      }
    }

  - open {} {
      if [catch {set socket [socket $host $port]} err] {
        return -code error "couldn't connect to $host at $port ($err)"
      } else {
        fconfigure $socket -buffering line -blocking yes
      }
    }

  - close {} {
      catch {close $socket}
      set socket {}
    }

  - isAlive {} {
      if {![string length $socket]} { return 0 }
      if [eof $socket] {
        $self close
        return 0
      } else {
        return 1
      }
    }

  - dealloc {} {
      $self close 
      return [$super dealloc]
    }
}