1 package fr.ove.openmath.mfd2;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.net.Socket;
6 import java.io.*;
7 import java.net.*;
8 import fr.ove.openmath.mfd2.PanelMfd2;
9
10 public final class Mfd2 {
11 private static final String HOST_NAME = "localhost";
12 private static final int PORT_NUMBER = 6666;
13 static String hostName;
14 static int port;
15
16 static Socket socket;
17
18 /***
19 * No-args constructor.
20 */
21 public Mfd2() throws IOException {
22 this(HOST_NAME, PORT_NUMBER);
23 }
24
25 /***
26 * Constructor.
27 * Instantiates the OM appletication and the appropriate communication module.
28 * Registers each as listening to the other.
29 *
30 * @param anOMAppName name of the OM appletication
31 * @param commModuleName exactly that
32 * @param hostName host on which the server runs
33 * @param port on which the server listens
34 * @see fr.essi.sander.om.clientserver.CommFactory
35 */
36 public Mfd2(String hostName, int port) throws IOException {
37 this.hostName = hostName;
38 this.port = port;
39
40 socket = new Socket(InetAddress.getByName(hostName), port);
41 }
42
43 public Socket getSocket() {
44 return socket;
45 }
46
47
48 /***
49 * Entry point for JVM.
50 * This should be run
51 * <OL>
52 * <LI>first on the server side, e.g.,<BR>
53 * java Mfd2 ServerSideSocketeer
54 * <LI>then on the client side, e.g.,<BR>
55 * java Mfd2 ClientSideSocketeer
56 * </OL>
57 *
58 * @param args name of communication module to use
59 * @see fr.essi.sander.om.clientserver.OMCommFactory
60 */
61 public static void main(String args[]) {
62 Mfd2 mfd2 = null;
63 final PanelMfd2 panelMfd2;
64
65 try {
66 switch (args.length) {
67 case 2 :
68 mfd2 = new Mfd2(args[0], Integer.parseInt(args[1]));
69 break;
70 case 0 :
71 mfd2 = new Mfd2();
72 break;
73 default :
74 usage();
75 System.exit(0);
76 }
77 }
78 catch (IOException ioe) {
79 System.out.println("Connection to : " + mfd2.hostName + " port " + mfd2.port + " failed");
80 ioe.printStackTrace();
81 System.exit(0);
82 }
83
84
85
86 Frame mainFrame = new Frame();
87 mainFrame.setTitle("Mfd2 Running. Connected to " + mfd2.hostName + " port " + mfd2.port);
88 mainFrame.setSize(550,450);
89 mainFrame.setBackground(new Color(12632256));
90 panelMfd2 = new PanelMfd2(mfd2.getSocket());
91 mainFrame.add(panelMfd2);
92 mainFrame.addWindowListener(
93 new WindowAdapter() {
94 public void windowClosing(WindowEvent event) {
95
96
97
98 event.getWindow().dispose();
99 try {
100 String object = "<OMOBJ>\n <OMS cd=\"control\" name=\"terminate\"/>\n</OMOBJ>";
101 panelMfd2.setObject(object.getBytes());
102 panelMfd2.closeStreams();
103 socket.close();
104 }
105 catch (IOException ioe) {
106 System.out.println("Can t close socket");
107 ioe.printStackTrace();
108 }
109
110 System.exit(0);
111 }
112 }
113 );
114
115 mainFrame.setVisible(true);
116 }
117
118 /***
119 * Hints how to use the damn thing.
120 */
121 public static void usage() {
122 final String USAGE = "usage:\tjava fr.ove.openmath.mfd2.Mfd2 [hostname [port]]\n";
123 System.err.println(USAGE);
124 }
125
126 }