You are here: DEVPPL Forum Programming Java Forum
NOTIFICATIONS
54.100
MEMBERS
15.693
TOPICS
62.271
POSTS
  562
FLASH GAMES
7.740
TUTORIALS
 

Login

E-mail:
Password:

I used the producer and consumer rule here but still it is..

0

Loading

I used the producer and consumer rule here but still it is..

Postby Sasi » Tue Apr 24, 2007 8:29 am

Code: Select all
      public void run()
      {
      intSmsBuf=new Vector();
      try{
      this.sleep(10);}
      catch(Exception e)
      {
      }
      globalBuffer=new byte[512];
      // as for initial default case ate 1, the response is ate 0 OK.
      // it is set to ate 0 and the response is not considered
      //boolean echoFlag=false;
      boolean echoFlag=true;
      boolean newMsgFlag=false;
      openSerialPort();      
      // set command echo mode OFF
      sendEchoOff();
      
      // receive response for echo command
      //echoFlag=getEchoResponse();   reason mentioned when echo flag is declared and initialized   
      getEchoResponse();
      
      // stop receiving new message indications
      offNewMsgIndication();
      
      // get response for command : offNew MsgIndication
      newMsgFlag=getResForNewMsgIndication();
      
      // these two commands are send as startup commands to
      // modem and if it fails to respond to these commands
      // display the dialog " to connect the modem properly
      // and to restart the application
      if(echoFlag==true && newMsgFlag==true) {
         // get database connection
         con=DBOps.getConnection();
         try {
            stmt=con.createStatement();      
            System.out.println("Database connected");
         
           String query9 = "SELECT count(*) from new_sms_send";
           int rows=0;
                        ResultSet rs9= stmt.executeQuery(query9);
           if(rs9.next())
                           rows = rs9.getInt(1);
                     System.out.println("rows are:"+rows);
                  
                     SendMessage();
                            
                     ReceiveMessage();
                      }//end of try
                        catch(SQLException se) {
            se.printStackTrace();
            System.out.println(se.getMessage());
            JOptionPane.showMessageDialog(null,"Database connection failed:"+se.getMessage(),"Information",JOptionPane.INFORMATION_MESSAGE);                        
            System.exit(1);                        
         }
       }//end of if where port and database connection
                        else {
         JOptionPane.showMessageDialog(null,"Cannot start the application \n as there is no response from modem","Information",JOptionPane.INFORMATION_MESSAGE);                        
         System.exit(1);
                        }
         
                     
                     }//end of run
                   
         public void SendMessage()   {   
         //if(flag ==0){      
         try{                  
           ResultSet rs1;
           String query1 = "SELECT * FROM new_sms_send";
                          rs1 =  stmt.executeQuery(query1);
                          System.out.println("query executed");
          
                           while(rs1.next()){
                                      long mobileNum = rs1.getLong(1);
                                      String sndMsg = rs1.getString(2);
                                      String sender = rs1.getString(3);
                                      int att = rs1.getInt(4);
                            System.out.println("query executed:"+att);
                        String time = rs1.getString(5);
                        String  sndMsgs = sender+"\t\t\n " +"SENDER:\n"+sndMsg;
                       // boolean flag=true;
            if(att==0 )
                {
         

                     //for(int i=0;i<rows;i++)
                     //{
                     sendReply(mobileNum,sndMsgs);
                     boolean status=rcvSendStatus();
                     if(status==true)
                     {
                        System.out.println("Send Success");      
                          //Connection con = null;
                     try
                     {
                          //System.out.println("connecting to dbase");
                                         con = DBOps.getConnection();
                                stmt = con.createStatement();
                                         String query = "INSERT INTO sucessful_send " + "values ('"+mobileNum+"', '"+sender.trim()+"', '"+sndMsg.trim()+"','"+time+"')";
                System.out.println("pasted in suces send table");
                                         System.out.println("INSERT INTO sucessful_send " + "values ('"+mobileNum+"', '"+sender.trim()+"', '"+sndMsg.trim()+"','"+time+"')");
                                         stmt.executeUpdate(query);
               
                                    String query4 = "DELETE FROM new_sms_send WHERE send_time = '"+time+"'";
                                         stmt.executeQuery(query4);
                System.out.println("Deleted successful");
                                         System.out.println("DELETE FROM new_sms_send WHERE send_time = '"+time+"'");
               
                     }
                     catch(Exception e)
                     {
                     }
                     }//end of if for status
                     else
                        {
                        System.out.println("Unable to send the data");
                        }
                  //flag = false;   //}//end of for
                     }//end of if(att==0)
                     else
                     {
                     try{
               con = DBOps.getConnection();
                                stmt = con.createStatement();
                                         String query = "INSERT INTO unsusessful_send " + "values ('"+mobileNum+"', '"+sender.trim()+"', '"+sndMsg.trim()+"','"+time+"')";
                System.out.println("pasted in suces send table");
                                         System.out.println("INSERT INTO unsusessful_send " + "values ('"+mobileNum+"', '"+sender.trim()+"', '"+sndMsg.trim()+"','"+time+"')");
                                         stmt.executeUpdate(query);
               
                                    String query4 = "DELETE FROM new_sms_send WHERE send_time = '"+att+"'";
                                         stmt.executeQuery(query4);
                System.out.println("Deleted successful");
                                         System.out.println("DELETE FROM new_sms_send WHERE send_time = '"+att+"'");
                }
                catch(Exception e){
                }
                     
                      //continue;
                     }
                     break;
                     //continue;
                     }
                     }
                     catch(Exception e){
                     }//end of while(rs1)
                     
                     ReceiveMessage();
                     //flag = 1;
                     //break;
                     //}
                     }//end of send method.
                                       
                public void ReceiveMessage(){
      //if(flag ==1){
      while(true)
                            {   
            // if(flag ==false){   
            
                  //message sending part should be done then after database should be update like
                 // send request to get the maximum memory capacity
              sendReqForMaxLoc();   
                             System.out.println("max location");              
         
             // get maximum memory capacity   
              getResForMaxLoc();
              System.out.println("max location12");

             if(MAXLOC > 0 ) {                                    
            // timer for sending the reply
            int delay=1000;
            ActionListener actionListenerForReply=new ActionListener() {
               public void actionPerformed(ActionEvent ae) {
                  try {     
                      sleep(10);   
                  } catch(InterruptedException ie) {
                  }
                  // get msgs from each location ( ie for x locations)
                  for(int i=0;i<MAXLOC;i++) {
                     // send request to get the message or SMS in this case
                     sendReqForMsg(i+1);
                     // receive SMS
                     String rcvMsg=getResForMsg();
                     // parse the msg
                     //System.out.println("parsing started");
                     String[] parMsg=rcvMsg.split(",");
                     String msg=new String();
                     boolean unRead=false;
                     boolean validMsg=false;
                     // if the msg is not an error msg                                          
                     if(parMsg.length > 1) {
                        // if msg is NOT READ
                        if(parMsg[0].trim().equals("+CMGR: \"REC UNREAD\"")) {
                        //if(parMsg[0].trim().equals("+CMGR: \"REC READ\"")) {   
                           //System.out.println("parMsg:"+parMsg[0]);
                           unRead=true;
                        }   
                        
                        //msgType=INVALID;   
                        if(unRead==true) {
                           /*   0                  1            2   3           4        5  6 7 8     9           
                           +CMGR: "REC READ","+919866524337",,"05/08/11,19:10:32+08",145,4,0,0,"+9198450870
                                             012345678901234                                    
                                10   11
                           01",145,15
                           this 1234567890
                           OK */
                           byte crByte[]=new byte[1];
                           crByte[0]=13;
                           String cr=new String(crByte);   
                                    //System.out.println("data is:"+ crByte[0] + "new:" + cr.hashCode());
                           //System.out.println("parsing started2");   
                           //System.out.println("is that:"+parMsg[1]);
                           //System.out.println("is that:"+parMsg[2].hashCode());
                           //System.out.println("is that:"+parMsg[3]);
                           //System.out.println("is that:"+parMsg[4]);
                           //System.out.println("is that:"+parMsg[5].hashCode());
                           //String[] msgSubStr = null;
                           String[] msgSubStr=parMsg[11].split(cr);
                           //System.out.println("data is:"+parMsg[11].split(cr).length);
                           // get the actual msg from the SMS received
                           int msgLen=Integer.parseInt(msgSubStr[0]);
                           System.out.println("data:"+msgSubStr);
                           msg=parMsg[11].substring(3,3+msgLen);
                           //System.out.println("parsing started3"+msgLen);
                           // find the msg type
                           //msgType=getMsgType(msg);
                           System.out.println("msg:"+msg);
                           //System.out.println("msg type:"+msgType);                           
                        }                     
                        //if(unRead==true && msgType!=INVALID) {
                        if(unRead==true ) {                           
                           // if it is unread, store mobile number and
                           // msg in the object of RequestSMS  and store in internal buffer                           
                           reqSMS=new RequestSMS();
                           long mobileNo=Long.parseLong(parMsg[1].substring(4,14));
                           reqSMS.set(mobileNo,msg,parMsg[3]+" "+parMsg[4],false);                        
                           intSmsBuf.add(reqSMS);                        
                        }          
                        // delete SMS from SIM      
                        //deleteSMSFromSIM(i);
                     } // if msg.lENGTH>1

                  }// for < MAXLOC
                  // sort this internal buffer based on date and time
                  RequestSMS reqSMSi,reqSMSj;               
                  for(int i=0;i<intSmsBuf.size();i++) {
                     reqSMSi=(RequestSMS)intSmsBuf.get(i);
                     for(int j=0;j<intSmsBuf.size();j++) {
                        reqSMSj=(RequestSMS)intSmsBuf.get(j);                        
                        if(reqSMSi.dateTime.compareTo(reqSMSj.dateTime) > 0) {
                           intSmsBuf2=(Vector)intSmsBuf.clone();
                           intSmsBuf2.set(i,(RequestSMS)intSmsBuf.get(j));
                           intSmsBuf2.set(j,(RequestSMS)intSmsBuf.get(i));
                           intSmsBuf=(Vector)intSmsBuf2.clone();            
                        }
                     }                  
                  }//end for intsmsbuf   
                  // remove reply msgs from internal buffer which are
                  // successfully send
                  for(int i=0;i<intSmsBuf.size();i++) {
                     reqSMSi=(RequestSMS)intSmsBuf.get(i);                     
                     if(reqSMSi.sentStatus==true) {
                        intSmsBuf.removeElementAt(i);
                        i=i-1;
                     }                  
                  }//end of removebuffer   
            }//end of action performed
            };//end of actionlistener      
                timerTimeOut=new javax.swing.Timer(delay,actionListenerForReply);
            timerTimeOut.setRepeats(true);
            timerTimeOut.start();
         } //end of if for MAXLOC
         else {
            JOptionPane.showMessageDialog(null,"SIM has no memory support","Information",JOptionPane.INFORMATION_MESSAGE);                        
            System.exit(1);            
         }
            
               //flag=true;
               //}
               break;
                                        //continue;
               }//end of while(
               SendMessage();
               //flag = 0;
               //break;
               //}
               }//end of receiving


Hello everybody, in the above i used the producer and consumer technology but still it is not working, can anyone can help me please its urgent.

Main moto is: i have to send the sms's(from modem/application) and i have to receive the sms's(to modem/application) simultaneouslly but it is not working if at all the records are completed in the database then it is able to receive the message and then after i am updating the table then the application is not those values(means the application/run method is not again starting from start position)


regards,
sasi.
Sasi
 
Reputation: 0
Posts: 13
Joined: Mon Apr 23, 2007 2:10 pm
Highscores: 0
Arcade winning challenges: 0

I used the producer and consumer rule here but still it is.. - Sponsored results

Sponsored results

Login to get rid of ads

 

0

Loading

Postby Ggomez » Tue Apr 24, 2007 6:22 pm

MMM

the run methos wont loop automatically

you must do it with some while loop or something.

if it dont work, maybe some other loop or a break statement is bothering around.
Ggomez
 
Reputation: 0
Posts: 45
Joined: Tue Apr 17, 2007 4:30 am
Location: !!!!!MéXiCo¡¡¡¡¡¡
Highscores: 0
Arcade winning challenges: 0
^ Back to Top