summaryrefslogtreecommitdiff
path: root/TAO/examples/Simple/time/Time_Client.java
blob: 6f430ec48ad1506a1ef9224af69afb226323d360 (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
150
151
152
153
154
// $Id$

// ============================================================================
//
// = LIBRARY
//    TAO/examples/Simple/time
//
// = FILENAME
//    Time_Client.java
//
// = DESCRIPTION
//    This class implements a Java client interface to the Time example.
//
// = AUTHOR
//   Martin Krumpolec <krumpo@pobox.sk>
//
// ============================================================================

import JACE.Misc.GetOpt;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;
import java.io.*;

public class Time_Client
{
    private ORB orb = null;
    private org.omg.CORBA.Object timeRef = null;
    private boolean shutdown = false;
    private boolean use_naming_service = false;
    private String ior_file = null;
    private String ior = null;

    Time_Client (String args [])
    {
        orb = ORB.init (args, null);
        parse_args (args);
    }

    private void parse_args (String args[])
    {
        GetOpt getopt = new GetOpt (args, "k:f:xn");

        int ch = -1;

        while ((ch = getopt.next ()) != -1)
            {
                switch (ch)
                    {
                    case 'k':
                        ior = getopt.opt_arg () ();
                        break;
                    case 'f':
                        ior_file = getopt.opt_arg () ();
                        try
                            {
                                java.io.FileInputStream file =
                                    new FileInputStream(ior_file);
                                java.io.BufferedReader myInput =
                                    new BufferedReader
                                        (new InputStreamReader (file));
                                ior = myInput.readLine();
                            }
                        catch (Exception e)
                            {
                                System.out.println ("Error: " + e);
                            }
                        break;
                    case 'x':
                        shutdown = true;
                        break;
                    case 'n':
                        use_naming_service = true;
                        break;
                    default:
                        break;
                    }
            }

        return;
    }

    private Time get_time_object ()
    {
        if (ior != null)
            {
                org.omg.CORBA.Object objref = orb.string_to_object (ior);
                return TimeHelper.narrow (objref);
            }
        else
            {
                if (use_naming_service)
                    {
                        // Use multicast to obtain NS handle.
                        NS_Resolve ns_resolve =
                            new NS_Resolve (null);
                        org.omg.CORBA.Object objRef =
                            ns_resolve.resolve_name_service (orb);

                        // Get the root naming context.
                        NamingContext ncRef =
                            NamingContextHelper.narrow (objRef);
                        System.out.println ("got initial NS reference");

                        // Resolve the Object Reference in Naming.
                        NameComponent nc =
                            new NameComponent ("Time", "");
                        NameComponent path[] = {nc};
                        try
                            {
                                return TimeHelper.narrow (ncRef.resolve (path));
                            }
                        catch (Exception e)
                            {
                                System.out.println ("Error: " + e);
                            }
                    }
            }
        return null;
    }

  public static void main (String args[])
  {
    try
      {
        Time_Client client = new Time_Client (args);
        Time timeRef = client.get_time_object ();

        if (timeRef == null)
            {
                System.out.println ("Unable to resolve Time.");
                System.exit (-1);
            }

        // Call the Time server object and print results.
        int time = timeRef.current_time ();

        java.util.Date date =
          new java.util.Date (((long) time) * 1000);

        System.out.println ("string time is " + date);

        if (client.shutdown)
            {
                timeRef.shutdown ();
                System.out.println ("Shutting down server ...");
            }
      }
    catch (Exception e)
      {
        System.out.println ("ERROR : " + e) ;
        e.printStackTrace (System.out);
      }
  }
}