Changeset 243
- Timestamp:
- 07/11/13 19:08:33 (10 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/plugins/CacheCoveragePlugin/src/com/bowman/cardserv/HttpCacheForwarder.java
r238 r243 8 8 9 9 import java.io.*; 10 import java.lang.System; 10 11 import java.net.*; 11 12 import java.util.*; … … 37 38 private int port; 38 39 private boolean connected, redundant; 39 private int counter, reconnects, errors, ecmForwards, delayAlerts, filtered;40 private int counter, reconnects, timeouts, errors, ecmForwards, delayAlerts, filtered; 40 41 private int maxDelay; 41 42 private Set profiles, caids; … … 105 106 p.setProperty("reconnects", String.valueOf(reconnects)); 106 107 p.setProperty("errors", String.valueOf(errors)); 108 p.setProperty("timeouts", String.valueOf(timeouts)); 107 109 return p; 108 110 } … … 223 225 headers.setProperty(s[0], s[1]); 224 226 if(s[0].equals("Set-Cookie")) { 225 s = s[1].split("="); 227 if(s[1].indexOf(";") != -1) { 228 s = s[1].split(";"); // ignore all except 1st 229 s = s[0].split("="); 230 } else s = s[1].split("="); 226 231 cookies.setProperty(s[0], s[1]); 227 232 } … … 345 350 reply.body = sb.toString().toCharArray(); 346 351 } else { 347 reply.body = new char[reply.getContentLength()]; 348 if(br.read(reply.body) != reply.body.length) { 349 throw new IOException("Assertation failed"); // todo 350 } 352 if(reply.getContentLength() > 0) { 353 reply.body = new char[reply.getContentLength()]; 354 if(br.read(reply.body) != reply.body.length) { 355 throw new IOException("Assertation failed"); // todo 356 } 357 } else reply.body = new char[0]; 351 358 } 352 359 … … 488 495 int curSize = redundant?localQ.size():singleQ.size(); 489 496 if(maxDelay > 0 && curSize < 100) Thread.sleep(maxDelay); 490 } catch 497 } catch(SocketException se) { 491 498 // socket was closed gracefully, reconnect immediately 492 499 parent.logger.throwing(se); … … 496 503 } 497 504 } catch(SocketException e) { // probably connect failure? 498 parent.logger.info("HttpCacheForwarder[" + name + "] disconnected");505 parent.logger.info("HttpCacheForwarder[" + name + "] failed to connect: " + e); 499 506 parent.logger.throwing(e); 507 handleError(myQ, false); 508 } catch(SocketTimeoutException e) { // timeout 509 parent.logger.warning("HttpCacheForwarder[" + name + "] timeout (sendQ size: " + myQ.size() + "): " + e); 510 parent.logger.throwing(e); 511 timeouts++; 500 512 handleError(myQ, false); 501 513 } catch(IOException e) { // abnormal disconnect -
trunk/src/com/bowman/cardserv/CardServProxy.java
r241 r243 401 401 402 402 if(modified.isEmpty() && !(profile.isCacheOnly() || config.isCatchAll())) { 403 logger.fine("Denying message with no connector candidates from '" + session + "': " + msg.hashCodeStr()); 404 denyMessage(session, msg); 405 return; 403 if(!cacheHandler.containsCaid(msg.getCaId())) { 404 logger.fine("Denying message with no connector candidates from '" + session + "': " + msg.hashCodeStr()); 405 denyMessage(session, msg); 406 return; 407 } 406 408 } 407 409 -
trunk/src/com/bowman/cardserv/DefaultCache.java
r235 r243 24 24 25 25 private int timeouts, instantHits, waitHits, remoteHits, pendingPeak, contested; 26 27 private Set caids = new HashSet(); 26 28 27 29 public DefaultCache() { … … 162 164 } 163 165 166 public synchronized void processReplies(Map replies) { 167 boolean lockFound = false; 168 CamdNetMessage request, reply; 169 for(Iterator iter = replies.keySet().iterator(); iter.hasNext(); ) { 170 request = (CamdNetMessage)iter.next(); 171 reply = (CamdNetMessage)replies.get(request); 172 // if(monitor != null) monitor.onReply(request, reply); 173 if(listener != null) listener.onReply(request, reply); 174 if(pendingEcms.containsKey(request)) { 175 removeRequest(request); 176 lockFound = true; 177 } 178 addReply(request, reply); 179 } 180 if(lockFound) notifyAll(); 181 } 182 164 183 public CamdNetMessage peekReply(CamdNetMessage request) { 165 184 return (CamdNetMessage)ecmMap.get(request); … … 186 205 } 187 206 207 public boolean containsCaid(int caid) { 208 return caids.contains(new Integer(caid)); 209 } 210 188 211 protected synchronized void addRequest(int successFactor, CamdNetMessage request, boolean alwaysWait) { 189 212 CamdNetMessage oldRequest = (CamdNetMessage)pendingEcms.put(request, request); 190 213 if(pendingEcms.size() > pendingPeak) pendingPeak = pendingEcms.size(); 191 214 if(oldRequest == null) { 215 caids.add(new Integer(request.getCaId())); 192 216 if(monitor != null) monitor.onRequest(successFactor, request); 193 217 if(listener != null) listener.onRequest(successFactor, request); -
trunk/src/com/bowman/cardserv/interfaces/CacheHandler.java
r191 r243 3 3 import com.bowman.cardserv.CamdNetMessage; 4 4 5 import java.util. Properties;5 import java.util.*; 6 6 7 7 /** … … 18 18 CamdNetMessage peekReply(CamdNetMessage request); 19 19 long getMaxCacheWait(long maxCwWait); 20 boolean containsCaid(int caid); 21 void processReplies(Map replies); 20 22 21 23 Properties getUsageStats(); -
trunk/src/com/bowman/cardserv/web/GHttpBackend.java
r238 r243 40 40 private Map sessionsByUser = new HashMap(); 41 41 private Map contexts = Collections.synchronizedMap(new LinkedHashMap()); 42 private Set blackList = new HashSet(); 42 43 43 44 private StatusCommand contextsCmd; … … 144 145 } 145 146 147 blackList.clear(); 148 String bl = FileFetcher.getProperty("cache.bl"); 149 if(bl != null) blackList.addAll(Arrays.asList(bl.split(" "))); 150 146 151 } else { 147 152 if(httpd != null) { … … 191 196 192 197 private CamdNetMessage waitForReply(CamdNetMessage request) { 198 // long start = System.currentTimeMillis(); 193 199 CamdNetMessage reply = cache.peekReply(request); 194 200 if(reply != null) return reply; 195 else reply = cache.processRequest(-1, request, true, request.getMaxWait() * 3); // todo 201 else reply = cache.processRequest(-1, request, true, request.getMaxWait() * 2); // todo 202 // System.out.println((System.currentTimeMillis() - start) + " - " + reply); 196 203 return reply; 197 204 } … … 211 218 } 212 219 213 private GHttpSession getSession(String id, String ip) {220 private GHttpSession getSession(String id, String ip) throws GHttpAuthException { 214 221 GHttpSession gs = (GHttpSession)sessions.get(id); 215 222 if(gs != null && gs.isExpired()) { 216 223 sessionsByUser.remove(gs.getUser()); 217 224 sessions.remove(id); 218 gs = null;225 throw new GHttpAuthException(getErrorResponse(401, "Authorization required")); 219 226 } 220 227 if(gs != null && ip.equals(gs.getRemoteAddress())) return gs; … … 222 229 } 223 230 224 private GHttpSession findSession(String user, String ip) {231 private GHttpSession findSession(String user, String ip) throws GHttpAuthException { 225 232 GHttpSession gs = (GHttpSession)sessionsByUser.get(user); 226 233 if(gs != null && gs.isExpired()) { 227 234 sessionsByUser.remove(user); 228 235 sessions.remove(gs.getGhttpSessionId()); 229 gs = null;236 throw new GHttpAuthException(getErrorResponse(401, "Authorization required")); 230 237 } 231 238 if(gs != null && ip.equals(gs.getRemoteAddress())) return gs; … … 234 241 235 242 private synchronized GHttpSession doGhttpAuth(HttpRequest req, String[] s) throws GHttpAuthException { 243 return doGhttpAuth(req, s, accessPasswd); 244 } 245 246 private synchronized GHttpSession doGhttpAuth(HttpRequest req, String[] s, String passwd) throws GHttpAuthException { 236 247 GHttpSession gs = null; 237 if(s [3].length() >= 6) { // session id included248 if(s.length > 3 && s[3].length() >= 6) { // session id included 238 249 gs = getSession(s[3], req.getRemoteAddress()); 239 250 } 240 251 if(gs == null) { // no session id in query string (or invalid/expired session), check for auth 241 String user = parent.checkBasicAuth(req, accessPasswd);242 if(user == null) throw new GHttpAuthException(getErrorResponse(40 1, "Authorization required"));252 String user = parent.checkBasicAuth(req, passwd); 253 if(user == null) throw new GHttpAuthException(getErrorResponse(403, "Forbidden")); 243 254 if(!user.startsWith(WebBackend.anonPrefix)) { 244 255 HttpResponse error = parent.doIpCheck(req, user); … … 298 309 return doPmtGetOrPost(req); 299 310 } else if(q.startsWith(API_FEEDER_POST)) { 300 // todo311 return doCachePost(req); 301 312 } 302 313 return getErrorResponse(503, "Not implemented"); // todo 314 } 315 } 316 317 private HttpResponse doCachePost(HttpRequest req) { 318 if(feederPasswd == null) return getErrorResponse(403, "Disabled"); 319 if(cache == null) cache = config.getCacheHandler(); 320 try { 321 String[] s = req.getQueryString().split("/"); 322 GHttpSession gs = doGhttpAuth(req, s, feederPasswd); 323 if(gs == null) return getErrorResponse(403, "Forbidden"); 324 byte[] content = req.getContent(); 325 String addr = req.getRemoteAddress(); 326 if(content.length >= RECORD_SIZE) { 327 Map replies = new HashMap(); 328 DataInputStream dis = new DataInputStream(new ByteArrayInputStream(content)); 329 while(dis.available() >= RECORD_SIZE) { 330 CamdNetMessage request = CamdNetMessage.parseCacheReq(dis, false); 331 CamdNetMessage reply = CamdNetMessage.parseCacheRpl(dis, request, false); 332 if(!blackList.contains(addr)) { 333 request.setOriginAddress(addr); 334 reply.setOriginAddress(addr); 335 } 336 replies.put(request, reply); 337 } 338 cache.processReplies(replies); 339 if(dis.available() != 0) parent.logger.warning("Trailing bytes in feed post: " + dis.available()); 340 HttpResponse res = new HttpResponse(204, "No Content"); // todo: include stats 341 if(s.length <= 3) res.setCookie("GSSID", gs.getGhttpSessionId()); 342 return res; 343 } else throw new IOException("Invalid content length (" + content.length + ")"); 344 } catch (GHttpAuthException e) { 345 return getErrorResponse(403, "Forbidden"); 346 } catch (Exception e) { 347 parent.logger.throwing("Bad ghttp feeder request: " + req.getQueryString(), e); 348 return HttpResponse.getErrorResponse(400, req.getQueryString()); 303 349 } 304 350 } … … 317 363 if(ProxyConfig.getInstance().getProfileById(ecmReq.getNetworkId(), ecmReq.getCaId()) == null) 318 364 return getErrorResponse(503, "Unknown system: " + CaProfile.getKeyStr(ecmReq.getNetworkId(), ecmReq.getCaId())); 365 else if(!cache.containsCaid(ecmReq.getCaId())) 366 return getErrorResponse(503, "Unknown caid: " + CaProfile.getKeyStr(ecmReq.getNetworkId(), ecmReq.getCaId())); 319 367 gs.fireCamdMessage(ecmReq, false); 320 368 reply = gs.waitForReply(ecmReq); 321 369 } else instant = true; 322 370 323 if(reply == null) cache.peekReply(ecmReq);371 if(reply == null) reply = cache.peekReply(ecmReq); 324 372 if(reply == null) return getErrorResponse(503, "Ecm timeout"); 325 373 else {
Note:
See TracChangeset
for help on using the changeset viewer.