KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > test > ImportThread


1 package org.jacorb.trading.test;
2
3 import org.omg.CosTrading.*;
4 import org.omg.CosTrading.RegisterPackage.*;
5 import org.omg.CosTrading.LookupPackage.*;
6 import org.omg.CosTradingRepos.*;
7 import org.omg.CosTradingRepos.ServiceTypeRepositoryPackage.*;
8 import org.omg.CORBA.*;
9 import java.util.*;
10 /**
11  * This class is an importer, which randomly selects a trader to query.
12  *
13  * @author Nicolas Noffke
14  */

15
16 public class ImportThread extends Thread JavaDoc {
17     private static final int TOTAL_QUERIES = 100;
18     private static final int MAX_SLEEP = 10;
19
20     private static long m_received_queries = 0;
21     private static Random m_rand = new Random();
22
23     private Lookup[] m_lookup;
24     private String JavaDoc m_type;
25     private org.omg.CORBA.ORB JavaDoc m_orb;
26
27     public int m_correct_results = 0;
28     public int m_incorrect_results = 0;
29
30     public ImportThread(String JavaDoc name, Lookup[] lookup, String JavaDoc type, org.omg.CORBA.ORB JavaDoc orb){
31     super(name);
32     m_lookup = lookup;
33     m_type = type;
34     m_orb = orb;
35
36     start();
37     }
38     
39     public void run (){
40     //build import policies
41
org.omg.CosTrading.Policy[] _policies = new org.omg.CosTrading.Policy[3];
42     _policies[0] = new org.omg.CosTrading.Policy();
43     _policies[0].name = "exact_type_match";
44     _policies[0].value = m_orb.create_any();
45     _policies[0].value.insert_boolean(false);
46     _policies[1] = new org.omg.CosTrading.Policy();
47     _policies[1].name = "use_dynamic_properties";
48     _policies[1].value = m_orb.create_any();
49     _policies[1].value.insert_boolean(false);
50     _policies[2] = new org.omg.CosTrading.Policy();
51     _policies[2].name = "use_proxy_offers";
52     _policies[2].value = m_orb.create_any();
53     _policies[2].value.insert_boolean(false);
54
55     for (int _i = 0; _i < TOTAL_QUERIES; _i++){
56         try {
57         //build up import parameters
58
String JavaDoc _constraint = "TRUE";
59         String JavaDoc _preference = "";
60         
61         SpecifiedProps _desired_props = new SpecifiedProps();
62                 // _desired_props.all_dummy((short)0);
63

64         OfferSeqHolder _offers = new OfferSeqHolder();
65         OfferIteratorHolder _iter = new OfferIteratorHolder();
66         PolicyNameSeqHolder _limits = new PolicyNameSeqHolder();
67
68         //randomly select a trader and query it
69
int n = Math.abs(m_rand.nextInt() % 4);
70         m_lookup[n].query(m_type, _constraint, _preference, _policies, _desired_props, 20,
71                   _offers, _iter, _limits);
72         
73         boolean[] _offer_ok = new boolean[8];
74         if (_offers.value != null && _offers.value.length == 8){
75
76             //init array
77
for (int _j = 0; _j < 8; _j++)
78             _offer_ok[_j] = false;
79
80             //check if result correct (offers are numbers)
81
for (int _j = 0; _j < 8; _j++){
82             String JavaDoc _name = _offers.value[_j].properties[0].value.extract_string();
83             _offer_ok[Integer.parseInt(_name)] = true;
84             }
85
86             //test if all expected results were received
87
boolean _result = true;
88             for (int _j = 0; _j < 8; _j++)
89             _result &= _offer_ok[_j];
90             
91             System.out.println("(" + m_received_queries++ + " / " + _i + ") Thread " + getName() +
92                        " received a correct result");
93             m_correct_results++;
94         }
95         else {
96             System.out.println("(" + m_received_queries++ + " / " + _i + ") Thread " + getName() +
97                        " received an incorrect result");
98             System.out.println("Reason: only " + _offers.value.length + " offers instead of 8 received");
99             m_incorrect_results++;
100         }
101         
102         sleep(Math.abs(m_rand.nextInt() % MAX_SLEEP));
103         
104         } catch (Exception JavaDoc _e){
105         System.out.println("(" + m_received_queries++ + " / " + _i + ") Thread " + getName() +
106                    " caught an exception");
107         _e.printStackTrace();
108         m_incorrect_results++;
109         }
110     }
111     System.out.println("(" + getName() + ") Finished querying. Received " + m_correct_results +
112                "correct results and " + m_incorrect_results + " incorrect results");
113
114     }
115 } // ImportThread
116

117
118
119
Popular Tags