/** * Wait for the message passing operation associated with this CommRequest object to finish. If * the message passing operation involved a receive, a {@linkplain CommStatus} object is returned * giving the results of the receive, otherwise null is returned. * * <p>For a receive operation, the returned status object gives the actual rank of the process * that sent the message, the actual message tag that was received, and the actual number of data * items in the message. If the actual number of data items in the message is less than the length * of the receive buffer, nothing is stored into the extra data items at the end of the buffer. If * the actual number of data items in the message is greater than the length of the receive * buffer, the extra data items at the end of the message are discarded. * * <p><I>Note:</I> If the <TT>waitForFinish()</TT> method is called on a newly-created CommRequest * object that has not been passed to or returned from a communicator's non-blocking send or * receive method, the <TT>waitForFinish()</TT> method immediately returns null. * * @return Status object for a receive operation, otherwise null. * @exception IOException Thrown if an I/O error occurred. */ public CommStatus waitForFinish() throws IOException { if (mySendRequest != null) { mySendRequest.waitForFinish(); } if (myRecvRequest != null) { benchmarks.detinfer.pj.edu.ritmp.Status status = myRecvRequest.waitForFinish(); return new CommStatus(Comm.getFarRank(status.channel), status.tag, status.length); } else { return null; } }
private void SendConnectRequest(ConnectionState connState) { int error = mComm.Connect(connState.addr, connState.timeout, this); if (error == Error.COMM_ALREADY_CONNECTED) { synchronized (connState) { connState.connected = true; connState.notifyAll(); } } else if (error != Error.OK) { log.severe( "Connection attempt to " + connState.serviceName + " at " + connState.addr + " failed - " + Error.GetText(error) + ". Will retry again in %d seconds..."); connState.nextRetry = System.currentTimeMillis() + connState.timeout; mRetryQueue.add(connState); notify(); } }
public static void main(String[] args) throws MPIException { Comm comm; IntBuffer sBuf, rBuf; int rank, size, extent; int[] sendCounts, recvCounts, rDispls, sDispls; Datatype[] sDTypes, rDTypes; Request req; MPI.Init(args); comm = MPI.COMM_WORLD; /* Create the buffer */ size = comm.getSize(); rank = comm.getRank(); sBuf = MPI.newIntBuffer(size * size); rBuf = MPI.newIntBuffer(size * size); /* Load up the buffers */ for (int i = 0; i < (size * size); i++) { sBuf.put(i, (i + 100 * rank)); rBuf.put(i, -i); } /* Create and load the arguments to alltoallw */ sendCounts = new int[size]; recvCounts = new int[size]; rDispls = new int[size]; sDispls = new int[size]; sDTypes = new Datatype[size]; rDTypes = new Datatype[size]; extent = 4; // MPI.INT.getExtent(); //getExtent returns 1, but a 4 is needed for these calculations for (int i = 0; i < size; i++) { sendCounts[i] = i; recvCounts[i] = rank; rDispls[i] = (i * rank * extent); sDispls[i] = (((i * (i + 1)) / 2) * extent); sDTypes[i] = MPI.INT; rDTypes[i] = MPI.INT; } req = comm.iAllToAllw(sBuf, sendCounts, sDispls, sDTypes, rBuf, recvCounts, rDispls, rDTypes); req.waitFor(); req.free(); /* Check rbuf */ for (int i = 0; i < size; i++) { int p = rDispls[i] / extent; for (int j = 0; j < rank; j++) { if (rBuf.get(p + j) != (i * 100 + (rank * (rank + 1)) / 2 + j)) { System.out.println(i + " " + j + " " + size + " " + rank + " " + extent); OmpitestError.ompitestError( OmpitestError.getFileName(), OmpitestError.getLineNumber(), "bad answer " + rBuf.get(p + j) + " (should be " + (i * 100 + (rank * (rank + 1)) / 2 + j) + ")\n"); } } } MPI.COMM_WORLD.barrier(); MPI.Finalize(); if (rank == 0) { System.out.println("Test completed."); } }
public static void run(String host, String port) throws Exception { ServidorLoja servico = new ServidorLoja(); Endereco rs = new Endereco(host, port); // rs.(servico.getClass().getSimpleName()); nome da loja ServidorNomes sn = new ServidorNomes(); Comm mCliente = new Comm(new Endereco("localhost", "5000")); Comm mServidor = new Comm(rs); JSONObject request = new JSONObject(); request.put("op", "cadastrarLoja"); JSONObject data = new JSONObject(); data.put("ip", "localhost"); data.put("porta", "5551"); // data.put("identificador",""); data.put("nomeServico", "cea"); data.put("login", "cea"); data.put("senha", "1234"); request.put("data", data); // passa valores no json System.out.println(mCliente.requestAndReceive(request.toJSONString())); ServidorLoja sl = new ServidorLoja(); while (true) { MiddlewareThread thread = new MiddlewareThread(mServidor.receiveThread()) { @Override public String exec(String m) throws ParseException { System.out.println("Server Loja"); JSONParser parser = new JSONParser(); Map json = (Map) parser.parse(m, new JsonConteiner()); String op = (String) json.get("op"); System.out.println(); String valor = ""; LinkedHashMap data = (LinkedHashMap) json.get("data"); switch (op) { case "loginServidor": boolean b = sl.LoginServidor( (String) data.get("login"), (String) data.get("senha"), (String) data.get("ip"), (String) data.get("porta")); valor = "" + b; break; case "atualizarPanfleto": System.out.println(parser.parse((String) data.get("panfletos"))); JSONArray ba = (JSONArray) parser.parse((String) data.get("panfletos")); List<Panfleto> lista = new ArrayList<Panfleto>(); for (int i = 0; i < ba.size(); i++) { JSONObject o = (JSONObject) ba.get(i); Panfleto p = new Panfleto( (String) o.get("titulo"), (String) o.get("texto"), (String) o.get("link"), (Double) o.get("preco")); lista.add(p); } sl.AtualizarPanfleto(lista); valor = "true"; break; case "retornarPanfletos": JSONArray jsonArray = new JSONArray(); List<Panfleto> panfletos = sl.RetornarPanfletos(); for (Panfleto p : panfletos) { JSONObject obj = new JSONObject(); obj.put("titulo", p.getTitulo()); obj.put("texto", p.getTexto()); jsonArray.add(obj); } valor = jsonArray.toJSONString(); break; default: valor = "operacao desconhecida"; } // ServidorLoja servico = new ServidorLoja(); // monta json JSONObject response = new JSONObject(); response.put("result", valor); String mOut = response.toJSONString(); return mOut; } }; new Thread(thread).start(); } }
/** * This class is a class responsible for requesting values from the arduino. Since the performance * of the java swing application is not as good as the rate which the arduino can send bytes, to * optimize the ui performance, we only want to update components on an as needed basis. * * @author James */ public class InstrumentClusterUpdater implements Runnable { public static volatile String commandReceived = ""; Comm c = Comm.getInstance(); public void run() { while (true) { updateSpeed(); updateLeftTurnSignal(); updateRightTurnSignal(); updateBrights(); updateVoltage(); updateDebugTable(); } } private void updateDebugTable() { DigitalPins.getInstance().setStringValue(getDebugTable()); } private void updateVoltage() { int ana0 = retrievePinValue("ana0"); if (ana0 != 111111) { VoltageL2.getInstance().setValue(ana0); Battery.getInstance().setValue((int) ((double) ana0 * (double) .63)); } } private void updateBrights() { int dig6 = retrievePinValue("dig6"); if (dig6 != 111111) { Brights.getInstance().setValue(dig6); } } private void updateRightTurnSignal() { int dig11 = retrievePinValue("dig11"); if (dig11 != 111111) { RightTurnSignal.getInstance().setValue(dig11); } } private void updateLeftTurnSignal() { int dig12 = retrievePinValue("dig12"); if (dig12 != 111111) { LeftTurnSignal.getInstance().setValue(dig12); } } private void updateSpeed() { int ana1 = retrievePinValue("ana1"); if (ana1 != 111111) { int speed = calculateSpeed(ana1); if (speed >= 30) { MphDigitalTextL2.getInstance().setValue(speed); SpeedometerNeedle.getInstance().setValue(speed); } else { // Else, my rectifier circuit is not very accurate at low speeds for // whatever reason. MphDigitalTextL2.getInstance().setValue(0); SpeedometerNeedle.getInstance().setValue(0); } } } private int calculateSpeed(int ana1) { return Double.valueOf(.0000000000148 * Math.pow(ana1, 4.7)).intValue(); } private int retrievePinValue(String pin) { String response = sendValue(pin); if (response.contains("bad")) { return 10; } else { return intVal(response); } } private String getDebugTable() { StringBuilder sb = new StringBuilder(); sb.append("<html>"); sb.append("<table>"); sb.append("<tr>"); sb.append("<td>"); sb.append("MPH = " + pad(sendValue("ana1"))); sb.append("</td>"); sb.append("</tr>"); sb.append("</table>"); sb.append("</html>"); // sb.append("<td>");sb.append("dig5 = "+ // pad(sendValue("dig5")));sb.append("</td>"); // sb.append("<td>");sb.append("dig11 = "+ // pad(sendValue("dig11")));sb.append("</td>"); // Right turn signal // sb.append("<td>");sb.append("ana2 = "+ // pad(sendValue("ana2")));sb.append("</td>"); // sb.append("<td>");sb.append("dig6 = "+ // pad(sendValue("dig6")));sb.append("</td>"); // Brights // sb.append("<td>");sb.append("dig12 = "+ // pad(sendValue("dig12")));sb.append("</td>"); // Left turn signal // sb.append("<td>");sb.append("ana3 = " + // pad(sendValue("ana3")));sb.append("</td>"); // sb.append("<td>");sb.append("dig7 = "+ // pad(sendValue("dig7")));sb.append("</td>"); // sb.append("<td>");sb.append("");sb.append("</td>"); // sb.append("<td>");sb.append("dig8 = "+ // pad(sendValue("dig8")));sb.append("</td>"); // sb.append("<td>");sb.append("");sb.append("</td>"); // sb.append("<td>");sb.append("ana5 = " + // pad(sendValue("ana5")));sb.append("</td>"); return sb.toString(); } private int intVal(String in) { try { return Integer.parseInt(in); } catch (NumberFormatException e) { return 1; } } private String pad(String in) { return StringUtils.leftPad(in, 4, "0"); } private String sendValue(String string) { commandReceived = ""; c.sendString(string); return waitForResponse(); } private String waitForResponse() { while (commandReceived.equals("")) { try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } return commandReceived; } }
private String sendValue(String string) { commandReceived = ""; c.sendString(string); return waitForResponse(); }