home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / DirectX / dplay / dpchat.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  9.1 KB  |  450 lines

  1. // 
  2. //  (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  3. // 
  4. //  File:               dpchat.java
  5. //  Description:        DPlay Sample
  6. //
  7.  
  8. import com.ms.com.*;
  9. import com.ms.directX.*;
  10. import com.ms.ui.resource.*;
  11. import com.ms.ui.*;
  12. import java.awt.*;
  13. import SpDlg;
  14. import PlayDlg;
  15. import SessDlg;
  16. import SendBox;
  17.  
  18. class DpChat extends Panel implements  DirectXConstants {
  19.  
  20.     public static int    APPMSG_CHATSTRING=0x48;
  21.  
  22.     SendBox                m_SendBox;                            //Edit box for sending
  23.     TextArea            m_ReceiveBox;                        //Edit box to read coversation    
  24.     boolean                m_End=false;
  25.  
  26.     byte                m_MsgBuffer[] = {1};
  27.     int                    m_MsgBufferSize[]=new int[1];
  28.  
  29.     
  30.     public    DirectPlay2    m_DirectPlay2=null;            //DirectPlay variables
  31.     public    int            m_Dpid=-1;                    //
  32.     public _Guid        m_ApplicationGuid =null;    //
  33.     public _Guid        m_SessionGuid =null;        //
  34.     public _Guid        m_ServiceProviderGuid=null;    //
  35.     String                m_SessionName;
  36.     String                m_UserShortName;
  37.     String                m_UserLongName;
  38.     
  39.     SpDlg                spdlg;
  40.     SessDlg                sessdlg;
  41.     PlayDlg                playdlg;
  42.     //=-----------------------------
  43.     // DoChat
  44.     //=-----------------------------    
  45.     void DoChat(){
  46.         
  47.         initChatVars();
  48.  
  49.         boolean bInitDplay=false;
  50.         
  51.         spdlg=new SpDlg(this);
  52.         add(spdlg);        
  53.         spdlg.setBounds(0,30,500,80);        
  54.  
  55.         while (bInitDplay==false){
  56.             //-    Select Service Provider
  57.             //    fills m_ServiceProviderGuid                            
  58.             spdlg.reset();
  59.             spdlg.setEnabled(true);
  60.             spdlg.show();        
  61.             if ( spdlg.DoWait()<0){
  62.                 DoCloseAll();
  63.                 return;
  64.             }
  65.             spdlg.setEnabled(false);
  66.             
  67.             //- exit here if we are being closed down            
  68.             if (m_End!=false) {
  69.                 DoCloseAll();
  70.                 return;
  71.             }
  72.             
  73.             //- Create DirectPlay2
  74.             bInitDplay=true;
  75.             try {
  76.                 m_DirectPlay2=m_DirectPlay2.create(m_ServiceProviderGuid);
  77.             }
  78.             catch (ComException e){
  79.                 bInitDplay=false;
  80.             }
  81.             if (m_DirectPlay2==null){
  82.                 bInitDplay=false;
  83.             }
  84.         }
  85.     
  86.         
  87.         //- exit here if we are being closed down            
  88.         if (m_End!=false) {            
  89.             DoCloseAll();            
  90.             return;
  91.         }
  92.  
  93.         //- Have user Pick Session to join or
  94.         //  ask to create one.
  95.         //    fills m_SessionGuid
  96.         SessDlg sessdlg =new SessDlg(this);    
  97.         add(sessdlg);
  98.         sessdlg.setBounds(0,120,500,130);
  99.         sessdlg.show();
  100.         if ( sessdlg.DoWait()<0)    {
  101.             DoCloseAll();            
  102.             return;
  103.         }
  104.         sessdlg.setEnabled(false);
  105.         
  106.         //- exit here if we are being closed down            
  107.         if (m_End!=false) {
  108.             DoCloseAll();
  109.             return;
  110.         }
  111.         
  112.         //- Have use enter name
  113.         //  fills m_SessionName
  114.         //  fills m_UserShortName
  115.         //  fills m_UserLongName
  116.         PlayDlg playdlg =new PlayDlg(this);
  117.         add(playdlg);
  118.         playdlg.setBounds(0,300,500,100);        
  119.         playdlg.show();
  120.  
  121.         if ( playdlg.DoWait()<0){
  122.             DoCloseAll();
  123.             return;
  124.         }
  125.  
  126.         playdlg.setEnabled(false);
  127.  
  128.         //- exit here if we are being closed down            
  129.         if (m_End!=false) {
  130.             DoCloseAll();
  131.             return;
  132.         }
  133.  
  134.         //- call dplay2.open
  135.         //  call dplay2.createPlayer
  136.         DoOpenSessionAndCreateMember();
  137.  
  138.         this.remove(playdlg);
  139.         this.remove(sessdlg);
  140.         this.remove(spdlg);
  141.  
  142.         //- cleanup
  143.         DoDestroyForms();
  144.  
  145.  
  146.         //- exit here if we are being closed down            
  147.         if (m_End!=false) {
  148.             DoCloseAll();
  149.             return;
  150.         }
  151.  
  152.         //- init the chat boxes
  153.         initChatUI();        
  154.  
  155.         //- notify people that we have joined
  156.         m_SendBox.setText("new member joining the conversation");
  157.         m_SendBox.SendChatMessage();
  158.  
  159.         //- show our stuff
  160.         this.show();
  161.  
  162.         //- loop for messages
  163.         while (m_End==false){
  164.             DoReceive();
  165.             Thread.yield();            
  166.         }    
  167.  
  168.  
  169.         //- cleanup
  170.         DoCloseAll();
  171.         this.notify();
  172.     }
  173.  
  174.  
  175.     //=---------------------------------
  176.     // DoOpenSessionAndCreateMember
  177.     //=---------------------------------
  178.     // Open a new session with username
  179.     int    DoOpenSessionAndCreateMember(){        
  180.  
  181.         DPSessionDesc dpsess= new DPSessionDesc();        
  182.         String  password = null;
  183.         int        flags=0;
  184.  
  185.         if (m_SessionGuid==null){            
  186.             dpsess.flags=DPSESSION_MIGRATEHOST;// | DPSESSION_KEEPALIVE;    
  187.             dpsess.maxPlayers=100;
  188.             m_SessionGuid=new _Guid();
  189.             flags=DPOPEN_CREATE;
  190.         }
  191.         else{            
  192.             flags=DPOPEN_JOIN;
  193.         }
  194.     
  195.         //- open the session        
  196.         m_DirectPlay2.open(dpsess,m_SessionGuid,m_ApplicationGuid,m_SessionName, password, flags);
  197.                 
  198.         //- add us as a new member to that session                
  199.         m_Dpid=m_DirectPlay2.createPlayer(m_UserShortName,m_UserLongName, null,0);        
  200.  
  201.         return m_Dpid;
  202.     }
  203.  
  204.  
  205.     //=---------------------------------
  206.     // StopAll()
  207.     //=---------------------------------
  208.     synchronized void StopAll(){
  209.  
  210.         m_End=true;
  211.  
  212.         try{    spdlg.notify();    }
  213.         catch (Exception e){}
  214.  
  215.         try{    sessdlg.notify();}
  216.         catch (Exception e){}
  217.  
  218.         try{    playdlg.notify();}
  219.         catch (Exception e){}
  220.  
  221.             
  222.         //wait for cleanup to happen
  223.         try {
  224.             this.wait();
  225.         }
  226.         catch (Exception e){
  227.             //do nothing;
  228.         }
  229.     }
  230.     
  231.     //=---------------------------------
  232.     // DoCloseAll
  233.     //=---------------------------------    
  234.     synchronized int DoCloseAll(){
  235.         //make sure recieve loop falls through        
  236.         m_End=false;
  237.  
  238.         DoDestroyForms();
  239.  
  240.         
  241.             //- cleanup sendbox
  242.         if (m_SendBox!=null)
  243.             m_SendBox.cleanup();
  244.  
  245.         //- clean up dplay
  246.         if (m_DirectPlay2!=null){
  247.             if (m_Dpid!=-1){
  248.                 m_DirectPlay2.destroyPlayer(m_Dpid);
  249.             }
  250.             m_Dpid=-1;
  251.             
  252.             try {
  253.                 m_DirectPlay2.close();
  254.             }
  255.             catch (ComException e){
  256.                 System.out.println("Session close failed");
  257.             }
  258.  
  259.         }
  260.         m_DirectPlay2=null;
  261.         System.gc();
  262.         
  263.         this.notify();
  264.         return 0;    
  265.     }
  266.  
  267.  
  268.     //=---------------------------------
  269.     // DoDestroyForms
  270.     //=---------------------------------    
  271.     void DoDestroyForms(){
  272.  
  273.  
  274.         if (spdlg!=null){
  275.             spdlg.setVisible(false);
  276.             spdlg.cleanup();
  277.         }
  278.  
  279.         if (sessdlg!=null){
  280.             sessdlg.setVisible(false);    
  281.             sessdlg.cleanup();
  282.         }
  283.             
  284.         
  285.         if (playdlg!=null){
  286.             playdlg.setVisible(false);
  287.             playdlg.cleanup();
  288.         }
  289.  
  290.         try{    this.remove(spdlg);    }
  291.         catch (Exception e){}
  292.  
  293.         try{    this.remove(sessdlg);}
  294.         catch (Exception e){}
  295.  
  296.         try{    this.remove(playdlg);}
  297.         catch (Exception e){}
  298.  
  299.         spdlg=null;
  300.         sessdlg=null;
  301.         playdlg=null;
  302.  
  303.         System.gc();
  304.  
  305.     }
  306.     //=---------------------------------
  307.     // handleEvent
  308.     //=---------------------------------
  309.     public boolean handleEvent(Event evt){
  310.         if (evt.id==Event.WINDOW_DESTROY){
  311.             m_End=true;            
  312.         }
  313.         return super.handleEvent(evt);
  314.     }
  315.  
  316.  
  317.     //=---------------------------------
  318.     // initChatVars
  319.     //=---------------------------------
  320.     void initChatVars(){
  321.  
  322.         m_DirectPlay2= new DirectPlay2();
  323.         m_ApplicationGuid= new _Guid();        
  324.         m_ApplicationGuid.set(0xba542a0,    (short)0xe1ff,    (short)0x11cf,
  325.             (byte)0x9c,    (byte)0x4e,    (byte)0x0, (byte)0xa0,    (byte)0xc9,    (byte)0x5,(byte)0x42, (byte)0x5e);
  326.     }
  327.  
  328.     //=---------------------------------
  329.     // initChatUI
  330.     //=---------------------------------
  331.     void initChatUI(){    
  332.         
  333.         setLayout(null);
  334.         
  335.         m_SendBox= new SendBox(m_DirectPlay2,m_Dpid,m_UserShortName,this);
  336.         m_ReceiveBox = new TextArea();
  337.         
  338.         add(m_SendBox);
  339.         add(m_ReceiveBox);
  340.         
  341.         m_SendBox.setBounds(10,20,500,60);
  342.         m_ReceiveBox.setBounds(10,100,500,240);
  343.  
  344.     }
  345.  
  346.     //=---------------------------------
  347.     // setReceiveInfo
  348.     //=---------------------------------
  349.     synchronized public void setReceiveInfo(String s){
  350.         m_ReceiveBox.append(s);
  351.     }
  352.  
  353.     //=---------------------------------
  354.     // DoReceive
  355.     //=---------------------------------
  356.     int DoReceive()
  357.     {
  358.         
  359.         if (m_DirectPlay2==null) return 0;
  360.  
  361.         int                    idFrom=0;
  362.         int                    idTo=0;
  363.         int                    hr=0;                        
  364.         int[] fromPlayerId= new int[1];
  365.         int[] toPlayerId= new int[1];
  366.  
  367.         hr=0;
  368.         try {
  369.             boolean buffsizeloop = true;
  370.             while ( buffsizeloop )
  371.             {
  372.                 hr = m_DirectPlay2.receive(
  373.                     fromPlayerId,toPlayerId, 
  374.                     DPRECEIVE_ALL,    m_MsgBuffer,
  375.                     m_MsgBufferSize);
  376.  
  377.  
  378.  
  379.                 if ( hr == DPERR_BUFFERTOOSMALL )
  380.                 {                    
  381.                     m_MsgBuffer= new byte[m_MsgBufferSize[0]];                    
  382.  
  383.                     try {
  384.                         int recievesize = m_DirectPlay2.receiveSize(fromPlayerId,toPlayerId,DPRECEIVE_ALL);                        
  385.                     }
  386.                     catch (ComException e){
  387.                         System.out.println("recisveSize Exeception hr ="+ e.getHResult());
  388.                     }
  389.                     
  390.                 }
  391.                 else
  392.                     buffsizeloop = false;
  393.             }
  394.         }
  395.         catch (ComException e){
  396.             hr = e.getHResult();            
  397.         }
  398.  
  399.         idFrom=fromPlayerId[0];
  400.         idTo=toPlayerId[0];
  401.  
  402.  
  403.         if (hr>=0){
  404.             
  405.             //if we have a valid message do something
  406.             if (m_MsgBufferSize[0]>0){                
  407.                 if (idFrom == 0) //DPID_SYSMSG)
  408.                     DoSystemMessage(m_MsgBuffer, m_MsgBufferSize[0], fromPlayerId[0], toPlayerId[0]);
  409.                 else
  410.                     DoApplicationMessage(m_MsgBuffer, m_MsgBufferSize[0], fromPlayerId[0], toPlayerId[0]);
  411.             }
  412.         }
  413.         
  414.         return 0;
  415.     }
  416.  
  417.  
  418.     //=---------------------------------
  419.     // DoApplicationMessage
  420.     //=---------------------------------
  421.     void DoApplicationMessage(byte [] Msg, int msgSize, int idFrom, int idTo)
  422.     {        
  423.         String szStr="";
  424.     
  425.         for (int i=0;i<msgSize;i++){
  426.             char c=(char)Msg[i];
  427.             szStr=szStr+String.valueOf(c);
  428.         }
  429.         
  430.  
  431.         if (szStr!=null)
  432.             setReceiveInfo(szStr);
  433.         else
  434.             System.out.println("our string is null");
  435.                             
  436.         
  437.     }
  438.  
  439.     //=---------------------------------
  440.     // DoSystemMessage
  441.     //=---------------------------------
  442.     void DoSystemMessage(byte[] MsgBuff,int MsgSize, int idFrom, int idTo)
  443.     {    
  444.         //System.out.println("got SysMessage");        
  445.     }
  446.  
  447.  
  448.  
  449. }
  450.