ProjectDarkStar服务器端开发文档(二)Hello World

转自:http://www.david0446.com/?p=45

第一课:Hello World!

It is traditional in any programming tutorial for the first example to be a simple program that prints “Hello World” to the console. This lets a programmer see the plumbing required to start even a basic application before diving into real-world application logic.

       在任何的程序指南中,第一个简单的例子便在控制台打印出“Hello World”已经是个传统。这让程序员了解到在真正应用逻辑启动之前,开始一个如此简单的应用有哪些要求。

For simplicity, this lesson and the two that follow it print their output on the server console, rather than starting off with client-server networking. Even when a client is connected, server-side log messages are invaluable for debugging and monitoring the application.

       为了简单起见,这节课和下面两节课只将输出打印在服务器控制台上,并不开始客户端-服务器之间的网络工作。当有客户端连接上了,服务器端的日志信息对于调试和监控应用也是无用的。

1.  编写Hello World

All PDS applications start with an AppListener. An AppListener is the object that handles an application’s startup and client login events. An application’s AppListener is simply a class that implements the AppListener interface. An AppListener must implement the Serializable marker interface as well.

       所有的PDS应用都开始于一个AppListener一个AppListener是一个用来处理应用程序启动和客户端登录事件的对象。一个应用程序的AppListener是一个实现了AppListener接口的简单类。一个AppListener也必须实现Serializable接口。

As mentioned above, AppListener contains two methods: initialize and loggedIn. The initialize method gets called on the startup of the application if and only if the Object Store for this application is empty. The AppListener is automatically created in the Object Store by the system the first time the application is started up; in practice, this means that it is created once per application, unless the Object Store for this application is deleted and the system is returned to its pristine “never having run this application” state. (5)

A “Hello World” AppListener looks like this:

       就像上面提到的,AppListener包含两个方法:initialize loggedIn。在启动应用时,当且仅当该用的对象存储器(Object Store)为空时,initialize方法才会被调用。AppListener会在第一次启动应用程序时,被系统自动在对象存储器里创建。这意味着,每个应用只创建一次,除非这个应用的对象存储器被删除了,系统恢复到它的原始状态“从来没有运行过这个应用程序”。(5)

       一个“Hello World”的AppListener就像这样:

 

/*
 * Copyright 2007-2009 Sun Microsystems, Inc.
 *
 * This file is part of Project Darkstar Server.
 *
 * Project Darkstar Server is free software: you can redistribute it
 * and/or modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation and
 * distributed hereunder to you.
 *
 * Project Darkstar Server is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see &lt;<a href="
http://www.gnu.org/licenses/">http://www.gnu.org/licenses/</a>&gt;.
 
*/
package com.sun.sgs.tutorial.server.lesson1;

import java.io.Serializable;
import java.util.Properties;
import com.sun.sgs.app.AppListener;
import com.sun.sgs.app.ClientSession;
import com.sun.sgs.app.ClientSessionListener;

/**
 * Hello World example for the Project Darkstar Server. Prints <a href="mailto:
 * {
@code">{@code</a> "Hello World!"} to the console the first time it is
 * started.
 
*/
public class HelloWorld implements AppListener, // to get called during
                                                
// application startup.
        Serializable // since all AppListeners are ManagedObjects.
{
    
/** The version of the serialized form of this class. */
    
private static final long serialVersionUID = 1L;

    
/**
     * <a href="mailto:{
@inheritDoc">{@inheritDoc</a>} &lt;p&gt; Prints our
     * well-known greeting during application startup.
     
*/
    
public void initialize(Properties props) {
        System.out.println(
"Hello World!");
    }

    
/**
     * <a href="mailto:{
@inheritDoc">{@inheritDoc</a>} &lt;p&gt; Prevents client
     * logins by returning <a href="mailto:{
@code">{@code</a> null}.
     
*/
    
public ClientSessionListener loggedIn(ClientSession session) {
        
return null;
    }
}

 

2.  运行Hello World

To run HelloWorld you need the following:

● The Project Darkstar Server installed on your system.

● A JDK™ 6 installation. The version can be found with:

java -version

If java is not in your default execution path, you will need to set JAVA_HOME to point to the root of its installation on your system.

       要运行HelloWorld需要做一下事情:

  ● 安装Project Darkstar Server环境。

  ● 安装JDK6。可以用下面的命令查看版本:

          java -version

如果java不在你默认的执行路径,你需要在系统里设置JAVA_HOME环境变量。

Path Conventions 路径约定 

Unix and many Unix-derived systems use a forward slash (/) to show subdirectories in a file path.Win32, however, uses a backslash (\) for this purpose. Throughout this document we use the Unix convention for file paths unless it is in a Windows-specific example.

       Unix和许多延伸系统用斜杠(/)表示文件路径分隔符,然而,Win32用反斜杠(\)来表示。这篇文档我们用Unix     表示方法,除非在特殊的Windows的例子里。

Please remember that you may have to substitute backslashes for forward slashes in the generic examples if you are working in Windows.

       请记住,如果你用的是Windows系统,请用反斜杠代替斜杠。

The PDS server is started by using the sgs-boot.jar executable jar file in the bin folder of the PDS installation on your computer. It requires a configuration file in order to configure the classpath and JVM environment that will be used to boot up the server. By default, this configuration is provided by the sgs-boot.properties file in the conf folder. However, if a filename is given on the command line, it will be used as the configuration file instead. Booting up the PDS server, therefore, is done in the following manner (assuming the current working directory is the root directory of the PDS installation):

       PDS服务器的启动需要用到sgs-boot.jar的执行jar包,它在你PDS安装目录下的bin目录中。它需要一个配置文件,用来配置JVM的classpath,在启动服务时使用。默认情况下,这个配置在conf目录下的sgs-boot.properties文件里进行设置。然而,当在命令行中给出一个文件名,它就会作为配置文件使用。所以,启动PDS服务要按照以下方式(假设当前目录是PDS安装的根目录):

                                 java -jar bin/sgs-boot.jar [boot_config_file]

Where boot_config_file is an optional configuration file. If you are interested, you may examine the sgs-boot. properties file in the conf folder to see what configuration options are available.

       那个boot_config_file位置就是配置文件的名称。如果你感兴趣,你可以到conf目录下的sgs-boot. Properties看看什么配置是有效的。

In addition to the boot environment properties, a PDS application also needs a set of application properties that are to be passed to the initialize method of the AppListener. These properties are combined from the following sources. If a property appears in more than one source, the value is taken from the source higher on the list below:

1. Properties specified on the command line with the -D flag.

2. The properties file with the filename .sgs.properties in the user’s home directory.

3. The properties file specified by name with the SGS_DEPLOY property in the boot_config_file.

4. The properties file with the resource name META-INF/app.properties. This file is typically included in the META-INF directory of one of the application jars copied into the deploy folder of the PDS installation.

       除了启动环境属性之外,一个PDS应用程序也需要一系列通过AppListener中的initialize方法传递来的程序属性。这些属性由以下的来源组合。如果一个属性有一个以上的来源,它的值就取以下列表中处于更高位的来源。

  1. 在命令行中用-D标识指定的属性
  2. 属性文件在用户根目录下,并以.sgs.properties结尾命名。
  3. boot_config_file文件中,以SGS_DEPLOY属性名指定的属性文件。
  4. 资源名为META-INF/app.properties的属性文件。这个文件一般都包含在一个应用程序jar包中的META-INF目录里,这个jar包是复制到PDS发布目录里的。

We have provided a tutorial.jar file in the tutorial folder that contains pre-compiled .class files for all the tutorial examples. Additionally, we have included default configuration files for each of the tutorial examples. These files are located in the tutorial/conf folder of the PDS installation. In order to run HelloWorld as it is

shipped to you in the tutorial directories, do the following:

1. Open a Unix shell, a Windows command window, or whatever you do to get a command line on your development system.

2. Change your working directory to the directory of your PDS installation. In Unix, the command might be something like this:

cd ~/sgs-server-VERSION

3. Type the following: java -jar bin/sgs-boot.jar tutorial/conf/HelloWorld.boot

       我们已经提供了一个tutorial.jar文件在tutorial目录下,它包含了指南中所有例子的编译好的.class文件。另外,我们也为每个例子都装配了默认的配置文件。这些文件在PDS安装路径下的tutorial/conf目录中。为了运行tutorial目录下的HelloWorld程序,我们做如下操作:

  1. 打开一个Unix shell,或是一个Windows命令窗口,或者你的开发系统中任何一个命令行。
  2. 将工作目录改为PDS的安装目录。在Unix中,类似下面的命令:

                                     cd ~/sgs-server-VERSION

  1. 按以下的例子:

                                     java -jar bin/sgs-boot.jar tutorial/conf/HelloWorld.boot

You should see the application print out “Hello World!” to standard output (as well as a couple of PDS startup log messages) and then sit doing nothing. At this point you can kill the application; there are two ways of doing this:

1. The preferred method is by using the shipped executable shutdown jar file by issuing the following command:

java -jar bin/sgs-stop.jar tutorial/conf/HelloWorld.boot

2. Typing Ctrl-c in the shell window will also stop the server and get the prompt back.

       你会看到控制台打印出“Hello World”,然后就什么都不做了。这时,你可以关闭程序,有两种方法:

  1. 推荐的方法通过执行停止jar包来实现,就像下面:

                                       java -jar bin/sgs-stop.jar tutorial/conf/HelloWorld.boot

  1. 直接在命令行中按Ctrl-c也能立刻让服务停止。

If you are interested, you can examine the HelloWorld.boot and the HelloWorld.properties files to see the details of how you set up an application configuration to run in the PDS.

       如果你有兴趣,可以看看HelloWorld.bootHelloWorld.properties文件中关于如何建立一个运行在PDS中的应用配置的细节部分。

 

重新运行HelloWorld

If you stop the PDS and then run the HelloWorld application again, you will notice that you don’t get a “Hello World!” output the second time. This is because the AppListener already exists in the Object Store from the previous run, and thus the initialize method on it is never called.

       如果你停止了PDS,然后又再次运行HelloWorld,你将不会第二次看到“HelloWorld”。这是因为AppListener在之前的运行中,已经存在于对象存储器(Object Store)中了,如此,initialize方法就不会被调用。

The location of the Object Store is specified by the com.sun.sgs.app.root property in the HelloWorld.properties file. If you want to see “Hello World” again, you can do it by clearing the Object Store with the following commands:

● For Unix:  rm -r tutorial/data/HelloWorld/dsdb/*

● For Windows: del /s tutorial\data\HelloWorld\dsdb\*.*

       对象存储器的位置在HelloWorld.properties文件中的com.sun.sgs.app.root属性中指定。如果你想再次看到“Hello World”,你可以用一下命令来清理对象存储器:

●  Unix:

rm -r tutorial/data/HelloWorld/dsdb/*

●  Windows:

del /s tutorial\data\HelloWorld\dsdb\*.*

注释:

(5) 附录中SwordWorld的例子展示了如何用这个行为来在对象存储器中创建你的初始的管理对象世界。

原文地址:https://www.cnblogs.com/sevenyuan/p/1609585.html