directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dran...@apache.org
Subject [1/2] directory-kerby git commit: DIRKRB-318 Added sample client and server basics
Date Thu, 02 Jul 2015 06:41:38 GMT
Repository: directory-kerby
Updated Branches:
  refs/heads/master 7f98de0bc -> d70edca9f


DIRKRB-318 Added sample client and server basics


Project: http://git-wip-us.apache.org/repos/asf/directory-kerby/repo
Commit: http://git-wip-us.apache.org/repos/asf/directory-kerby/commit/a7ff654e
Tree: http://git-wip-us.apache.org/repos/asf/directory-kerby/tree/a7ff654e
Diff: http://git-wip-us.apache.org/repos/asf/directory-kerby/diff/a7ff654e

Branch: refs/heads/master
Commit: a7ff654e57618e759c58ea6ad9b16102555ef854
Parents: 9170df7
Author: drankye <kai.zheng@intel.com>
Authored: Thu Jul 2 22:22:42 2015 +0800
Committer: Drankye <drankye@gmail.com>
Committed: Thu Jul 2 22:22:42 2015 +0800

----------------------------------------------------------------------
 .../kerberos/kerb/integration/test/AppBase.java |  36 +++++
 .../kerb/integration/test/AppClient.java        |  61 +++++++++
 .../kerb/integration/test/AppServer.java        |  78 +++++++++++
 .../kerb/integration/test/Transport.java        | 135 +++++++++++++++++++
 4 files changed, 310 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a7ff654e/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/AppBase.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/AppBase.java
b/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/AppBase.java
new file mode 100644
index 0000000..278ce4f
--- /dev/null
+++ b/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/AppBase.java
@@ -0,0 +1,36 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.integration.test;
+
+/**
+ * Making it runnable because the app will be launched in a separate thread in
+ * a test.
+ */
+public abstract class AppBase implements Runnable {
+    private boolean isTestOK = false;
+
+    public synchronized void setTestOK(boolean isOK) {
+        this.isTestOK = isOK;
+    }
+
+    public boolean isTestOK() {
+        return isTestOK;
+    }
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a7ff654e/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/AppClient.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/AppClient.java
b/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/AppClient.java
new file mode 100644
index 0000000..4ae19a6
--- /dev/null
+++ b/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/AppClient.java
@@ -0,0 +1,61 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.integration.test;
+
+import java.io.IOException;
+
+public abstract class AppClient extends AppBase {
+    protected Transport.Connection conn;
+
+    protected void usage(String[] args) {
+        if (args.length < 2) {
+            System.err.println("Usage: java <options> AppClient "
+                    + "<server-host> <server-port>");
+            System.exit(-1);
+        }
+    }
+
+    public AppClient(String[] args) throws Exception {
+        usage(args);
+
+        String hostName = args[0];
+        short port = (short) Integer.parseInt(args[1]);
+
+        this.conn = Transport.Connector.connect(hostName, port);
+    }
+
+    public void run() {
+        System.out.println("Connected to server");
+
+        try {
+            withConnection(conn);
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                conn.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    protected abstract void withConnection(Transport.Connection conn) throws Exception;
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a7ff654e/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/AppServer.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/AppServer.java
b/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/AppServer.java
new file mode 100644
index 0000000..0d7a8fa
--- /dev/null
+++ b/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/AppServer.java
@@ -0,0 +1,78 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.integration.test;
+
+import java.io.IOException;
+
+public abstract class AppServer extends AppBase {
+    protected Transport.Acceptor acceptor;
+    private boolean terminated = false;
+
+    protected void usage(String[] args) {
+        if (args.length < 1) {
+            System.err.println("Usage: java <options> AppServer <ListenPort>");
+            System.exit(-1);
+        }
+    }
+
+    public AppServer(String[] args) throws IOException {
+        usage(args);
+
+        short listenPort = (short) Integer.parseInt(args[0]);
+        this.acceptor = new Transport.Acceptor(listenPort);
+    }
+
+    public synchronized void terminate() {
+        terminated = true;
+    }
+
+    public void run() {
+        try {
+            synchronized (this) {
+                while (!terminated) {
+                    runOnce();
+                }
+            }
+        } finally {
+            acceptor.close();
+        }
+    }
+
+    private void runOnce() {
+        System.out.println("Waiting for incoming connection...");
+
+        Transport.Connection conn = acceptor.accept();
+        System.out.println("Got connection from client");
+
+        try {
+            onConnection(conn);
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                conn.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    protected abstract void onConnection(Transport.Connection conn) throws Exception;
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a7ff654e/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/Transport.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/Transport.java
b/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/Transport.java
new file mode 100644
index 0000000..ee14e43
--- /dev/null
+++ b/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/Transport.java
@@ -0,0 +1,135 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.integration.test;
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.net.Socket;
+
+public class Transport {
+
+    public static class Acceptor {
+        ServerSocket serverSocket;
+
+        public Acceptor(short listenPort) throws IOException {
+            this.serverSocket = new ServerSocket(listenPort);
+        }
+
+        public Connection accept() {
+            try {
+                Socket socket = serverSocket.accept();
+                return new Connection(socket);
+            } catch (IOException ioe) {
+                throw new RuntimeException(ioe);
+            }
+        }
+
+        public void close() {
+            try {
+                serverSocket.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    public static class Connector {
+        public static Connection connect(String host, short port) throws IOException {
+            Socket socket = new Socket(host, port);
+            return new Connection(socket);
+        }
+    }
+
+    public static class Connection {
+        private Socket socket;
+        private DataInputStream instream;
+        private DataOutputStream outstream;
+
+        public Connection(Socket socket) throws IOException {
+            this.socket = socket;
+            instream = new DataInputStream(socket.getInputStream());
+            outstream = new DataOutputStream(socket.getOutputStream());
+        }
+
+        public void close() throws IOException {
+            socket.close();
+        }
+
+        public void sendToken(byte[] token) throws IOException {
+            if (token != null) {
+                outstream.writeInt(token.length);
+                outstream.write(token);
+            } else {
+                outstream.writeInt(0);
+            }
+            outstream.flush();
+        }
+
+        public void sendMessage(Message msg) throws IOException {
+            if (msg != null) {
+                sendToken(msg.header);
+                sendToken(msg.body);
+            }
+        }
+
+        public void sendMessage(byte[] header, byte[] body) throws IOException {
+            sendMessage(new Message(header, body));
+        }
+
+        public void sendMessage(String header, byte[] body) throws IOException {
+            sendMessage(new Message(header, body));
+        }
+
+        public byte[] recvToken() throws IOException {
+            int len = instream.readInt();
+            if (len > 0) {
+                byte[] token = new byte[len];
+                instream.readFully(token);
+                return token;
+            }
+            return null;
+        }
+
+        public Message recvMessage() throws IOException {
+            byte[] header = recvToken();
+            byte[] body = recvToken();
+            Message msg = new Message(header, body);
+            return msg;
+        }
+    }
+
+    public static class Message {
+        public byte[] header;
+        public byte[] body;
+
+
+        Message(byte[] header, byte[] body) {
+            this.header = header;
+            this.body = body;
+        }
+
+        public Message(String header, byte[] body) {
+            this.header = header.getBytes();
+            this.body = body;
+        }
+    }
+}


Mime
View raw message