You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by ch...@apache.org on 2014/10/02 20:42:02 UTC

svn commit: r1629036 - in /uima/sandbox/uima-ducc/trunk/uima-ducc-examples/src/main/java/org/apache/uima/ducc/test/service: AnonymousPinger.java AnonymousService.java

Author: challngr
Date: Thu Oct  2 18:42:01 2014
New Revision: 1629036

URL: http://svn.apache.org/r1629036
Log:
UIMA-4023 CLI / API tests - support for Ping-only and MR-based service testing.

Added:
    uima/sandbox/uima-ducc/trunk/uima-ducc-examples/src/main/java/org/apache/uima/ducc/test/service/AnonymousPinger.java
    uima/sandbox/uima-ducc/trunk/uima-ducc-examples/src/main/java/org/apache/uima/ducc/test/service/AnonymousService.java

Added: uima/sandbox/uima-ducc/trunk/uima-ducc-examples/src/main/java/org/apache/uima/ducc/test/service/AnonymousPinger.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uima-ducc/trunk/uima-ducc-examples/src/main/java/org/apache/uima/ducc/test/service/AnonymousPinger.java?rev=1629036&view=auto
==============================================================================
--- uima/sandbox/uima-ducc/trunk/uima-ducc-examples/src/main/java/org/apache/uima/ducc/test/service/AnonymousPinger.java (added)
+++ uima/sandbox/uima-ducc/trunk/uima-ducc-examples/src/main/java/org/apache/uima/ducc/test/service/AnonymousPinger.java Thu Oct  2 18:42:01 2014
@@ -0,0 +1,96 @@
+package org.apache.uima.ducc.test.service;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.net.Socket;
+
+import org.apache.uima.ducc.cli.AServicePing;
+import org.apache.uima.ducc.cli.ServiceStatistics;
+
+
+/**
+ * This is designed for the simple sleeper "service" that does nothing other than
+ * wait for requests from the pinger.
+ *
+ * The necessary endpoint is CUSTOM:name:host:port
+ */
+public class AnonymousPinger
+    extends AServicePing
+{
+    String host;
+    int  port;
+
+    public void init(String arguments, String endpoint)
+    	throws Exception
+    {
+        System.out.println("INIT: endpoint " + endpoint);
+        System.out.println("Socket file: " + arguments);
+
+        FileInputStream fis = new FileInputStream(arguments);
+
+        byte[] bytes = new byte[128];
+        int bytesread = fis.read(bytes);
+        fis.close();
+        String sockloc = new String(bytes, 0, bytesread);
+        System.out.println("Service is listening at " + sockloc);
+
+        String [] parts = sockloc.split(":");
+        for ( String s : parts ) {
+        	System.out.println("Parts: " + s);
+        }
+        host = parts[0];
+        port = Integer.parseInt(parts[1]);
+        System.out.println("Parsed service location to " + host + " : " + port);
+    }
+
+    public void stop()
+    {
+
+    }
+
+    public ServiceStatistics getStatistics()
+    {
+        ServiceStatistics stats = new ServiceStatistics(false, false,"<NA>");
+        Socket sock = null;
+
+        try {
+            sock = new Socket(host, port);
+            
+            ObjectInputStream dis = new ObjectInputStream(sock.getInputStream());
+            String response = (String) dis.readObject();
+            System.out.println("Pong response: " + response);
+
+            stats.setAlive(true);
+            stats.setHealthy(true);
+            stats.setInfo( response );
+        } catch ( Throwable t) {
+        	t.printStackTrace();
+            stats.setInfo(t.getMessage());
+        } finally {
+        	try { sock.close(); } catch (IOException e) {}
+        }
+        return stats;        
+    }
+
+    public static void main(String[] args)
+    {
+        try {
+			AnonymousPinger cp = new AnonymousPinger();
+			cp.init(args[0], args[1]);
+			for ( int i = 0; i < 10; i++ ) {
+			    ServiceStatistics stats = cp.getStatistics();
+			    System.out.println(stats);
+			    Thread.sleep(2000);
+			}
+			cp.stop();
+		} catch (InterruptedException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		} catch (Exception e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+    }
+
+}

Added: uima/sandbox/uima-ducc/trunk/uima-ducc-examples/src/main/java/org/apache/uima/ducc/test/service/AnonymousService.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uima-ducc/trunk/uima-ducc-examples/src/main/java/org/apache/uima/ducc/test/service/AnonymousService.java?rev=1629036&view=auto
==============================================================================
--- uima/sandbox/uima-ducc/trunk/uima-ducc-examples/src/main/java/org/apache/uima/ducc/test/service/AnonymousService.java (added)
+++ uima/sandbox/uima-ducc/trunk/uima-ducc-examples/src/main/java/org/apache/uima/ducc/test/service/AnonymousService.java Thu Oct  2 18:42:01 2014
@@ -0,0 +1,130 @@
+package org.apache.uima.ducc.test.service;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.ObjectOutputStream;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+
+
+public class AnonymousService
+{
+    String sockdir;
+    AnonymousService(String sockdir)
+        throws Exception
+    {
+        System.out.println("Custom Service starts");
+        this.sockdir = sockdir;
+        File f = new File(sockdir);
+        if ( f.exists() && f.isDirectory() ) {
+            System.out.println("Reusing socket dir: " + sockdir);
+            return;
+        }
+
+        if ( f.exists()) {
+            throw new IllegalStateException("Socket tempdir conflict, exits but is not a directory: " + sockdir);
+        }
+        
+        System.out.println("Making new sockdir: " + sockdir);
+        f.mkdirs();
+    }
+
+    void startPingResponder()
+    	throws Exception
+    {
+        PingResponder pr = new PingResponder(sockdir);
+        Thread prth = new Thread(pr);
+        prth.start();
+    }
+
+    void run()
+    	throws Exception
+    {
+        startPingResponder();
+
+        try {
+			while ( true ) {
+			    System.out.println("Serving stuff");
+			    Thread.sleep(30000);
+			}
+		} catch (InterruptedException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+    }
+
+    public static void main(String[] args)
+    {
+        if ( args.length > 0 ) {
+            for ( int i = 0; i < args.length; i++ ) {
+                System.out.println("Args[" + i + "] = " + args[i]);
+            }
+        } else {
+            System.out.println("No args, can't run");
+            System.exit(1);
+        }
+
+        try {
+			AnonymousService as = new AnonymousService(args[0]);
+			as.run();
+		} catch (Exception e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+    }
+
+    class PingResponder
+        implements Runnable
+    {
+        String sockdir;
+        String hostname;
+        PingResponder(String sockdir)
+        	throws Exception
+        {
+            this.sockdir = sockdir;
+            this. hostname = InetAddress.getLocalHost().getCanonicalHostName();
+            System.out.println("Starting ping responder on " + hostname);
+        }
+
+        public void run()
+        {
+            ServerSocket server = null;
+            System.out.println("Waiting for ping");
+            int pingid = 0;
+            try {
+
+                server = new ServerSocket(0);
+                int port = server.getLocalPort();
+
+                System.out.println("ServicePingMain listens on port " + port);
+                
+                File f = new File(sockdir + "/hostport");
+                FileOutputStream fos = new FileOutputStream(f);
+                fos.write((hostname + ":" + port).getBytes());
+                fos.close();
+
+                while ( true ) {
+                    Socket sock = server.accept();
+                    ObjectOutputStream out = new ObjectOutputStream(sock.getOutputStream());
+                    String reply = "Pong number " + pingid++;
+                    System.out.println("Responding to ping: " + reply);
+                    out.writeObject(reply);
+                    out.flush();
+                    out.close();                    
+                }
+			} catch (Exception e) {
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+                return;
+			} finally {
+                try {
+					server.close();
+				} catch (IOException e) {
+					e.printStackTrace();
+				}
+            }
+        }
+    }
+}