summaryrefslogtreecommitdiff
path: root/java/ImageProcessing/framework/TestHandler.java
blob: 5a97ae6f8b268cb31d49a52a3e87d0303f6b1c5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package imaging.framework;

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import JACE.OS.*;
import JACE.Connection.*;
import JACE.Timers.*;

public class TestHandler extends SvcHandler
{
  public TestHandler (String imageList,
		      String JAWSServer, int JAWSPort,
		      ImageApp parent)
  {
    this.imageList_ = imageList;
    this.JAWSServer_ = JAWSServer;
    this.JAWSPort_ = JAWSPort;
    this.parent_ = parent;
    this.filterTable_ = this.parent_.filterTable();
  }

  public int open (Object obj)
  {
    // We got called by the Connector so assume connection was set up
    // fine and therfore do not use standard output
    stdOut = false;

    doTesting ();
    return 0;
  }

  public void doTesting ()
  {
    if (imageList_ != null)
      {
	StringTokenizer tokens = new StringTokenizer (imageList_);
	String image = null;
	
	// Now parse the string, picking up image names. 
	while (tokens.hasMoreTokens ())
	  {
	    // Get the next token
	    image = tokens.nextToken ();
	    this.process (image);
	  }
      }
  }

  private void write (String data)
  {
    try
      {
	// If we are connected to the server then send the data to the
	// server, otherwise write it to standard out.
	if (stdOut)
	  System.out.print (data);
	else
	  this.peer ().send (data);
      }
    catch (IOException e)
      {
	ACE.ERROR (e);
      }
  }

  private void process (String image)
  {
    this.write ("Image: " + image + "\n");

    this.loadImage (image);
    this.processImage (image);
    this.uploadImage (image);
  }

  private void loadImage (String image)
  {
    this.write ("\tLoading...");
    
    // Start the timer
    timer_.start ();
    
    // Load the image
    parent_.openURL (image);
    
    // Stop the timer
    timer_.stop ();
    long time = timer_.elapsedTime ();

    this.write ("done (" + ((double) time)/1000 + " seconds).\n");
  }

  private void processImage (String image)
  {
    this.write ("\tProcessing...\n");

    for (Enumeration e = filterTable_.keys (); e.hasMoreElements (); )
      {
	String filterName = (String) e.nextElement ();

	this.write ("\t\t" + filterName + "...");
	
	ImageFilter filter = (ImageFilter) filterTable_.get (filterName);
	    
	// Reset the image -- in case it was modified earlier
	this.parent_.resetImage ();

	// Start the timer
	timer_.start ();
	
	this.parent_.apply (filter);
	
	// Stop the timer
	timer_.stop ();
	long time = timer_.elapsedTime ();
	
	this.write ("done (" + ((double) time)/1000 + " seconds).\n");
      }
  }
   
  private void uploadImage (String image)
  {
    int index = image.lastIndexOf ("/");
    String imageName = image.substring (index+1);
    String url = "http://" + this.JAWSServer_ + ":" + this.JAWSPort_ + "/" + imageName;
    this.write ("\tUploading " + url + "...");

    // Start the timer
    timer_.start ();

    this.parent_.saveFile (url);

    // Stop the timer
    timer_.stop ();
    long time = timer_.elapsedTime ();

    this.write ("done (" + ((double) time)/1000 + " seconds).\n");		       
  }

  private ImageApp parent_ = null;
  private ProfileTimer timer_ = new ProfileTimer ();
  private String imageList_ = null;
  private boolean stdOut = true;
  private Hashtable filterTable_ = null;
  private String JAWSServer_ = null;
  private int JAWSPort_ = 5432;
}