Sending the Fax


The SendingFax.java file sends the fax to the number specified in the Fax Number text box of the Fax Application window.

Listing 6-10 shows the SendingFax.java file:

Listing 6-10: The SendingFax.java File

start example
 /*Imports required java.awt.image class*/ import java.awt.Image; /*Imports required java.i/o package classes*/ import java.io.*; /*Imports required java.text.DateFormat class*/ import java.text.DateFormat; /*  Imports required java.text.SimpleDateFormat class  */ import java.text.SimpleDateFormat; /*Imports required java.util.Calendar class*/ import java.util.Calendar; import java.util.Date; import javax.comm.*; /* class SendingFax - This class is used to send commands to modem to send fax Constructor:    SendingFax-This constructor creates GUI. Methods:    openForFax - This method is used to send fax. actionPerformed - This method describes which action will be performed on which component. main - This method creates the main window of the application and displays all the components.*/ class SendingFax {    /*Declare objects of String class.*/       public String flowcontrolin;       protected String modemresponse;       public String dialingmode;       public String noecho;       public String ownid;       public String logStr;       public String initialcommands[];       public String port;       public String initString;       public String faxFile;       public String lastError;       public String resetcommand;       public int flowcontrol;       public boolean bitorder;       public boolean modemFBOR;       public int faxclass;       public boolean log;       public boolean debug;       public PreparedFaxDoc producer;       protected int pages;       protected ImageToFaxEncoder encoder;       public PortFax listener;       protected boolean V27Supported;       protected boolean V29Supported;       protected boolean V17Supported;       public int MPSEOPdelay;       public int buffersize;       long startTime;       long tout;       /*       Declare objects of CommPortIdentifier class*/       CommPortIdentifier commportid;       /*Declare objects of SerialPort class*/       SerialPort serialPort;       /*Declare objects of OutputStream class*/       OutputStream outputStream;       /*Declare objects of InputStream class*/       InputStream inputStream;       protected byte reverseBytes[];       boolean isClass1;       boolean isClass2;       boolean isClass20;       protected static double TRAINING_SECONDS = 1.5D;       protected ModemCapabilities cap;       public int maxRetries;       protected static int brClass2_to_Class1[] = {24, 48, 72, 96, 121, 145};       protected static String brClass2_to_Class1V[] = {"V27", "V27", "V29", "V29", "V17", "V17"};        public int bitrate;        public int resolution;        public static final int RESOLUTION_NORMAL = 0;        public static final int RESOLUTION_FINE = 1;        public int timeout;        public int resetdelay;        public int lastresponse;        public int pagecode;        protected int postcode;        public int hangcode;        public SendingFax()       {             reverseBytes = new byte[256];             port = "COM1";             isClass1 = false;             isClass2 = false;             isClass20 = false;             initString = "ATV1Q0";             maxRetries = 3;             bitrate = 3;             resolution = 1;             faxFile = "tmpFax";             lastError = "";             resetcommand = "ATZ";             timeout = 30;             resetdelay = 0;             lastresponse = 0;             pagecode = -1;             postcode = -1;             hangcode = -1;             flowcontrol = 0;             modemresponse = "";             noecho = "ATE0";             flowcontrolin="";             bitorder = false;             modemFBOR = true;             dialingmode = "";             faxclass = 2;             ownid = "12345";             log = true;             debug = true;             logStr = "";             initialcommands = new String[0];             pages = 0;             encoder = null;             listener = null;             V27Supported = true;             V29Supported = true;             V17Supported = false;             MPSEOPdelay = 8;             buffersize = 0x3d090;             startTime = 0L;             tout = 0L;             /*             Initialize object of ImageToFaxEncoder class.             */             encoder = new ImageToFaxEncoder();             encoder.minBytesLine = 0;             for(int i = 0; i < 256; i++)            {                byte b = (byte)i;                byte bi = 0;                if((b & 0x80) > 0)                    bi |= 1;                if((b & 0x40) > 0                   bi |= 2;                if((b & 0x20) > 0)                    bi |= 4;                if((b & 0x10) > 0)                    bi |= 8;                if((b & 8) > 0)                    bi |= 0x10;                if((b & 4) > 0)                    bi |= 0x20;                if((b & 2) > 0)                    bi |= 0x40;                if((b & 1) > 0)                    bi |= 0x80;                reverseBytes[i] = bi;            }       }       /*       openForFax - This method is called to open and prepare a port to send fax.       Parameters: preparedfaxdoc - Object of PreparedFaxDoc class       Return Value:boolean       */       public boolean openForFax(PreparedFaxDoc preparedfaxdoc)       {       /*createFaxFiles method is called.*/            if(!createFaxFiles(preparedfaxdoc))            {                lastError = "Could not create fax files.";                return false;            }            if(listener != null)             listener.faxProgress(1, 0);             /*             Initialize object of CommPortIdentifier.             */            try            {                commportid = ommPortIdentifier.getPortIdentifier(port);            }            catch(Exception e)            {                System.err.println(e.getMessage());                lastError = "Could not find port ".concat(String.valueOf(String.valueOf(port)));                boolean flag = false;                return flag;         }         if(commportid == null)         {             lastError = "Could not find port ".concat(String.valueOf(String.valueOf(port)));             return false;         }         if(commportid.getPortType() != 1)         {             lastError = String.valueOf(String.valueOf(             (new StringBuffer("")).append(port).append(" is not a serial port")));             return false;         }         /*Open the serial port.*/         try         {             serialPort = (SerialPort)commportid.open("Fax Application", 2000);         }         catch(PortInUseException e)         {             System.err.println(e.getMessage());             lastError = "Could not open port ".concat(String.valueOf(String.valueOf(port)));             boolean flag1 = false;             return flag1;         }         /*Sets port receive time.*/         try         {             serialPort.enableReceiveTimeout(1000);         }         catch(Exception exception) { }      /*Gets input and output stream.*/         try         {             outputStream = serialPort.getOutputStream();             inputStream = serialPort.getInputStream();         }         catch(IOException e)         {             System.err.println(e.getMessage());             lastError = "Could not get streams";             boolean flag2 = false;             return flag2;         }      /*Sets serial port parameter.*/         try         {             serialPort.setSerialPortParams(19200, 8, 1, 0);         }         catch(UnsupportedCommOperationException e)         {             System.err.println(e.getMessage());             lastError = "Could not configure port";             boolean flag3 = false;             return flag3;         }        if(listener != null)        listener.faxProgress(2, 0);        /*Send reset command to modem.*/        if(!sendATCommand(resetcommand))        {             lastError = "Could not reset modem with ATZ";             return false;         }       if(resetdelay > 0)       /*softDelay method is called.*/       softDelay(resetdelay);       if(flowcontrol == 2 && flowcontrolin.length()>0)       sendATCommand(flowcontrolin);       if(flowcontrol == 1 &&   flowcontrolin.length()>0)       sendATCommand(flowcontrolin);       if(flowcontrol == 0 && flowcontrolin.length()>0)       sendATCommand(flowcontrolin);       try       {          if(flowcontrol == 2)             serialPort.setFlowControlMode(2);             if(faxclass != 1 && flowcontrol == 1)                serialPort.setFlowControlMode(8);             if(flowcontrol == 0)                  serialPort.setFlowControlMode(0);         }         catch(Exception e)         {             System.err.println(e.getMessage());         }         /*Add flow control type in log file.*/         if(log)         {             if((serialPort.getFlowControlMode() & 8) > 0) addLogLn("FLOWCONTROL_XONXOFF_OUT");             if((serialPort.getFlowControlMode() & 4) > 0) addLogLn("FLOWCONTROL_XONXOFF_IN");             if((serialPort.getFlowControlMode() & 1) > 0) addLogLn("FLOWCONTROL_RTSCTS_IN");             if((serialPort.getFlowControlMode() & 2) > 0) addLogLn("FLOWCONTROL_RTSCTS_OUT");         }         /*Send echo command to modem.*/         sendATCommand(noecho);         sendATCommand(initString);         /*queryCapabilities method is called.*/         queryCapabilities();         if(isClass1 && faxclass == 1)         {            /*             Send service class selection command             */             if(!sendATCommand("AT+FCLASS=1"))             {                 lastError = "Could not set class 1";                 return false;             }          /*queryBR method is called.*/             queryBR();         }         if(isClass2 && faxclass == 2)         {             /*             Send service class selection command             */             if(!sendATCommand("AT+FCLASS=2"))             {                 lastError = "Could not set class 2";                 return false;             }             if(debug)             /*Send HDLC frame tracing command.*/             sendATCommand("AT+FBUG=1");         }         if(isClass20 && faxclass == 20)         {             /*Send service class selection command*/             if(!sendATCommand("AT+FCLASS=2.0"))             {                lastError = "Could not set class 2.0";                return false;             }             sendATCommand("AT+FNR=1,1,1,0");             if(debug)                 sendATCommand("AT+FBU=1");         }         if(modemFBOR)         {             if(faxclass == 2)             {                if(!bitorder)                /*                The FBOR command determines the order in which data bits are transmitted                 between the DTE and the modem and between the modem and the PSTN.                 +FBOR=0 selects direct bit order where the first bit of a byte sent to the                 modem is the first bit sent down the PSTN.                 +FBOR=1 selects reverse bit order where the first bit of a byte sent to the                 modem is the last bit sent down the PSTN.                 */                sendATCommand("AT+FBOR=1");                if(bitorder)                sendATCommand("AT+FBOR=0");             }             if(faxclass == 20)             {                /*                Send command to setup data bit order.                */                if(!bitorder)                sendATCommand("AT+FBO=1");                if(bitorder)                sendATCommand("AT+FBO=0");             }         }         if(ownid.length() > 0)         {             /*Send Local ID string command.*/             if(faxclass == 20)             sendATCommand(String.valueOf(String.valueOf((new StringBuffer("AT+FLI=\"")).append(ownid).append("\""))));             if(faxclass == 2)             /*             This command is used to set the local identifying information stored within             the modem. This allows the remote system to identify the calling station.              The string may be up to 20 ASCII characters in length.               */sendATCommand(String.valueOf(String.valueOf(             (new StringBuffer("AT+FLID=\"")).append(ownid).append("\""))));         }          /*          Send command for modem capabilities parameters.             */          if(faxclass == 20)          sendATCommand(String.valueOf(String.valueOf(          (new StringBuffer("AT+FCC=")).append(resolution).append(",").append          (bitrate).append(",0,2,0,0,0,0"))));          if(faxclass == 2)          sendATCommand(String.valueOf(String.valueOf(          (new StringBuffer("AT+FDCC=")).append(resolution).append(",").append          (bitrate).append(",0,2,0,0,0,0"))));          for(int i = 0; i < initialcommands.length; i++)          {              System.out.println("");               sendATCommand(initialcommands[i]);          }          return true;     }    /*    createFaxFiles - This method is called to create image of editpane content.    Parameters: p - Object of PreparedFaxDoc class    Return Value:boolean    */    protected boolean createFaxFiles(PreparedFaxDoc p)     {       if(faxclass == 1)             bitorder = true;         if(p != null)             producer = p;         if(producer == null)             return false;         if(listener != null)       /*faxProgress method is called.*/             listener.faxProgress(6, 0);            pages = 0;            encoder.createEOP = faxclass == 20;         if(faxclass == 1)             encoder.createEOP = false;          Image page = null;       for(page = producer.getFaxPage(pages); page != null; page = producer.getFaxPage(pages))       {          byte pageBytes[] = encoder.encodeImage(page);          if(pageBytes != null)          {          /*Write string in the file.*/          writeFile(String.valueOf(String.valueOf(          (new StringBuffer(String.valueOf(String.valueOf(faxFile)))).append(".").append          (pages))), pageBytes, pageBytes.length);          pages++;          }       }       return true;     }    /*    setPortName - This method is called to set the port name.    Parameters: port - Object of String class    Return Value: NA    */     public void setPortName(String port)     {      this.port = port;     }    /*    setInitString - This method is called to set the modem initial string.    Parameters: s - Object of String class    Return Value: NA    */     public void setInitString(String s)     {      initString = s;     }    /*    writeFile - This method is called to write string in the file.    Parameters: f - Object of String class,b[] - array of bytes,count - value of integer type.    Return Value:boolean    */        protected boolean writeFile(String f, byte b[], int count)        {         try         {             FileOutputStream fo = new FileOutputStream(f);             fo.write(b, 0, count);             fo.close();         }         catch(Exception e)         {             System.err.println("Error writing to file             ".concat(String.valueOf(String.valueOf(e.getMessage()))));             boolean flag = false;             return flag;         }         return true;     }    /*    sendATCommand - This method is called to send the command to the  modem.    Parameters: messageString - Object of String class    Return Value:boolean    */    protected boolean sendATCommand(String messageString)    {        if(messageString.trim().length() == 0)        return true;        if(sendATCommandnoWait(messageString))        return waitFor("OK");        else        return false;     }    /*    sendATCommandnoWait - This method is called to send the wait command to the modem.    Parameters: messageString - Object of String class    Return Value:boolean    */    protected boolean sendATCommandnoWait(String messageString)     {         char cr = '\r';         try         {             if(log)             {                    addLogLn("");                    addLogLn("Command out.");             }             if(log)             addLog(messageString);             messageString = String.valueOf(messageString) + String.valueOf(cr);             /*Write output stream.*/             outputStream.write(messageString.getBytes());         }         catch(IOException e)         {             System.err.println(e.getMessage());                boolean flag = false;             return flag;         }         return true;      }       /*       addLogLn - This method is called to display the fax status.       Parameters: s - Object of String class       Return Value: NA       */        protected void addLogLn(String s)        {          logStr = String.valueOf(String.valueOf(          (new StringBuffer(String.valueOf(String.valueOf(logStr)))).append("\n").append(s)));          if(debug)          System.out.println(s);        }          /*          softDelay - This method is called to display the fax status.          Parameters: t - integer type variable.          Return Value: NA          */        protected void softDelay(int t)        {         try         {             Thread.currentThread();             Thread.sleep(t);         }         catch(Exception exception) { }     }    /*    queryCapabilities - This method is called to set the fax class.    Parameters: NA    Return Value: Nected void queryCapabilities()    {A    */    prot       sendATCommand("AT+FCLASS=?");       if(modemresponse.indexOf("1") > 0)          isClass1 = true;       if(modemresponse.indexOf("2") > 0)          isClass2 = true;       if(modemresponse.indexOf("2.0") > 0)          isClass20 = true;      }       /*       queryBR - This method is called to set the modem variables.       Parameters: NA       Return Value: NA       */        protected void queryBR()    {       if(isClass1)       {          sendATCommand("AT+FTM=?");          if(modemresponse.indexOf("48") > 0)             V27Supported = true;          if(modemresponse.indexOf("96") > 0)             V29Supported = true;          if(modemresponse.indexOf("97") > 0)             V17Supported = true;          if(modemresponse.indexOf("98") > 0)             V17Supported = true;          if(modemresponse.indexOf("145") > 0)             V17Supported = true;          if(modemresponse.indexOf("146") > 0)             V17Supported = true;       }    }    /*    waitFor - This method is used to invoke waitForOK method.    Parameters: resp - Object of String class.    Return Value:boolean    */     public boolean waitFor(String resp)     {       return waitForOK(resp, true);     }    /*    waitForOK - This method is used to invoke waitForOK method.    Parameters:  resp - object of String class, wOK - boolean type variable.    Return Value:boolean     */    protected boolean waitForOK(String resp, boolean wOK)     {         return waitForOK(resp, wOK, timeout);     }       protected void addLog(String s)        {            logStr = String.valueOf(logStr) + String.valueOf(s);            if(debug)            System.out.print(s);        }        /*        waitForOK - This method is called to call waitForOK method.        Parameters: resp - object of String class, wOK - boolean type variable, ptout - integer type variable.        Return Value:boolean        */        protected boolean waitForOK(String resp, boolean wOK, int ptout)        {            String r = "";            String line = "";            modemresponse = "";            Calendar cal = Calendar.getInstance();            long startTime = cal.getTime().getTime();            try            {                if(log)                {                       addLogLn("");                       addLog("Response: ");                }             int c = inputStream.read();             r = String.valueOf(r) + String.valueOf((char)c);             do             {                long now = Calendar.getInstance().getTime().getTime();                Calendar cale = Calendar.getInstance();                cale.add(6, 7);                String dateStr = (new SimpleDateFormat("ddmmyyyy")).format(cale.getTime());                if(startTime < now && now - startTime > (long)(ptout * 1000))                {                   timeout = 9;                   lastError = "Timeout waiting for response";                   boolean flag1 = false;                   return flag1;                 }                 if(c == 13)                 {                      if(line.indexOf("+FHNG:") >= 0 || line.indexOf("+FHS:") >= 0 ||                      line.indexOf("+FHG:") >= 0)                      hangcode = (new Integer(line.substring(line.indexOf(":") + 1,                      line.length()))).intValue();                      if(line.indexOf("+FPS:") >= 0 || line.indexOf("+FPTS:") >= 0)                      pagecode = (new Integer(line.substring(line.indexOf(":") + 1,                      line.length()))).intValue();                      if(line.indexOf("+FET:") >= 0)                      postcode = (new Integer(line.substring(line.indexOf(":") + 1,                      line.length()))).intValue();                      if(line.indexOf("+FDSC:") >= 0)                     {                         String capStr = line.substring(line.indexOf(":") + 1, line.length());                         ModemCapabilities tmpCap = new ModemCapabilities();                         tmpCap.decodeCapabilitiesClass2(capStr);                      }                      if(line.indexOf("+FCON") >= 0 || line.indexOf("+FCO") >= 0)                      lastresponse = 5;                      if(line.indexOf("CONNECT") >= 0 && faxclass == 1)                      lastresponse = 5;                      if(line.indexOf("ERROR") >= 0 || line.indexOf("FCERROR") >= 0)                      {                            lastresponse = 1;                               boolean flag2 = false;                            return flag2;                      }                      if(line.indexOf("BUSY") >= 0)                      {                            lastresponse = 8;                            lastError = "Line busy";                            boolean flag3 = false;                            return flag3;                      }                      if(line.indexOf("NO DIALTONE") >= 0)                      {                            lastresponse = 7;                            lastError = "No dial tone";                            boolean flag4 = false;                            return flag4;                       }                      if(line.indexOf("NO CARRIER") >= 0 && resp.compareTo("NO CARRIER") != 0)                      {                            lastresponse = 6;                            lastError = "No carrier";                            boolean flag5 = false;                            return flag5;                      }                       line = "";                 }                 if(log)                addLogCh(c);                if(c == 13 && r.indexOf(resp) >= 0 && (r.indexOf("OK") >= 0 || !wOK))                break;                if(c == 13 && r.indexOf("+FHNG") >= 0 && (r.indexOf("OK") >= 0 || !wOK))                 {                     boolean flag6 = false;                     return flag6;                 }                if(c == 13 && r.indexOf("+FHG") >= 0 && (r.indexOf("OK") >= 0 || !wOK))                 {                     boolean flag7 = false;                     return flag7;                 }                if(c == 13 && r.indexOf("+FHS") >= 0 && (r.indexOf("OK") >= 0 || !wOK))                 {                     boolean flag8 = false;                     return flag8;                 }                 if(r.indexOf("ERROR") >= 0)                 {                        boolean flag9 = false;                       return flag9;                 }                c = inputStream.read();                r = String.valueOf(r) + String.valueOf((char)c);                modemresponse = String.valueOf(modemresponse) + String.valueOf((char)c);                if(c != 10 && c != 13)                line = String.valueOf(line) + String.valueOf((char)c);             }              while(true);             if(log)                 addLogLn("");         }         catch(Exception e)         {             System.err.println(e.getMessage());             boolean flag = false;             return flag;         }         return true;     }    /*    addLogCh - This method is called to print log document.    Parameters: c - integer type variable.    Return Value: NA    */    protected void addLogCh(int c)        {            String cs = "";            if(c < 32)             cs = String.valueOf(String.valueOf((new StringBuffer("<")).append(c).append(">")));            else             cs = "".concat(String.valueOf(String.valueOf((char)c)));             logStr = String.valueOf(logStr) + String.valueOf(cs);            if(debug)             System.out.print(cs);        }        /*       close - This method is called to close input and output stream.       Parameters: NA       Return Value:boolean       */           public boolean close()        {            for(int c = 0; c < pages; c++)            {                File file;                try                {                   file = new File(String.valueOf(String.valueOf(                   (new StringBuffer(String.valueOf(String.valueOf(faxFile))))                   .append(".").append(c))));                }                catch(Exception exception) { }         }         try         {             inputStream.close();             outputStream.close();             serialPort.close();         }         catch(Exception e)         {             System.err.println(e.getMessage());             boolean flag = false;             return flag;         }         return true;     }    /*    sendFax - This method is called to send the fax    Parameters: destId - Object of String class.    Return Value:boolean     */        public boolean sendFax(String destId)        {         hangcode = -1;         boolean resendPage = false;         int retry = 0;         String r = "";         if(listener != null)             listener.faxProgress(3, 0);         if(connect(destId))         {             if(faxclass == 1)             {                 if(!phaseB())                     return false;                 if(!sendTSI())                     return false;             }             int pageCount = 0;             boolean retrain = true;             System.out.println("send");             do             {                 if(pageCount >= pages)                     break;                 if(listener != null)                listener.faxProgress(4, pageCount);                 boolean sendingUserFile = false;                 byte pageBytes[];                 if((new File("send.g3")).exists())                 {                   pageBytes = readFile("send.g3");                   sendingUserFile = true;                   System.out.println("** SENDING USER FILE *** send.g3");                 }                 else                 {                      pageBytes = readFile(String.valueOf(String.valueOf(                      (new StringBuffer(String.valueOf(String.valueOf(faxFile))))                      .append(".").append(pageCount))));                 }                 if(pageBytes == null)                 {                   lastError = "Could not read fax file.";                   return false;                 }                 if(faxclass == 1 && retrain)                 {                     if(!sendDCS())                         return false;                     if(!doTraining())                         return false;                 }                 retrain = false;                if((faxclass == 2 || faxclass == 20) && !sendATCommandnoWait("AT+FDT"))                {                   lastError = "Could not start phase C";                   return false;                 }                 if(faxclass == 20)                    waitForOK("CONNECT", false);                 if(faxclass == 2)                 {                      int previousFlowControl = serialPort.getFlowControlMode();                      try                        {                            serialPort.setFlowControlMode(0);                        }                         catch(Exception exception) { }                        try                        {                            if(log)                            addLog("Response: ");                            r = "";                            int c = inputStream.read();                            r = String.valueOf(r) + String.valueOf(c);                            do                            {                                if(c == 17)                                break;                                c = inputStream.read();                                if(log)                                addLogCh(c);                                r = String.valueOf(r) + String.valueOf(c);                                if(c == 13 && r.indexOf("OK") >= 0)                                break;                                if(c == 17 && log)                                addLogLn("XON received");                            }                             while(true);                            if(r.indexOf("+FHNG") >= 0)                            {                               lastresponse = 2;                               lastError = "Remote closed connection";                               boolean flag = false;                               return flag;                            }                            if(r.indexOf("+FHS") >= 0)                            {                               lastresponse = 2;                               lastError = "Remote closed connection";                               boolean flag1 = false;                               return flag1;                            }                        }                        catch(Exception e)                        {                            System.err.println(e.getMessage());                            lastError = "Error reading (wait for XON)";                            boolean flag2 = false;                            return flag2;                        }                        try                        {                            serialPort.setFlowControlMode(previousFlowControl);                        }                        catch(Exception exception1) { }                    }                    pagecode = -1;                    if(faxclass == 1)                   {                       if(!sendDataClass1(pageBytes, sendingUserFile))                      {                         lastError = "Could not send page bytes";                         return false;                        }                         byte eop[] = {0, 8, -128, 0, 8, -128, 0, 8, -128                      };                      sendBytes(eop, eop.length);                      byte rtc[] = {16, 3};                      sendBytes(rtc, rtc.length);                  }                   else                 if(!sendData(pageBytes))                 {                   lastError = "Could not send page bytes";                   return false;                 }                 if(faxclass == 20)                 {                     if(pageCount == pages - 1)                     {                         byte rtc[] = {16, 46};                         sendBytes(rtc, rtc.length);                     }                      else                     {                         byte rtc[] = {16, 44};                         sendBytes(rtc, rtc.length);                     }                     sendATCommand("AT+FPS?");                 }                 if(faxclass == 2)                 {                     byte rtc[] = {16, 3};                     sendBytes(rtc, rtc.length);                     try                     {                         Thread.currentThread();                         Thread.sleep(500L);                     }                     catch(Exception exception2) { }                     if(pageCount == pages - 1)                         sendATCommand("AT+FET=2");                     else                         sendATCommand("AT+FET=0");                     if(pagecode == -1)                         sendATCommand("AT+FPTS?");                 }                 resendPage = false;                 if(faxclass == 1)                 {                   if(!sendATCommand("AT+FTS=".concat(String.valueOf(String.valueOf(MPSEOPdelay)))))                      return false;                      int PPMretry = 1;                      LCFrame rsp = null;                      do                      {                         if(PPMretry > 3)                            break;                            if(!sendClass1FET(pageCount == pages - 1))                            return false;                            if(!sendATCommandnoWait("AT+FRH=3"))                            return false;                            rsp = waitForFrame(3000);                            if(rsp != null)                            break;                            lastError = "";                            PPMretry++;                        }                       while(true);                      if(rsp == null)                      {                            lastError = "Response to MPS/EOP not received after 3 retries.";                            return false;                        }                        if(rsp.getFrameType() == 44)                        {                            lastError = "Procedure interrupted";                            return false;                        }                        if(rsp.getFrameType() == 172)                        {                            lastError = "Procedure interrupted";                            return false;                        }                        if(rsp.getFrameType() == 140 && log)                        addLogLn("Positive message confirmation.");                        if(pageCount == pages - 1 && rsp.getFrameType() == 204)                        {                            retrain = true;                            if(log)                            addLogLn("Retrain positive.");                        }                        if(rsp.getFrameType() == 76)                        {                               retrain = true;                            if(retry < maxRetries)                            {                                  resendPage = true;                                  retry++;                            }                        }                        if(rsp.getFrameType() == 250)                        {                            lastError = "Disconnect frame received";                            hangcode = 20;                            break;                        }                    }                             if(faxclass == 2 || faxclass == 20)                    switch(pagecode)                    {                       case -1:                        case 0:                        case 1:                        default:                       break;                       case 2:                        if(retry < maxRetries)                       {                           resendPage = true;                           retry++;                       }                       break;                   }                   if(!resendPage)                   pageCount++;                }                 while(hangcode < 0);                if(listener != null)                listener.faxProgress(5, 0);                if(faxclass == 1 && sendClass1Disconnect())                hangcode = 0;                hangup();                if(hangcode == 0)                return true;            }            return false;        }       /*       connect - This method is called to connect the modem.       Parameters: destId - Object of String class.       Return Value:boolean       */          protected boolean connect(String destId)        {          String dialMode = "P";          if(dialingmode=="Tone")             dialMode = "T";          if(faxclass == 1)          {              if(!sendATCommandnoWait(String.valueOf(String.valueOf(             (new StringBuffer("ATD")).append(dialMode).append(destId)))))             return false;             waitForOK("CONNECT", false);          }           else          if(!sendATCommand(String.valueOf(String.valueOf(          (new StringBuffer("ATD")).append(dialMode).append(destId)))))          return false;          return lastresponse == 5;        }        protected boolean phaseB()        {            LCFrame f = null;            boolean haveDIS = false;            do            {                f = waitForFrame(10000);                if(f == null)                       return false;                if(!waitForOK("OK", false))                       return false;                if(f.getFrameType() == 128)                {                       haveDIS = true;                      cap = new ModemCapabilities();                       byte capb[] = f.getData();                      for(int i1 = 0; i1 < capb.length; i1++)                       {                           int p = capb[i1];                           if(p < 0)                           p = 256 + p;                           capb[i1] = reverseBytes[p];                       }                      cap.decodeCapabilities(capb);                }                if(f.isLast())                       break;                sendATCommandnoWait("AT+FRH=3");               }                 while(true);               if(!haveDIS)               {                lastError = "DIS not received";                return false;            }            if(!cap.t4)           {                lastError = "Receiver does not support T.4";                return false;            } else            {                return true;            }        }       /*       readFile - This method is called to read from file.       Parameters: f - Object of String class.       Return Value: byte       */          protected byte[] readFile(String f)        {            byte b[] = null;         try         {             FileInputStream fi = new FileInputStream(f);             b = new byte[(int)(new File(f)).length()];             fi.read(b, 0, b.length);             fi.close();         }         catch(Exception e)         {            System.err.println(String.valueOf(String.valueOf(            (new StringBuffer("Read file ")).append(f).append(" ").append(e.getMessage()))));             byte abyte0[] = null;             return abyte0;         }         return b;     }    /*    readFile - This method is called to test bit rate.    Parameters: NA    Return Value:boolean    */        protected boolean doTraining()     {      boolean reTry = true;      LCFrame f = null;      while(reTry)       {          sendTraining();          reTry = false;          sendATCommand("AT+FTS=1");          sendATCommandnoWait("AT+FRH=3");          do          {              f = waitForFrame(30000);              if(f == null)                  return false;          } while(!f.isLast());          if(f.getFrameType() == 68)          {             lastError = "Training failed at speed ".concat(String.valueOf(String.valueOf(cap.rate)));                 reTry = true;                 int newRate2 = 1;                 if(bitrate == 5)                newRate2 = 3;                 if(bitrate == 4)                newRate2 = 3;                 if(bitrate == 3)                newRate2 = 1;                 if(bitrate == 2)                newRate2 = 1;                 if(bitrate == 1)                newRate2 = 0;                 if(bitrate == 0)                reTry = false;                 if(reTry)                 {                     bitrate = newRate2;                     lastError = "";                 }                 sendClass1FTH(3);                 if(!sendDCS())                     return false;                }            }            if(f != null && f.getFrameType() != 132)            {               lastError = "Confirmation to receive excepted.";               return false;         }         return waitForOK("OK", false);     }    /*    sendDataClass1 - This method is called to fragment the data.    Parameters: b - array of byte type, len - variable of integer type,     ignoreLineScan - variable of boolean type.    Return Value:boolean     */          protected boolean sendDataClass1(byte b[], boolean ignoreLineScan)        {         int count = 0;         int totalCount = 0;         int len = b.length;         byte chunk[] = new byte[buffersize];         int bytesLine = 0;         int minBytesLine = 0;         int lineNumber = 0;         String logLin = "Line: ";         boolean error = false;         if(!ignoreLineScan && cap.scanTime > 0)         {             double scanTimeSecs = (double)cap.scanTime / (double)1000;             minBytesLine = (int)((double)((cap.rate * 100) / 8) * scanTimeSecs);         }         if(log)             addLogLn(String.valueOf(String.valueOf((new StringBuffer             ("Number of byte/line ")).append(minBytesLine).append             (" (").append(cap.scanTime).append(" ms )"))));            if(!sendClass1FTM(cap.rate))                return false;            try            {                if(flowcontrol == 1)                serialPort.setFlowControlMode(8);            }            catch(Exception exception) { }            try            {                bytesLine = 0;                do                {                   if(totalCount >= b.length || error)                     break;                    count = 0;                    do                    {                      if(count >= buffersize || totalCount >= b.length || error)                      break;                     byte dataByte = 0;                     byte nextByte = 0;                     boolean EOL = false;                      if(totalCount < b.length - 1)                      nextByte = b[totalCount + 1];                      if((b[totalCount] & 7) == 0 && nextByte == 1)                      EOL = true;                     if(bitorder)                     {                         int p = b[totalCount++];                        if(p < 0)                         p = 256 + p;                         dataByte = reverseBytes[p];                     }                      else                     {                         dataByte = b[totalCount++];                     }                         if(dataByte == 16)                         chunk[count++] = 16;                         chunk[count++] = dataByte;                         bytesLine++;                         if(EOL && lineNumber > 0 && bytesLine < minBytesLine)                         for(; bytesLine < minBytesLine; bytesLine++)                            chunk[count++] = 0;                         if(EOL)                         {                             if(bitorder)                            chunk[count++] = -128;                               else                               chunk[count++] = 1;                               totalCount++;                               lineNumber++;                               bytesLine = 0;                         }                    } while(true);                    if(!sendBytes(chunk, count))                        error = true;                   } while(true);               }               catch(Exception e)               {                  System.err.println(e.getMessage());               }               try               {                   if(flowcontrol == 1)                   serialPort.setFlowControlMode(0);               }               catch(Exception exception1) { }               return !error;           }          /*          sendBytes - This method is called to send output bits.          Parameters: b - array of byte type, len - variable of integer type.          Return Value:boolean          */             protected boolean sendBytes(byte b[], int len)          {          try            {                addLog(String.valueOf(String.valueOf((new StringBuffer                ("Send ")).append(len).append(" bytes  ... "))));                outputStream.write(b, 0, len);                addLogLn("OK");            }            catch(IOException e)            {                System.err.println(e.getMessage());                boolean flag = false;                return flag;            }            return true;        }       /*       sendData - This method is called to invoke sendBytes method.       Parameters: b - array of byte type       Return Value:boolean       */          protected boolean sendData(byte b[])       {          int count = 0;            int totalCount = 0;            int len = b.length;            byte chunk[] = new byte[65];            try            {                while(totalCount < b.length)                 {                   for(count = 0; count < 64 && totalCount < b.length;)                   {                         byte dataByte = 0;                         if(bitorder)                         {                            int p = b[totalCount++];                            if(p < 0)                            p = 256 + p;                            dataByte = reverseBytes[p];                        }                          else                         {                            dataByte = b[totalCount++];                         }                         if(dataByte == 16)                         chunk[count++] = 16;                         chunk[count++] = dataByte;                       }                       if(!sendBytes(chunk, count))                       {                          boolean flag = false;                          return flag;                      }                      if(!log);                   }               }               catch(Exception e)               {                   System.err.println(e.getMessage());               }               return true;           }          /*          waitForFrame - This method is called to receive frames.          Parameters: tiout - array of byte type          Return Value: LCFrame           */             protected LCFrame waitForFrame(int tiout)           {               Calendar cal = Calendar.getInstance();               long startTime = cal.getTime().getTime();               long startFrameTime = 0L;               String line = "";               boolean dle = false;               boolean started = false;               byte bytes[] = new byte[1024];               int byteCount = 0;               try               {                   if(log)                      addLog("Wait for frame: ");                   int c = inputStream.read();                do                {                   long now = Calendar.getInstance().getTime().getTime();                   if(now - startTime > (long)tiout)                {                   cancelReception();                   lastresponse = 9;                   lastError = "Timeout waiting for frame";                   LCFrame hdlcframe = null;                   return hdlcframe;                 }                 if(c == 13)                 {                     if(line.indexOf("ERROR") >= 0)                     {                         lastresponse = 1;                         LCFrame hdlcframe1 = null;                         return hdlcframe1;                     }                     if(line.indexOf("FCERROR") >= 0)                     {                         lastresponse = 1;                         LCFrame hdlcframe2 = null;                         return hdlcframe2;                     }                     if(line.indexOf("OK") >= 0)                     {                         lastresponse = 10;                         LCFrame hdlcframe3 = null;                         return hdlcframe3;                     }                     line = "";                 }                 if(log)                addLog(" ".concat(String.valueOf(String.valueOf(Integer.toHexString(c)))));                 if(c == 255 && !started)                 {                      started = true;                      startFrameTime = cal.getTime().getTime();                 }                 if(started)                 if(!dle)                 {                      if(c == 16)                      dle = true;                      else                      bytes[byteCount++] = (byte)c;                  }                   else                  {                      if(c == 16)                      {                         dle = false;                         bytes[byteCount++] = (byte)c;                         }                         if(c == 3)                             break;                     }                      c = inputStream.read();                       if(c != 10 && c != 13)                      line = String.valueOf(line) + String.valueOf((char)c);                  } while(true);               }               catch(Exception e)               {               System.err.println(e.getMessage());               LCFrame hdlcframe4 = null;               return hdlcframe4;            }            if(byteCount == 0)                return null;            else             return new LCFrame(bytes, byteCount);     }    protected boolean sendClass1Disconnect()    {       if(!sendClass1FTH(3))       return false;       LCFrame DSCN = new LCFrame();       DSCN.setLast(true);       DSCN.setFrameType((byte)-5);       if(!sendFrame(DSCN))       return false;       return waitForOK("OK", false);    }    protected boolean sendClass1FET(boolean lastPage)    {       if(!sendClass1FTH(3, 3))             return false;        LCFrame FET = new LCFrame();        FET.setLast(true);        if(lastPage)           FET.setFrameType((byte)47);        else             FET.setFrameType((byte)79);        if(!sendFrame(FET))             return false;        return waitForOK("OK", false);     }    protected boolean sendTSI()    {     if(!sendClass1FTH(3))          return false;        LCFrame TSI = new LCFrame();        TSI.setLast(false);        TSI.setFrameType((byte)67);        for(int i = ownid.length() - 1; i >= 0; i--)           TSI.addByte((byte)ownid.charAt(i));        for(int i = 0; i < 20 - ownid.length(); i++)             TSI.addByte((byte)32);        if(!sendFrame(TSI))             return false;        return waitForOK("CONNECT", false);     }     protected boolean sendDCS()     {         LCFrame DCS = new LCFrame();         DCS.setLast(true);         DCS.setFrameType((byte)-125);         minCapabilities(cap);         byte bcap[] = cap.encodeCapabilities();         for(int i1 = 0; i1 < bcap.length; i1++)         {             int p = bcap[i1];             if(p < 0)                 p = 256 + p;             bcap[i1] = reverseBytes[p];         }         DCS.addByte(bcap[0]);         DCS.addByte(bcap[1]);         DCS.addByte(bcap[2]);         if(!sendFrame(DCS))             return false;         return waitForOK("OK", false);     }    /*    hangup - This method is called to hang-up the modem, killing the dial tone.    Parameters: NA    Return Value: NA    */       protected void hangup()        {          sendATCommand("ATH");        }    /*    sendTraining - This method is called to set flow control of modem and invoke sendBytes method.    Parameters: NA    Return Value:boolean    */       protected boolean sendTraining()    {       /*        Terminates transmission and waits for 7*10ms interval before responding with OK.       ERROR is issued if the modem is on-hook.       */       sendATCommand("AT+FTS=7");       if(!sendClass1FTM(cap.rate))          return false;            try            {                if(flowcontrol == 1)                serialPort.setFlowControlMode(8);           }           catch(Exception exception) { }           byte b[] = new byte[(int)((double)((cap.rate * 100) / 8) * TRAINING_SECONDS) + 1];           for(int i = 0; i < b.length; i++)             b[i] = 0;          b[b.length - 1] = 1;          sendBytes(b, b.length);          byte b1[] = {16, 3};          sendBytes(b1, b1.length);          try          {              if(flowcontrol == 1)             serialPort.setFlowControlMode(0);            }            catch(Exception exception1) { }            return waitFor("OK");        }       protected boolean sendClass1FTH(int rate)        {          return sendClass1FTH(rate, timeout);        }       /*       sendClass1FTM - This method is called to set transmits data using        HDLC protocol and the defined modulation.       Parameters: rate - Variable of integer type, pout - variable of integer type.       Return Value:boolean       */          protected boolean sendClass1FTH(int rate, int pout)       {          String fth = "AT+FTH=".concat(String.valueOf(String.valueOf(rate)));          int retry = 0;          do          {             if(retry >= 3)             break;             if(!sendATCommandnoWait(fth))             return false;             if(waitForOK("CONNECT", false, pout))             break;             retry++;          }           while(true);          return retry < 3;     }    /*    sendClass1FTM - This method is called to set transmits data according to the defined modulation    Parameters: rate - Variable of integer type.    Return Value:boolean    */       protected boolean sendClass1FTM(int rate)        {          String ftm = "AT+FTM=".concat(String.valueOf(String.valueOf(rate)));          if(!sendATCommandnoWait(ftm))          return false;          return waitForOK("CONNECT", false);        }        protected void cancelReception()        {         if(log)             addLogLn("Cancel: ");            byte cancelByte[] = {24};            sendBytes(cancelByte, 1);            waitFor("OK");     }    /*    sendFrame - This method is called to send frames.    Parameters: f - object of LCFrame class.    Return Value:boolean    */       protected boolean sendFrame(LCFrame f)     {         byte b[] = f.getRawData();         if(log)         {             addLogLn("");             addLog("Send frame: ");             for(int i = 0; i < b.length; i++)             addLog(" ".concat(String.valueOf(String.valueOf(Integer.toHexString(b[i] & 0xff)))));          }                  startTimer(2500);          if(!sendBytes(b, b.length))                      return false;                     byte endOfFrame[] = {16, 3};                     if(!sendBytes(endOfFrame, 2))                      return false;                     if(timeout())                     {                         lastError = "timeout sending frame.";                         return false;                     }                      else                     {                         return true;                     }                 }                /*                minCapabilities - This method is called to set modem capabilities.                Parameters: cap - object of ModemCapabilities class.                Return Value: NA                */                   protected void minCapabilities(ModemCapabilities cap)                {                   int desiredRate = brClass2_to_Class1[bitrate];                  if(desiredRate < cap.rate)                  {                      cap.rate = desiredRate;                      cap.vRate = brClass2_to_Class1V[bitrate];                  }                  if(cap.resolution == 1 && resolution == 0)                      cap.resolution = 0;                  if(encoder.lineWidth < cap.width)                      cap.width = encoder.lineWidth;                  cap.huffman = 1;              }             /*             startTimer - This method is called to set starting time.             Parameters: t - Integer type variable.             Return Value: NA             */                protected void startTimer(int t)             {               tout = t;               Calendar cal = Calendar.getInstance();               startTime = cal.getTime().getTime();             }             /*             timeout - This method is called to check time out.             Parameters: NA             Return Value:boolean             */                protected boolean timeout()             {                long now = Calendar.getInstance().getTime().getTime();                return now - startTime > tout * (long)1000;              } } 
end example

Download this listing.

In the above listing, the SendingFax class sends a fax to the fax number of the destination. The various methods defined in the listing are:

  • addLogLn():dds a line that depicts the status of the Fax application in a log file.

  • addLogCh():Adds a character that depicts the status of the modem to the log file.

  • readFile():Reads the log file that contains status information about the modem.

  • openForFax():Opens the selected port to send the fax.

  • createFaxFiles():Creates an image for the file that is open in the edit pane of the Fax Application window.

  • setPortName():Sets the name of the port that sends the fax.

  • setInitString():Sets the initial string for the modem.

  • writeFile():Writes the file that is open in the edit pane of the Fax Application window to a temporary file on a physical drive. The createFaxFiles() method invokes the writeFile() method to write the file.

  • sendATCommand():Sends the AT command to the modem. The AT command checks if the modem is ready to send the fax.

  • sendATCommandnoWait():Sends the wait command to the modem.

  • softDelay(): Suspends the processing of the main thread for a specified period to display the status of the Fax application. The end user specifies the duration of the delay.

  • queryCapabilities():Retrieves the encoding and decoding capabilities of the modem.

  • waitFor():Invokes the waitForOK() method.

  • waitForOK():Waits for the OK command that the modem sends if the modem is ready for communication.

  • close():Closes the input and output streams of the Fax application.

  • sendFax():Sends the fax document to the recipient number specified in the Fax Number text box of the Fax Application window.

  • connect():Connects to the modem.

  • sendDataClass1():Sends the fax document if the modem used is of Class 1 type. The three different classes of modems are Class 1, Class 2, and Class 2.0.

  • sendBytes():Helps the modem send the fax document in the form of bytes to the recipient.

  • sendData():Invokes the sendBytes() method to send the fax document.

  • waitForFrame():Sets the delay time between two frames.

  • sendClass1Disconnect(): Disconnects a Class 1 modem.

  • sendClass1FET():Sets the number of bytes in a frame.

  • sendTSI():Sends the frames of the fax document to the Transmit Station ID (TSI) modem and waits for a response from the modem.

  • sendDCS():Determines the type of modem, and sends the frames of the fax document to the modem if the modem type is Digital Command Signal (DCS).

  • hangup():Disconnects the modem.

  • sendTraining():Sets the flow control property of the modem and invokes the sendBytes() method.

  • sendClass1FTH():Transmits data using the High-Level Data Link Control (HDLC) protocol and the defined modulation.

  • sendClass1FTM():Transmits data according to the defined modulation.

  • cancelReception():Sends the cancel command to the modem.

  • sendFrame():Sends the frames of the fax document to the recipient.

  • minCapabilities():Retrieves the minimum encoding and decoding capabilities of the modem.

  • startTimer():Starts a timer for the Fax application.

  • timeout():Closes the connection if the recipient does not respond for a specific period.




Developing Applications Using JCA
Developing Applications Using JCA
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 43

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net