001 /* 002 003 $Id: DaemonCommunication.java,v 1.10 2003/05/02 20:29:47 culdesac Exp $ 004 005 */ 006 007 package sharpster.client.daemoncommunication; 008 009 import sharpster.common.*; 010 import sharpster.common.ClientDaemonMessage; 011 import sharpster.common.FileCollection; 012 import sharpster.common.ResponseCollection; 013 import sharpster.common.InternalMessageType; 014 import java.net.Socket; 015 import java.io.*; 016 017 /** 018 * Class responsible for forwarding commands from the client to the daemon. 019 * 020 */ 021 public class DaemonCommunication { 022 023 /** 024 * Holds the data which is to be sent to the daemon. 025 */ 026 private ClientDaemonMessage message; 027 028 /** 029 * Constructs a daemon communication object 030 */ 031 public DaemonCommunication() { 032 message = new ClientDaemonMessage(); 033 message.clear(); 034 } 035 036 /** 037 * Transfers the initialized message to the daemon and waits for 038 * a response. 039 */ 040 private boolean transferMessage() { 041 Socket socket = null; 042 try { 043 // adress to connect to. (localhost) 044 String adress = "127.0.0.1"; 045 // portnumber to connect to. 046 int port = 7000; 047 // try to create the new socket. 048 socket = new Socket(adress, port); 049 } catch(IOException e) { 050 System.out.println("DaemonCommuncation::DaemonCommunication could "+ 051 "not connect to daemon\n"); 052 // we got an error, set socket to null to be on the safe side. 053 socket = null; 054 } 055 056 if (null == socket) 057 return false; 058 059 boolean retval = true; 060 try { 061 /* send the object */ 062 ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); 063 oos.writeObject(message); 064 oos.flush(); 065 066 /* receive answer */ 067 ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); 068 message = (ClientDaemonMessage) ois.readObject(); 069 070 /* close the connections. */ 071 oos.close(); 072 ois.close(); 073 socket.close(); 074 } catch (Exception e) { // IOException, java.lang.ClassNotFoundException 075 System.out.println("DaemonCommuncation::transferMessage error " + e.getMessage()); 076 retval = false; 077 } 078 return retval; 079 } 080 081 /** 082 * Utility function that gets the current working directory. 083 */ 084 private String getCurrentWorkingDirectory() { 085 return new String(System.getProperty("user.dir")); 086 } 087 088 /** 089 * Forwards a shareFiles call to the daemon. 090 */ 091 public ResponseCollection shareFiles(FileCollection files) { 092 message.clear(); 093 message.commandId = InternalMessageType.SHARE_FILES; 094 message.files = files; 095 message.workingDirectory = getCurrentWorkingDirectory(); 096 097 if (!transferMessage()) 098 return null; 099 100 return message.response; 101 } 102 103 /** 104 * Forwards a unshareFiles call to the daemon. 105 */ 106 public ResponseCollection unShareFiles(FileCollection files) { 107 message.clear(); 108 message.commandId = InternalMessageType.UNSHARE_FILES; 109 message.files = files; 110 message.workingDirectory = getCurrentWorkingDirectory(); 111 if (!transferMessage()) 112 return null; 113 114 return message.response; 115 } 116 117 /** 118 * Forwards a localDoCVS call to the daemon. 119 */ 120 public ResponseCollection localDoCVS(String command) { 121 message.clear(); 122 message.commandId = InternalMessageType.LOCAL_DO_CVS; 123 message.CVScommand = command; 124 message.workingDirectory = getCurrentWorkingDirectory(); 125 if (!transferMessage()) 126 return null; 127 128 return message.response; 129 } 130 131 /** 132 * Forwards a remoteCheckoutFiles call to the daemon. 133 */ 134 public ResponseCollection remoteCheckoutFiles(FileCollection files, 135 String fromUser, 136 String role) { 137 message.clear(); 138 message.commandId = InternalMessageType.REMOTE_CHECKOUT; 139 message.files = files; 140 message.fromUser = fromUser; 141 message.role = role; 142 message.workingDirectory = getCurrentWorkingDirectory(); 143 if (!transferMessage()) 144 return null; 145 146 return message.response; 147 } 148 149 /** 150 * Forwards a remoteUpdateFiles call to the daemon. 151 */ 152 public ResponseCollection remoteUpdateFiles(FileCollection files, 153 String fromUser) { 154 message.clear(); 155 message.commandId = InternalMessageType.REMOTE_UPDATE; 156 message.files = files; 157 message.fromUser = fromUser; 158 message.workingDirectory = getCurrentWorkingDirectory(); 159 if (!transferMessage()) 160 return null; 161 162 return message.response; 163 } 164 165 /** 166 * Forwards a remoteCommitFiles call to the daemon. 167 */ 168 public ResponseCollection remoteCommitFiles(FileCollection files, 169 String fromUser, 170 String role) { 171 message.clear(); 172 message.commandId = InternalMessageType.REMOTE_COMMIT; 173 message.files = files; 174 message.fromUser = fromUser; 175 message.role = role; 176 message.workingDirectory = getCurrentWorkingDirectory(); 177 if (!transferMessage()) 178 return null; 179 180 return message.response; 181 } 182 183 184 /** 185 * Forwards a groupCommand call to the daemon. 186 */ 187 public ResponseCollection groupCommand(GroupCommand command) { 188 message.clear(); 189 message.commandId = InternalMessageType.GROUP_COMMAND; 190 message.command = command; 191 message.workingDirectory = getCurrentWorkingDirectory(); 192 if (!transferMessage()) 193 return null; 194 195 return message.response; 196 } 197 198 199 /** 200 * Forwards a remoteAddFiles call to the daemon. 201 */ 202 public ResponseCollection remoteAddFiles(FileCollection files, 203 String fromUser) { 204 message.clear(); 205 message.commandId = InternalMessageType.REMOTE_ADD; 206 message.files = files; 207 message.fromUser = fromUser; 208 message.workingDirectory = getCurrentWorkingDirectory(); 209 if (!transferMessage()) 210 return null; 211 212 return message.response; 213 } 214 215 /** 216 * Forwards a remoteRemoveFiles call to the daemon. 217 */ 218 public ResponseCollection remoteRemoveFiles(FileCollection files, 219 String fromUser) { 220 message.clear(); 221 message.commandId = InternalMessageType.REMOTE_REMOVE; 222 message.files = files; 223 message.fromUser = fromUser; 224 message.workingDirectory = getCurrentWorkingDirectory(); 225 if (!transferMessage()) 226 return null; 227 228 return message.response; 229 } 230 231 /** 232 * Forwards a viewFileSharing call to the daemon. 233 */ 234 public ResponseCollection viewFileSharing() { 235 message.clear(); 236 message.commandId = InternalMessageType.VIEW_SHARING; 237 if (!transferMessage()) 238 return null; 239 240 return message.response; 241 } 242 243 /** 244 * Forwards a viewUsers call to the daemon. 245 */ 246 public ResponseCollection viewUsers() { 247 message.clear(); 248 message.commandId = InternalMessageType.VIEW_USERS; 249 if (!transferMessage()) 250 return null; 251 252 return message.response; 253 } 254 255 /** 256 * Forwards a viewFileAccess call to the daemon. 257 */ 258 public ResponseCollection viewFileAccess() { 259 message.clear(); 260 message.commandId = InternalMessageType.VIEW_ACCESS; 261 if (!transferMessage()) 262 return null; 263 264 return message.response; 265 } 266 267 /** 268 * Forwards a viewFileAccess call to the daemon. 269 */ 270 public ResponseCollection synchronizeCVS() { 271 message.clear(); 272 message.commandId = InternalMessageType.SYNCHRONIZE_CVS; 273 message.workingDirectory = getCurrentWorkingDirectory(); 274 if (!transferMessage()) 275 return null; 276 277 return message.response; 278 } 279 280 281 282 283 } 284 285 286 287 288 289 290 291 292