/**
  * Handle incoming data
  *
  * @param   peer.Socket socket
  * @param   peer.server.ServerProtocol protocol
  * @param   int type
  * @param   string data
  */
 public function handle($socket, $protocol, $type, $data)
 {
     try {
         $handler = EascMessageFactory::forType($type);
         $handler->handle($protocol, $data);
         $response = EascMessageFactory::forType(REMOTE_MSG_VALUE);
         $response->setValue($handler->getValue());
     } catch (Throwable $e) {
         $response = EascMessageFactory::forType(REMOTE_MSG_EXCEPTION);
         $response->setValue($e);
     }
     $protocol->answerWithMessage($socket, $response);
 }
    /**
     * Start server
     *
     * @param   string[] args
     */
    public static function main(array $args)
    {
        // Add shutdown message handler
        EascMessageFactory::setHandler(61, newinstance('remote.server.message.EascMessage', array(), '{
        public function getType() { 
          return 61; 
        }
        public function handle($protocol, $data) {
          Logger::getInstance()->getCategory()->debug("Shutting down");
          $protocol->server->terminate= TRUE; 
        }
      }')->getClass());
        $s = new Server('127.0.0.1', 0);
        try {
            $protocol = new EascProtocol(newinstance('remote.server.deploy.scan.DeploymentScanner', array(), '{
          private $changed= TRUE;

          public function scanDeployments() {
            $changed= $this->changed;
            $this->changed= FALSE;
            return $changed;
          }

          public function getDeployments() {
            $res= "net/xp_framework/unittest/remote/deploy/beans.test.CalculatorBean.xar";

            with ($d= new Deployment($res)); {
              $d->setClassLoader(new ArchiveClassLoader(new Archive(ClassLoader::getDefault()->getResourceAsStream($res))));
              $d->setImplementation("beans.test.CalculatorBeanImpl");
              $d->setInterface("beans.test.Calculator");
              $d->setDirectoryName("xp/test/Calculator");

              return array($d);
            }
          }
        }'));
            $protocol->initialize();
            $s->setProtocol($protocol);
            $s->init();
            Console::writeLinef('+ Service %s:%d', $s->socket->host, $s->socket->port);
            $s->service();
            Console::writeLine('+ Done');
        } catch (Throwable $e) {
            Console::writeLine('- ', $e->getMessage());
        }
    }