Added Echo example.
authorJan Vrany <jan.vrany@fit.cvut.cz>
Mon, 11 Aug 2014 12:23:33 +0100
changeset 3221 3aed12bd6638
parent 3220 dac830f75266
child 3222 81e439368e3d
child 3224 79fe9b2d4932
Added Echo example. A simple example with TCP server and client
examples/Make.proto
examples/Make.spec
examples/abbrev.stc
examples/bc.mak
examples/examples.rc
examples/java/src/stx/libjava/examples/Echo.java
examples/libInit.cc
examples/stx_libjava_examples.st
--- a/examples/Make.proto	Mon Aug 11 10:57:46 2014 +0100
+++ b/examples/Make.proto	Mon Aug 11 12:23:33 2014 +0100
@@ -1,4 +1,4 @@
-# $Header: /cvs/stx/stx/libjava/examples/Make.proto,v 1.5 2013-09-06 00:41:46 vrany Exp $
+# $Header$
 #
 # DO NOT EDIT
 # automagically generated from the projectDefinition: stx_libjava_examples.
@@ -34,7 +34,7 @@
 # add the path(es) here:,
 # ********** OPTIONAL: MODIFY the next lines ***
 # LOCALINCLUDES=-Ifoo -Ibar
-LOCALINCLUDES= -I$(INCLUDE_TOP)/stx/libbasic
+LOCALINCLUDES= -I$(INCLUDE_TOP)/stx/libbasic -I$(INCLUDE_TOP)/stx/libjava
 
 
 # if you need any additional defines for embedded C code,
@@ -114,7 +114,7 @@
 
 # run default testsuite for this package
 test: $(TOP)/goodies/builder/reports
-	$(MAKE) -C $(TOP)/goodies/builder/reports
+	$(MAKE) -C $(TOP)/goodies/builder/reports -f Makefile.init
 	$(TOP)/goodies/builder/reports/report-runner.sh -D . -r Builder::TestReport -p $(PACKAGE)
 
 
@@ -153,7 +153,7 @@
 
 
 # BEGINMAKEDEPEND --- do not remove this line; make depend needs it
-$(OUTDIR)stx_libjava_examples.$(O) stx_libjava_examples.$(H): stx_libjava_examples.st $(INCLUDE_TOP)/stx/libbasic/LibraryDefinition.$(H) $(INCLUDE_TOP)/stx/libbasic/ProjectDefinition.$(H) $(INCLUDE_TOP)/stx/libbasic/Object.$(H) $(STCHDR)
+$(OUTDIR)stx_libjava_examples.$(O) stx_libjava_examples.$(H): stx_libjava_examples.st $(INCLUDE_TOP)/stx/libbasic/LibraryDefinition.$(H) $(INCLUDE_TOP)/stx/libbasic/Object.$(H) $(INCLUDE_TOP)/stx/libbasic/ProjectDefinition.$(H) $(STCHDR)
 
 # ENDMAKEDEPEND --- do not remove this line
 
--- a/examples/Make.spec	Mon Aug 11 10:57:46 2014 +0100
+++ b/examples/Make.spec	Mon Aug 11 12:23:33 2014 +0100
@@ -1,4 +1,4 @@
-# $Header: /cvs/stx/stx/libjava/examples/Make.spec,v 1.5 2013-09-06 00:41:46 vrany Exp $
+# $Header$
 #
 # DO NOT EDIT
 # automagically generated from the projectDefinition: stx_libjava_examples.
--- a/examples/abbrev.stc	Mon Aug 11 10:57:46 2014 +0100
+++ b/examples/abbrev.stc	Mon Aug 11 12:23:33 2014 +0100
@@ -1,9 +1,9 @@
 # automagically generated by the project definition
 # this file is needed for stc to be able to compile modules independently.
 # it provides information about a classes filename, category and especially namespace.
-JavaExamples JavaExamples stx:libjava/examples 'Languages-Java-Examples' 0
 stx_libjava_examples stx_libjava_examples stx:libjava/examples '* Projects & Packages *' 3
+CDDatabaseHandler1 CDDatabaseHandler1 stx:libjava/examples 'Languages-Java-Examples' 0
+CDDatabaseHandler2 CDDatabaseHandler2 stx:libjava/examples 'Languages-Java-Examples' 0
 HelloWorldExampleS HelloWorldExampleS stx:libjava/examples 'Languages-Java-Utilities' 0
-CDDatabaseHandler2 CDDatabaseHandler2 stx:libjava/examples 'Languages-Java-Examples' 0
-CDDatabaseHandler1 CDDatabaseHandler1 stx:libjava/examples 'Languages-Java-Examples' 0
+JavaExamples JavaExamples stx:libjava/examples 'Languages-Java-Examples' 0
 STComparator STComparator stx:libjava/examples 'Languages-Java-Examples' 0
--- a/examples/bc.mak	Mon Aug 11 10:57:46 2014 +0100
+++ b/examples/bc.mak	Mon Aug 11 12:23:33 2014 +0100
@@ -1,4 +1,4 @@
-# $Header: /cvs/stx/stx/libjava/examples/bc.mak,v 1.4 2013-09-06 00:41:47 vrany Exp $
+# $Header$
 #
 # DO NOT EDIT
 # automagically generated from the projectDefinition: stx_libjava_examples.
@@ -34,7 +34,7 @@
 
 
 
-LOCALINCLUDES= -I$(INCLUDE_TOP)\stx\libbasic
+LOCALINCLUDES= -I$(INCLUDE_TOP)\stx\libbasic -I$(INCLUDE_TOP)\stx\libjava
 LOCALDEFINES=
 
 STCLOCALOPT=-package=$(PACKAGE) -I. $(LOCALINCLUDES) -headerDir=. $(STCLOCALOPTIMIZATIONS) $(STCWARNINGS) $(LOCALDEFINES)  -varPrefix=$(LIBNAME)
@@ -77,7 +77,7 @@
 
 
 # BEGINMAKEDEPEND --- do not remove this line; make depend needs it
-$(OUTDIR)stx_libjava_examples.$(O) stx_libjava_examples.$(H): stx_libjava_examples.st $(INCLUDE_TOP)\stx\libbasic\LibraryDefinition.$(H) $(INCLUDE_TOP)\stx\libbasic\ProjectDefinition.$(H) $(INCLUDE_TOP)\stx\libbasic\Object.$(H) $(STCHDR)
+$(OUTDIR)stx_libjava_examples.$(O) stx_libjava_examples.$(H): stx_libjava_examples.st $(INCLUDE_TOP)\stx\libbasic\LibraryDefinition.$(H) $(INCLUDE_TOP)\stx\libbasic\Object.$(H) $(INCLUDE_TOP)\stx\libbasic\ProjectDefinition.$(H) $(STCHDR)
 
 # ENDMAKEDEPEND --- do not remove this line
 
--- a/examples/examples.rc	Mon Aug 11 10:57:46 2014 +0100
+++ b/examples/examples.rc	Mon Aug 11 12:23:33 2014 +0100
@@ -4,7 +4,7 @@
 //
 VS_VERSION_INFO VERSIONINFO
   FILEVERSION     6,2,32767,32767
-  PRODUCTVERSION  6,2,3,0
+  PRODUCTVERSION  6,2,4,0
 #if (__BORLANDC__)
   FILEFLAGSMASK   VS_FF_DEBUG | VS_FF_PRERELEASE
   FILEFLAGS       VS_FF_PRERELEASE | VS_FF_SPECIALBUILD
@@ -24,8 +24,8 @@
       VALUE "InternalName", "stx:libjava/examples\0"
       VALUE "LegalCopyright", "Copyright Claus Gittinger 1988-2012\nCopyright eXept Software AG 1998-2012\0"
       VALUE "ProductName", "Smalltalk/X\0"
-      VALUE "ProductVersion", "6.2.3.0\0"
-      VALUE "ProductDate", "Thu, 05 Sep 2013 23:11:15 GMT\0"
+      VALUE "ProductVersion", "6.2.4.0\0"
+      VALUE "ProductDate", "Mon, 11 Aug 2014 11:11:45 GMT\0"
     END
 
   END
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/java/src/stx/libjava/examples/Echo.java	Mon Aug 11 12:23:33 2014 +0100
@@ -0,0 +1,147 @@
+package stx.libjava.examples;
+
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.SocketException;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.InputStreamReader;
+import java.io.Writer;
+import java.io.OutputStreamWriter;
+
+
+import stx.libjava.annotation.Package;
+
+@Package("stx:libjava/examples")
+public class Echo extends java.lang.Object {
+    public static int DEFAULT_PORT = 30001;
+
+    public static Server newServer() {
+        return new Server();
+    }
+
+    public static Server newServer(int port) {
+        return new Server(port);
+    }
+
+
+    public static class Server {
+
+        protected int port;
+        protected boolean shuttingdown = false;
+        protected ServerSocket socket;
+        protected Thread worker;
+
+        public Server() {
+            this(DEFAULT_PORT);
+        }
+
+        public Server(int port) {
+            this.port = port;
+        }
+
+        public void start() throws IOException {            
+            socket = new ServerSocket(port);
+            worker = new Thread(new Runnable() {
+               public void run() {
+                    try {
+                        shuttingdown = false;
+                        while ( ! shuttingdown ) {                                            
+                            Socket client = socket.accept();
+                            try {
+                                Reader reader = new InputStreamReader(client.getInputStream());
+                                Writer writer = new OutputStreamWriter(client.getOutputStream());                    
+                                int c;
+                                do {                            
+                                    StringBuffer buffer = new StringBuffer();
+                                    while ( ( ( c = reader.read() ) != -1 ) && (c != '\n') && (c != '\r') ) {
+                                        buffer.append((char)c);
+                                    }
+                                    buffer.append('\n');
+                                    writer.write(buffer.toString());
+                                    writer.flush();
+                                } while ( c != -1 );
+                                //reader.close();
+                                //writer.close();
+                            } catch ( IOException ioe) {
+                                System.err.println("Error handling client: " + ioe.getMessage());
+                                ioe.printStackTrace();
+                            } finally {
+                                client.close();
+                            }
+                        }
+                    } catch ( SocketException se ) {
+                        if ( ! ( shuttingdown && socket.isClosed()) ) {
+                            System.err.println("Error listening for a connection: " + se.getMessage());
+                            se.printStackTrace();
+                        }
+                    } catch (IOException ioe) {
+                        System.err.println("Error listening for a connection: " + ioe.getMessage());
+                        ioe.printStackTrace();
+                    } finally {
+                        socket = null;
+                        worker = null;
+                        shuttingdown = false;
+                    }
+               }
+            });
+            worker.start();
+        }
+
+        public void shutdown() {
+            if (  socket != null && ! socket.isClosed() ) {
+                shuttingdown = true;
+                try {
+                    socket.close();
+                } catch ( IOException ioe ) {
+                    System.err.println("Error closing server socket:" + ioe.getMessage());
+                    ioe.printStackTrace();   
+                }
+
+            }
+        }
+
+    }
+
+    public static Client newClient() {
+        return new Client();
+    }
+
+    public static class Client {
+        protected Socket socket;
+
+        public void connect(String host) throws IOException {
+            connect(host, DEFAULT_PORT);
+        }
+
+        public void connect(String host, int port) throws IOException {
+            socket = new Socket(host, port);
+        }
+
+        public void disconnect() {
+            if ( socket != null) {
+                try {
+                    socket.close();
+                } catch ( IOException ioe) {
+                    System.err.println("Error disconnecting:" + ioe.getMessage());
+                    ioe.printStackTrace();   
+                }
+            }
+        }
+
+        public String echo(String echo) throws IOException  {
+            Reader reader = new InputStreamReader(socket.getInputStream());
+            Writer writer = new OutputStreamWriter(socket.getOutputStream());       
+            writer.write(echo);
+            writer.write('\n');
+            writer.flush();
+            int c;
+            StringBuffer buffer = new StringBuffer();
+            while ( ( ( c = reader.read() ) != -1 ) && (c != '\n') && (c != '\r') ) {
+                buffer.append((char)c);
+            }
+            return buffer.toString();            
+        }
+    }
+
+}
--- a/examples/libInit.cc	Mon Aug 11 10:57:46 2014 +0100
+++ b/examples/libInit.cc	Mon Aug 11 12:23:33 2014 +0100
@@ -1,5 +1,5 @@
 /*
- * $Header: /cvs/stx/stx/libjava/examples/libInit.cc,v 1.3 2013-09-06 00:41:47 vrany Exp $
+ * $Header$
  *
  * DO NOT EDIT
  * automagically generated from the projectDefinition: stx_libjava_examples.
--- a/examples/stx_libjava_examples.st	Mon Aug 11 10:57:46 2014 +0100
+++ b/examples/stx_libjava_examples.st	Mon Aug 11 12:23:33 2014 +0100
@@ -19,28 +19,27 @@
 !
 
 mandatoryPreRequisites
-    "list all required mandatory packages.
-     Packages are mandatory, if they contain superclasses of the package's classes
-     or classes which are extended by this package.
-     This list can be maintained manually or (better) generated and
-     updated by scanning the superclass hierarchies
-     (the browser has a menu function for that)"
+    "list packages which are mandatory as a prerequisite.
+     This are packages containing superclasses of my classes and classes which
+     are extended by myself.
+     They are mandatory, because we need these packages as a prerequisite for loading and compiling.
+     This method is generated automatically,
+     by searching along the inheritance chain of all of my classes."
 
     ^ #(
-        #'stx:libbasic'    "LibraryDefinition - superclass of stx_libjava_examples "
+        #'stx:libbasic'    "Autoload - superclass of CDDatabaseHandler1"
     )
 !
 
 referencedPreRequisites
-    "list all packages containing classes referenced by the packages's members.
-     This list can be maintained manually or (better) generated and
-     updated by looking for global variable accesses
-     (the browser has a menu function for that)
-     However, often too much is found, and you may want to explicitely
-     exclude individual packages in the #excludedFromPreRequisites method."
+    "list packages which are a prerequisite, because they contain
+     classes which are referenced by my classes.
+     We do not need these packages as a prerequisite for loading or compiling.
+     This method is generated automatically,
+     by searching all classes (and their packages) which are referenced by my classes."
 
     ^ #(
-        #'stx:libjava'    "JavaCodeBundle - referenced by stx_libjava_examples class>>javaBundle "
+        #'stx:libjava'    "JavaCodeBundle - referenced by stx_libjava_examples class>>javaBundle"
     )
 !
 
@@ -114,18 +113,18 @@
 
     ^ #(
         "<className> or (<className> attributes...) in load order"
-        (JavaExamples autoload)
         #'stx_libjava_examples'
+        (CDDatabaseHandler1 autoload)
+        (CDDatabaseHandler2 autoload)
         (HelloWorldExampleS autoload)
-        (CDDatabaseHandler2 autoload)
-        (CDDatabaseHandler1 autoload)
+        (JavaExamples autoload)
         (STComparator autoload)
     )
 !
 
 extensionMethodNames
-    "lists the extension methods which are to be included in the project.
-     Entries are 2-element array literals, consisting of class-name and selector."
+    "list class/selector pairs of extensions.
+     A correponding method with real names must be present in my concrete subclasses"
 
     ^ #(
     )