Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/open3sp/agent/system/SystemAgentClient.java


1   /*
2    *  Open3SP - Framework Server
3    *
4    *  Copyright (C) 2003 3SP LTD. All Rights Reserved
5    *
6    *  This program is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU General Public License
8    *  as published by the Free Software Foundation; either version 2 of
9    *  the License, or (at your option) any later version.
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public
16   *  License along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18   */
19  package org.open3sp.agent.system;
20  
21  import java.io.DataInputStream;
22  import java.io.DataOutputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.OutputStream;
26  
27  
28  public class SystemAgentClient extends SystemAgentBase {
29      private DataInputStream in;
30      private DataOutputStream out;
31      private boolean running = true;
32  
33      public SystemAgentClient(InputStream input, OutputStream output)
34          throws IOException {
35          in = new DataInputStream(input);
36          out = new DataOutputStream(output);
37  
38          int msgid = in.readInt();
39  
40          if (msgid != AGENT_INIT) {
41              throw new IOException(
42                  "Failed to read AGENT_INSTALLER_INIT from stream");
43          }
44  
45          out.writeInt(AGENT_INIT);
46          out.flush();
47  
48          if (in.readInt() != AGENT_STATUS_OK) {
49              throw new IOException("Failed to initiate the agent installation");
50          }
51      }
52  
53      public String getProperty(String name) throws IOException {
54          out.writeInt(AGENT_GET_PROPERTY);
55          out.writeUTF(name);
56          out.flush();
57  
58          int msgid = in.readInt();
59  
60          if (msgid == AGENT_STATUS_OK) {
61              return in.readUTF();
62          } else if (msgid == AGENT_STATUS_ERROR) {
63              return null;
64          } else {
65              throw new IOException("Unexpected msgid " + String.valueOf(msgid));
66          }
67      }
68  
69      public void makeDirectory(String dir) throws IOException {
70          out.writeInt(AGENT_MAKE_DIRECTORY);
71          out.writeUTF(dir);
72          out.flush();
73  
74          int msgid = in.readInt();
75  
76          if (msgid == AGENT_STATUS_OK) {
77              return;
78          } else {
79              throw new IOException("Unexpected msgid " + String.valueOf(msgid));
80          }
81      }
82  
83      public boolean doesExist(String file) throws IOException {
84          out.writeInt(AGENT_FILE_EXISTS);
85          out.writeUTF(file);
86          out.flush();
87  
88          int msgid = in.readInt();
89  
90          if (msgid == AGENT_STATUS_OK) {
91              return in.readBoolean();
92          } else {
93              throw new IOException(
94                  "Unexpected error status returned from AGENT_FILE_EXISTS");
95          }
96      }
97  
98      public boolean isDirectory(String file) throws IOException {
99          out.writeInt(AGENT_IS_DIRECTORY);
100         out.writeUTF(file);
101         out.flush();
102 
103         int msgid = in.readInt();
104 
105         if (msgid == AGENT_STATUS_OK) {
106             return in.readBoolean();
107         } else {
108             return false;
109         }
110     }
111 
112     public void uploadFile(InputStream fin, String destination)
113         throws IOException {
114         out.writeInt(AGENT_OPEN_FILE);
115         out.writeUTF(destination);
116         out.writeInt(OPEN_FILE_WRITE);
117         out.flush();
118 
119         int msgid = in.readInt();
120 
121         if (msgid == AGENT_STATUS_OK) {
122             String key = in.readUTF();
123             byte[] buf = new byte[8096];
124             int read;
125 
126             while ((read = fin.read(buf)) > -1) {
127                 if (read > 0) {
128                     out.writeInt(AGENT_WRITE_FILE);
129                     out.writeUTF(key);
130                     out.writeInt(read);
131                     out.write(buf, 0, read);
132                     out.flush();
133 
134                     msgid = in.readInt();
135 
136                     if (msgid != AGENT_STATUS_OK) {
137                         in.readInt();
138 
139                         String msg = in.readUTF();
140                         throw new IOException(msg);
141                     }
142                 }
143             }
144 
145             fin.close();
146             out.writeInt(AGENT_CLOSE_FILE);
147             out.writeUTF(key);
148             out.flush();
149             msgid = in.readInt();
150 
151             if (msgid != AGENT_STATUS_OK) {
152                 in.readInt();
153 
154                 String msg = in.readUTF();
155                 throw new IOException(msg);
156             }
157         } else {
158             in.readInt();
159 
160             String msg = in.readUTF();
161             throw new IOException(msg);
162         }
163     }
164 
165     public void downloadFile(String source, OutputStream fout)
166         throws IOException {
167         out.writeInt(AGENT_OPEN_FILE);
168         out.writeUTF(source);
169         out.writeInt(OPEN_FILE_READ);
170         out.flush();
171 
172         int msgid = in.readInt();
173 
174         if (msgid == AGENT_STATUS_OK) {
175             String key = in.readUTF();
176             byte[] buf = new byte[8096];
177             int read = 0;
178 
179             while (read > -1) {
180                 out.writeInt(AGENT_READ_FILE);
181                 out.writeUTF(key);
182                 out.writeInt(buf.length);
183                 out.flush();
184 
185                 msgid = in.readInt();
186 
187                 if (msgid != AGENT_STATUS_OK) {
188                     in.readInt();
189 
190                     String msg = in.readUTF();
191                     throw new IOException(msg);
192                 }
193 
194                 read = in.readInt();
195 
196                 if (read > 0) {
197                     read = in.read(buf, 0, read);
198                     fout.write(buf, 0, read);
199                 }
200             }
201 
202             fout.close();
203             out.writeInt(AGENT_CLOSE_FILE);
204             out.writeUTF(key);
205             out.flush();
206             msgid = in.readInt();
207 
208             if (msgid != AGENT_STATUS_OK) {
209                 in.readInt();
210 
211                 String msg = in.readUTF();
212                 throw new IOException(msg);
213             }
214         } else {
215             in.readInt();
216 
217             String msg = in.readUTF();
218             throw new IOException(msg);
219         }
220     }
221 }