Browse files

Working on server model and test class (not working yet :-p)

  • Loading branch information...
1 parent 33b3b60 commit 7c6c2b06f93a7a84982a057a405b9b82aad1dfd0 @samholman committed Aug 6, 2011
View
9 application/Bootstrap.php
@@ -2,7 +2,10 @@
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
-
-
+ protected function _initConfig()
+ {
+ $config = new Zend_Config($this->getOptions(), true);
+ Zend_Registry::set('config', $config);
+ return $config;
+ }
}
-
View
3 application/configs/application.ini
@@ -8,6 +8,9 @@ appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
+minecraft.serverPath = "/opt/local/minecraft_server.jar"
+minecraft.worldPath = "/opt/local/minecraft"
+
[staging : production]
[testing : production]
View
77 application/models/Server.php
@@ -1,8 +1,79 @@
<?php
+/**
+ * The server model handles starting and stopping the server, etc
+ */
class Application_Model_Server
{
-
-
+ private
+ $_pid,
+ $_process,
+ $_pipes,
+ $_descriptorSpec = array(
+ array('pipe', 'r'),
+ array('pipe', 'w'),
+ array('pipe', 'r'),
+ );
+
+ /**
+ * Returns whether or not the Minecraft server is running
+ *
+ * @return bool
+ */
+ public function isRunning()
+ {
+ return is_resource($this->_process);
+ }
+
+ /**
+ * Starts the Minecraft server, returns true on success
+ *
+ * @return bool
+ */
+ public function start()
+ {
+ $jarPath = Zend_Registry::get('config')->get('minecraft')->get('serverPath');
+ $worldPath = Zend_Registry::get('config')->get('minecraft')->get('worldPath');
+
+ $this->_process = proc_open('java -Xmx1024M -Xms1024M -jar ' . $jarPath . ' ', $this->_descriptorSpec, $this->_pipes, $worldPath);
+
+ if ($this->isRunning())
+ {
+ stream_set_blocking($this->_pipes[0], false);
+ stream_set_blocking($this->_pipes[1], false);
+ stream_set_blocking($this->_pipes[2], false);
+ fwrite($this->_pipes[0], 'list');
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Stop the minecraft server process
+ *
+ * @return int
+ */
+ public function stop()
+ {
+ fclose($this->_pipes[0]);
+ fclose($this->_pipes[1]);
+ fclose($this->_pipes[2]);
+
+ //$status = proc_get_status($this->_process);
+ //exec('kill ' . $status['pid']);
+ return proc_close($this->_process);
+ }
+
+ /**
+ * Returns the server output
+ *
+ * @return string
+ */
+ public function getOutput()
+ {
+ if ($this->isRunning()) {
+ return stream_get_contents($this->_pipes[2]);
+ }
+ }
}
-
View
12 tests/application/models/ServerModelTest.php
@@ -4,12 +4,20 @@ class ServerModelTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
- $this->bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
+ $this->application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
+ $this->application->bootstrap();
parent::setUp();
}
- public function testStart()
+ public function testStartStop()
{
$server = new Application_Model_Server();
+ $this->assertFalse($server->isRunning());
+
+ $this->assertTrue($server->start());
+ var_dump($server->getOutput());
+
+ $this->assertTrue($server->isRunning());
+ $server->stop();
}
}

0 comments on commit 7c6c2b0

Please sign in to comment.