Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
206 ligi 1
package org.ligi.ufo;
2
 
3
 
4
import java.io.*;
5
//import javax.microedition.io.*;
6
 
7
//#ifdef j2me
8
//# import javax.microedition.io.*;
9
//#endif
10
 
11
public class MKProxy
12
 implements  Runnable
13
{
14
 
15
    public boolean connected;
16
    public String url;
17
 
18
    public String err_str="none";
19
 
20
//#ifdef j2me
21
//#    StreamConnection connection;
22
//#endif
23
 
24
//#ifdef android
25
    java.net.Socket connection;
26
//#endif
27
 
28
    public java.io.InputStream  reader;    
29
     public java.io.OutputStream writer;    
30
 
31
    MKCommunicator mk;
32
 
33
 
34
    public MKProxy(MKCommunicator _mk)
35
    {
36
        mk=_mk;
37
        new Thread( this ).start(); // fire up main Thread 
38
    }
39
 
40
    public void connect(String url_)
41
    {
42
        url=url_;
43
 
44
        try
45
            {
46
                //
47
 
48
//#ifdef android
49
                connection = new java.net.Socket(url_,9876);
50
 
51
                reader=connection.getInputStream();
52
                writer=connection.getOutputStream();
53
//#endif
54
 
55
//#ifdef j2me
56
//#             connection = (StreamConnection) Connector.open(url, Connector.READ_WRITE);
57
//#             reader=connection.openInputStream();
58
//#             writer=connection.openOutputStream();
59
 
60
//#endif
61
 
62
                String init="new:foo bar\r\n";
63
                writer.write(init.getBytes());
64
                writer.flush();
65
                connected=true;
66
            }
67
 
68
        catch (Exception e)
69
            {
70
                //              err_str=e.toString();
71
                //              this=null;
72
 
73
                connected=false;
74
            }
75
 
76
    }
77
 
78
    public void write(byte[] input,int off,int len)
79
    {
80
        if (connected)
81
            try{ writer.write(input,off,len);
82
            writer.write(13);
83
            writer.flush();
84
            //      if (input==13) writer.flush();
85
 
86
            }
87
            catch(Exception e) { connected=false; }
88
    }
89
 
90
    public void sleep(int time)
91
    {
92
        try { Thread.sleep(time); }
93
        catch (Exception e)  {   }
94
    }
95
 
96
 
97
 
98
    public void run()
99
    {
100
 
101
        while(true)
102
            {
103
 
104
                try {
105
                if (connected)
106
                    {
107
                        byte[] data_in_buff=new byte[reader.available()];
108
                        int read_count =reader.read(data_in_buff,0,reader.available());
109
                        if (read_count>0) mk.write_raw(data_in_buff);
110
                        sleep(30);
111
                    }
112
                else
113
                    sleep(300);
114
                }
115
                catch ( Exception e){}
116
            }
117
 
118
    }
119
 
120
 
121
 
122
}