Java Client functions
[bcusdk.git] / eibd / client / java / io.inc
blobb7b96f446301c4bd61b2754916501114e7f46328
1 /*
2     EIBD client library
3     Copyright (C) 2005-2007 Martin Koegler <mkoegler@auto.tuwien.ac.at>
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
10     In addition to the permissions in the GNU General Public License, 
11     you may link the compiled version of this file into combinations
12     with other programs, and distribute those combinations without any 
13     restriction coming from the use of this file. (The General Public 
14     License restrictions do apply in other respects; for example, they 
15     cover modification of the file, and distribution when not linked into 
16     a combine executable.)
18     This program is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21     GNU General Public License for more details.
23     You should have received a copy of the GNU General Public License
24     along with this program; if not, write to the Free Software
25     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28   private int readlen = 0;
29   private byte[] head = null;
30   private byte[] data = null;
32   private Socket con;
33   private InputStream is;
34   private OutputStream os;
36   public EIBConnection (String host) throws IOException
37   {
38     this (host, 6720);
39   }
41   public EIBConnection (String host, int port) throws IOException
42   {
43     con = new Socket (host, port);
44     is = con.getInputStream ();
45     os = con.getOutputStream ();
46   }
48   protected int _EIB_SendRequest (byte[]data) throws IOException
49   {
50     if (is == null)
51       throw new IOException ("connection closed");
52     byte len[] = new byte[2];
53     if (data.length > 0xffff || data.length < 2)
54       {
55         errno = EINVAL;
56         return -1;
57       }
58     len[0] = (byte) ((data.length >> 8) & 0xff);
59     len[1] = (byte) ((data.length) & 0xff);
60     os.write (len);
61     os.write (data);
62     return 0;
63   }
65   protected int _EIB_CheckRequest (boolean block) throws IOException
66   {
67     int res;
68     if (os == null)
69       throw new IOException ("connection closed");
71     if (readlen == 0)
72       head = new byte[2];
73     if (!block && is.available () < 1)
74         return 0;
75     if (readlen < 2)
76       {
77         res = is.read (head, readlen, 2 - readlen);
78         if (res == -1)
79           {
80             throw new IOException ("connection closed");
81           }
82         readlen += res;
83         if (readlen < 2)
84           return 0;
85         res = SHORT2INT (head[0], head[1]);
86         data = new byte[res];
87       }
88     if (!block && is.available () < 1)
89       return 0;
90     res = is.read (data, readlen - 2, data.length + 2 - readlen);
91     if (res == -1)
92       {
93         throw new IOException ("connection closed");
94       }
95     readlen += res;
96     return 0;
97   }
99   protected int _EIB_GetRequest () throws IOException
100   {
101     do
102       {
103         if (_EIB_CheckRequest (true) == -1)
104           return -1;
105       }
106     while (readlen < 2 || (readlen >= 2 && readlen < data.length + 2));
107     readlen = 0;
108     return 0;
109   }
111   public int EIB_Poll_Complete () throws IOException
112   {
113     if (_EIB_CheckRequest (false) == -1)
114       return -1;
115     if (readlen < 2 || (readlen >= 2 && readlen < data.length + 2))
116       return 0;
117     return 1;
118   }
120   public int EIBClose () throws IOException
121   {
122     if (con == null)
123       throw new IOException ("connection closed");
124       try
125     {
126       con.close ();
127     }
128     finally
129     {
130       is = null;
131       os = null;
132       con = null;
133     }
134     return 0;
135   }
137   public int EIBClose_sync () throws IOException
138   {
139     try
140     {
141       EIBReset ();
142     } catch (Exception e)
143     {
144     }
145     return EIBClose ();
146   }