Back to index

im-sdk  12.3.91
Protocol.java
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-2001 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a
00005 copy of this software and associated documentation files (the
00006 "Software"), to deal in the Software without restriction, including
00007 without limitation the rights to use, copy, modify, merge, publish,
00008 distribute, sublicense, and/or sell copies of the Software, and to
00009 permit persons to whom the Software is furnished to do so, subject to
00010 the following conditions: The above copyright notice and this
00011 permission notice shall be included in all copies or substantial
00012 portions of the Software.
00013 
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018 IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022 ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025 Except as contained in this notice, the names of The Open Group and/or
00026 Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027 promote the sale, use or other dealings in this Software without prior
00028 written authorization from The Open Group and/or Sun Microsystems,
00029 Inc., as applicable.
00030 
00031 
00032 X Window System is a trademark of The Open Group
00033 
00034 OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035 logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036 Group. All other trademarks and registered trademarks mentioned herein
00037 are the property of their respective owners. No right, title or
00038 interest in or to any trademark, service mark, logo or trade name of
00039 Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 
00043 
00044 package sun.awt.im.iiimp;
00045 
00046 import java.net.*;
00047 import java.io.*;
00048 
00049 class Protocol implements IIIMProtocol {
00050     ProtocolHeader header = null;
00051     ProtocolData data;
00052     
00053     Protocol() {
00054         data = new ProtocolData();
00055     }
00056 
00057     Protocol(int protocol, ProtocolData data) {
00058         try {
00059            if (data != null) {
00060               data.pad();
00061            }
00062         } catch(IOException e) {
00063         }
00064         this.data = data;
00065        if (data != null) {
00066            calculateHeader(protocol);
00067        }
00068     }
00069     
00070     void calculateHeader(int protocol) {
00071        int pad = ProtocolData.paddings(data.count);
00072        int total = data.count + pad;
00073 
00074         int size = 0;
00075        if(total < (int)Math.pow((double)2, (double)24)) 
00076             size = IIIMProtocol.FOURBYTES_PACKET;
00077        else size = IIIMProtocol.EIGHTBYTES_PACKET;
00078        
00079        header = new ProtocolHeader(size, protocol, total);
00080     }
00081     
00082     public void read(IIIMPInputStream in) {
00083         header = new ProtocolHeader();
00084         synchronized(in) {
00085             header.read(in);
00086         
00087             data.read(in, header.packetLength);
00088         }
00089     }
00090     
00091     public void write(IIIMPOutputStream out) {
00092         if (header == null) {
00093            debug("error");
00094        } else {
00095            header.write(out);
00096        }
00097         data.write(out);
00098     }
00099     
00100     int getOpCode() {
00101         return header.getOpCode();
00102     }
00103 
00104     ProtocolHeader getHeader() {
00105         return header;
00106     }
00107 
00108     ProtocolData getData() {
00109         return data;
00110     }
00111 
00112     static String toString(int opcode) {
00113        switch(opcode) {
00114          case IM_NO_PROTOCOL:
00115            return "IM_NO_PROTOCOL";
00116          case IM_CONNECT:
00117            return "IM_CONNECT";
00118          case IM_CONNECT_REPLY:
00119            return "IM_CONNECT_REPLY";
00120          case IM_DISCONNECT:
00121            return "IM_DISCONNECT";
00122          case IM_DISCONNECT_REPLY:
00123            return "IM_DISCONNECT_REPLY";
00124          case IM_REGISTER_TRIGGER_KEYS:
00125            return "IM_REGISTER_TRIGGER_KEYS";
00126          case IM_TRIGGER_NOTIFY:
00127            return "IM_TRIGGER_NOTIFY";
00128          case IM_TRIGGER_NOTIFY_REPLY:
00129            return "IM_TRIGGER_NOTIFY_REPLY";
00130          case IM_SETIMVALUES:
00131            return "IM_SETIMVALUES";
00132          case IM_SETIMVALUES_REPLY:
00133            return "IM_SETIMVALUES_REPLY";
00134          case IM_GETIMVALUES:
00135            return "IM_GETIMVALUES";
00136          case IM_GETIMVALUES_REPLY:
00137            return "IM_GETIMVALUES_REPLY";
00138          case IM_FORWARD_EVENT:
00139            return "IM_FORWARD_EVENT";
00140          case IM_FORWARD_EVENT_REPLY:
00141            return "IM_FORWARD_EVENT_REPLY";
00142          case IM_COMMIT_STRING:
00143            return "IM_COMMIT_STRING";
00144          case IM_FORWARD_EVENT_WITH_OPERATIONS:
00145            return "IM_FORWARD_EVENT_WITH_OPERATIONS";
00146          case IM_FORWARD_EVENT_WITH_OPERATIONS_REPLY:
00147            return "IM_FORWARD_EVENT_WITH_OPERATIONS_REPLY";
00148          case IM_CREATEIC:
00149            return "IM_CREATEIC";
00150          case IM_CREATEIC_REPLY:
00151            return "IM_CREATEIC_REPLY";
00152          case IM_DESTROYIC:
00153            return "IM_DESTROYIC";
00154          case IM_DESTROYIC_REPLY:
00155            return "IM_DESTROYIC_REPLY";
00156          case IM_SETICVALUES:
00157            return "IM_SETICVALUES";
00158          case IM_SETICVALUES_REPLY:
00159            return "IM_SETICVALUES_REPLY";
00160          case IM_GETICVALUES:
00161            return "IM_GETICVALUES";
00162          case IM_GETICVALUES_REPLY:
00163            return "IM_GETICVALUES_REPLY";
00164          case IM_SETICFOCUS:
00165            return "IM_SETICFOCUS";
00166          case IM_SETICFOCUS_REPLY:
00167            return "IM_SETICFOCUS_REPLY";
00168          case IM_UNSETICFOCUS:
00169            return "IM_UNSETICFOCUS";
00170          case IM_UNSETICFOCUS_REPLY:
00171            return "IM_UNSETICFOCUS_REPLY";
00172          case IM_RESETIC:
00173            return "IM_RESETIC";
00174          case IM_RESETIC_REPLY:
00175            return "IM_RESETIC_REPLY";
00176          case IM_PREEDIT_START:
00177            return "IM_PREEDIT_START";
00178          case IM_PREEDIT_START_REPLY:
00179            return "IM_PREEDIT_START_REPLY";
00180          case IM_PREEDIT_DRAW:
00181            return "IM_PREEDIT_DRAW";
00182          case IM_PREEDIT_DRAW_REPLY:
00183            return "IM_PREEDIT_DRAW_REPLY";
00184          case IM_PREEDIT_DONE:
00185            return "IM_PREEDIT_DONE";
00186          case IM_PREEDIT_DONE_REPLY:
00187            return "IM_PREEDIT_DONE_REPLY";
00188          case IM_STATUS_START:
00189            return "IM_STATUS_START";
00190          case IM_STATUS_START_REPLY:
00191            return "IM_STATUS_START_REPLY";
00192          case IM_STATUS_DRAW:
00193            return "IM_STATUS_DRAW";
00194          case IM_STATUS_DRAW_REPLY:
00195            return "IM_STATUS_DRAW_REPLY";
00196          case IM_STATUS_DONE:
00197            return "IM_STATUS_DONE";
00198          case IM_STATUS_DONE_REPLY:
00199            return "IM_STATUS_DONE_REPLY";
00200          case IM_LOOKUP_CHOICE_START:
00201            return "IM_LOOKUP_CHOICE_START";
00202          case IM_LOOKUP_CHOICE_START_REPLY:
00203            return "IM_LOOKUP_CHOICE_START_REPLY";
00204          case IM_LOOKUP_CHOICE_DRAW:
00205            return "IM_LOOKUP_CHOICE_DRAW";
00206          case IM_LOOKUP_CHOICE_DRAW_REPLY:
00207            return "IM_LOOKUP_CHOICE_DRAW_REPLY";
00208          case IM_LOOKUP_CHOICE_DONE:
00209            return "IM_LOOKUP_CHOICE_DONE";
00210          case IM_LOOKUP_CHOICE_DONE_REPLY:
00211            return "IM_LOOKUP_CHOICE_DONE_REPLY";
00212          case IM_LOOKUP_CHOICE_PROCESS:
00213            return "IM_LOOKUP_CHOICE_PROCESS";
00214          case IM_LOOKUP_CHOICE_PROCESS_REPLY:
00215            return "IM_LOOKUP_CHOICE_PROCESS_REPLY";
00216          case IM_AUX_START:
00217            return "IM_AUX_START";
00218          case IM_AUX_START_REPLY:
00219            return "IM_AUX_START_REPLY";
00220          case IM_AUX_DRAW:
00221            return "IM_AUX_DRAW";
00222          case IM_AUX_DRAW_REPLY:
00223            return "IM_AUX_DRAW_REPLY";
00224          case IM_AUX_DONE:
00225            return "IM_AUX_DONE";
00226          case IM_AUX_DONE_REPLY:
00227            return "IM_AUX_DONE_REPLY";
00228          case IM_AUX_SETVALUES:
00229            return "IM_AUX_SETVALUES";
00230          case IM_AUX_SETVALUES_REPLY:
00231            return "IM_AUX_SETVALUES_REPLY";
00232          default:
00233            return "Unknown Protocol";
00234        }
00235     }
00236 
00237     public String toString() {
00238        int opcode = getOpCode();
00239        return toString(opcode);
00240     }
00241 
00242     private void debug(String str) {
00243        if (Manager.DEBUG) {
00244            System.err.println(str);
00245        }
00246     }
00247 }
00248 
00249 class ProtocolHeader {
00250     int packet_size;
00251     int opCode;
00252     int packetLength;
00253     
00254     ProtocolHeader() {
00255         packet_size = 0;
00256         opCode = 0;
00257         packetLength = 0;
00258     }
00259     
00260     ProtocolHeader(int packet_size, int opCode, int packetLength) {
00261         this.packet_size = packet_size;
00262         this.opCode = opCode;
00263         this.packetLength = packetLength;
00264     }
00265     
00266     void setPacketSize(int size) {
00267         packet_size = size;
00268     }
00269     
00270     int getPacketSize() {
00271         return packet_size;
00272     }
00273     
00274     void setOpCode(int opCode) {
00275         this.opCode = opCode;
00276     }
00277     
00278     int getOpCode() {
00279         return opCode;
00280     }
00281     
00282     void setPacketLength(int length) {
00283         packetLength = length;
00284     }
00285     
00286     int getPacketLength() {
00287         return packetLength;
00288     }
00289     
00290     public void read(IIIMPInputStream in) {
00291         try {
00292             int c = in.read();
00293 
00294             int ret = ((c >>> 7) & 0xFF);
00295             if(ret == 0) {
00296                 packet_size = IIIMProtocol.FOURBYTES_PACKET;
00297             } else if (ret == 1) {
00298                 packet_size = IIIMProtocol.EIGHTBYTES_PACKET;
00299             }
00300             opCode = (c & 0x7f);
00301             
00302             int count = 0;
00303             if(packet_size == IIIMProtocol.FOURBYTES_PACKET) count = 4;
00304             else if(packet_size == IIIMProtocol.EIGHTBYTES_PACKET) count = 8;
00305             
00306             int ch[] = new int[count];
00307             // Because the first byte is packet_size and opcode,
00308             // this byte con't calculate for totoal length of protocol
00309             ch[0] = 0x00;
00310             
00311             ch[1] = in.read();
00312             // O byte protocol
00313             if(ch[1] == -1) {
00314                 packetLength = 0;
00315                 return;
00316             }
00317             
00318             for(int i=2;i<count;i++) {
00319                 ch[i] = in.read();
00320             }
00321             
00322             
00323             for(int i = 0 ;i < count; i++) {
00324                 packetLength += (ch[i] << ((count - i - 1) * 8));
00325             }
00326             packetLength = (packetLength << 2);
00327             
00328         } catch(IOException e) {
00329             debug("e = " + e);
00330         }
00331     }
00332     
00333     void write(IIIMPOutputStream out) {
00334         try {
00335             int length = ((packetLength / 4) & 0x00FFFFFF);   
00336             out.write(opCode & 0x7f);
00337             out.write((length >>> 16) & 0xFF);
00338             out.write((length >>> 8)  & 0xFF);
00339             out.write((length >>> 0)  & 0xFF);
00340                      
00341         } catch(IOException e) {
00342             debug("e = " + e);
00343         }
00344     }
00345 
00346     private void debug(String str) {
00347        if (Manager.DEBUG) {
00348            System.err.println(str);
00349        }
00350     }
00351 }
00352