Пример #1
0
 /** Implementation of the functionality related to joining an existing remote session. */
 private void doLeaveSession() {
   Log.i(TAG, "doLeaveSession()");
   if (mJoinedToSelf == false) {
     mBus.leaveSession(mUseSessionId);
   }
   mUseSessionId = -1;
   mJoinedToSelf = false;
   mUseChannelState = UseChannelState.IDLE;
   mChatApplication.useSetChannelState(mUseChannelState);
 }
Пример #2
0
    public void handleMessage(Message msg) {
      switch (msg.what) {
        case (CONNECT):
          {
            org.alljoyn.bus.alljoyn.DaemonInit.PrepareDaemon(getApplicationContext());
            mBus = new BusAttachment(getPackageName(), BusAttachment.RemoteMessage.Receive);

            mBus.registerBusListener(
                new BusListener() {
                  @Override
                  public void foundAdvertisedName(String name, short transport, String namePrefix) {
                    logInfo(
                        String.format(
                            "MyBusListener.foundAdvertisedName(%s, 0x%04x, %s)",
                            name, transport, namePrefix));
                    /*
                     * This client will only join the first service that it sees advertising
                     * the indicated well-known name.  If the program is already a member of
                     * a session (i.e. connected to a service) we will not attempt to join
                     * another session.
                     * It is possible to join multiple session however joining multiple
                     * sessions is not shown in this sample.
                     */
                    if (!mIsConnected) {
                      Message msg = obtainMessage(JOIN_SESSION, name);
                      sendMessage(msg);
                    }
                  }
                });

            // Connect the BusAttachment with the bus.
            Status status = mBus.connect();
            logStatus("BusAttachment.connect()", status);
            if (Status.OK != status) {
              finish();
              return;
            }

            status = mBus.findAdvertisedName(SERVICE_NAME);
            logStatus("BusAttachement.findAdvertisedName()", status);
            if (Status.OK != status) {
              finish();
              return;
            }
            break;
          }
        case (JOIN_SESSION):
          {
            /*
             * If discovery is currently being stopped don't join to any other sessions.
             */
            if (mIsStoppingDiscovery) {
              break;
            }

            /*
             * In order to join the session, we need to provide the well-known
             * contact port.  This is pre-arranged between both sides as part
             * of the definition of the chat service.  As a result of joining
             * the session, we get a session identifier which we must use to
             * identify the created session communication channel whenever we
             * talk to the remote side.
             */
            short contactPort = CONTACT_PORT;
            SessionOpts sessionOpts = new SessionOpts();
            Mutable.IntegerValue sessionId = new Mutable.IntegerValue();

            Status status =
                mBus.joinSession(
                    (String) msg.obj,
                    contactPort,
                    sessionId,
                    sessionOpts,
                    new SessionListener() {
                      @Override
                      public void sessionLost(int sessionId, int reason) {
                        mIsConnected = false;
                        logInfo(
                            String.format(
                                "MyBusListener.sessionLost(sessionId = %d, reason = %d)",
                                sessionId, reason));
                        mHandler.sendEmptyMessage(MESSAGE_START_PROGRESS_DIALOG);
                      }
                    });
            logStatus("BusAttachment.joinSession()", status);
            if (status == Status.OK) {
              mProxyObj =
                  mBus.getProxyBusObject(
                      SERVICE_NAME,
                      "/testProperties",
                      sessionId.value,
                      new Class<?>[] {PropertiesInterface.class});

              mPropertiesInterface = mProxyObj.getInterface(PropertiesInterface.class);
              mSessionId = sessionId.value;
              mIsConnected = true;
              mHandler.sendEmptyMessage(MESSAGE_STOP_PROGRESS_DIALOG);
            }
            break;
          }
        case (DISCONNECT):
          {
            mIsStoppingDiscovery = true;
            if (mIsConnected) {
              Status status = mBus.leaveSession(mSessionId);
              logStatus("BusAttachment.leaveSession()", status);
            }
            mBus.disconnect();
            getLooper().quit();
            break;
          }
        case (GET_BACKGROUND_COLOR_PROPERTY):
          {
            if (!mIsConnected) {
              break;
            }
            try {
              String backgroundColor = mPropertiesInterface.getBackGroundColor();
              mHandler.sendMessage(
                  mHandler.obtainMessage(MESSAGE_UPDATE_BACKGROUND_COLOR, backgroundColor));
            } catch (BusException e) {
            }
            break;
          }
        case (SET_BACKGROUND_COLOR_PROPERTY):
          {
            if (!mIsConnected) {
              break;
            }
            try {
              mPropertiesInterface.setBackGroundColor((String) msg.obj);
              mHandler.sendMessage(
                  mHandler.obtainMessage(MESSAGE_UPDATE_BACKGROUND_COLOR, (String) msg.obj));
            } catch (BusException e) {
              logException(getString(R.string.get_properties_error), e);
            }
            break;
          }
        case (GET_TEXT_SIZE_PROPERTY):
          {
            if (!mIsConnected) {
              break;
            }
            try {
              int textSize = mPropertiesInterface.getTextSize();
              Message textMsg = mHandler.obtainMessage(MESSAGE_UPDATE_TEXT_SIZE);
              textMsg.arg1 = textSize;
              mHandler.sendMessage(textMsg);
            } catch (BusException e) {
              logException(getString(R.string.get_properties_error), e);
            }
            break;
          }
        case (SET_TEXT_SIZE_PROPERTY):
          {
            if (!mIsConnected) {
              break;
            }
            try {
              mPropertiesInterface.setTextSize(msg.arg1);
              Message textMsg = mHandler.obtainMessage(MESSAGE_UPDATE_TEXT_SIZE);
              textMsg.arg1 = msg.arg1;
              mHandler.sendMessage(textMsg);
            } catch (BusException e) {
              logException(getString(R.string.get_properties_error), e);
            }
            break;
          }
        default:
          break;
      }
    }
      @Override
      public void handleMessage(Message msg) {
        switch (msg.what) {
            /* Connect to a remote instance of an object implementing the BasicInterface. */
          case CONNECT:
            {
              org.alljoyn.bus.alljoyn.DaemonInit.PrepareDaemon(getApplicationContext());
              /*
               * All communication through AllJoyn begins with a BusAttachment.
               *
               * A BusAttachment needs a name. The actual name is unimportant except for internal
               * security. As a default we use the class name as the name.
               *
               * By default AllJoyn does not allow communication between devices (i.e. bus to bus
               * communication). The second argument must be set to Receive to allow communication
               * between devices.
               */
              mBus = new BusAttachment(getPackageName(), BusAttachment.RemoteMessage.Receive);

              /*
               * Create a bus listener class
               */
              mBus.registerBusListener(
                  new BusListener() {
                    @Override
                    public void foundAdvertisedName(
                        String name, short transport, String namePrefix) {
                      logInfo(
                          String.format(
                              "MyBusListener.foundAdvertisedName(%s, 0x%04x, %s)",
                              name, transport, namePrefix));
                      /*
                       * This client will only join the first service that it sees advertising
                       * the indicated well-known name.  If the program is already a member of
                       * a session (i.e. connected to a service) we will not attempt to join
                       * another session.
                       * It is possible to join multiple session however joining multiple
                       * sessions is not shown in this sample.
                       */
                      if (!mIsConnected) {
                        Message msg = obtainMessage(JOIN_SESSION);
                        msg.arg1 = transport;
                        msg.obj = name;
                        sendMessage(msg);
                      }
                    }
                  });

              /* To communicate with AllJoyn objects, we must connect the BusAttachment to the bus. */
              Status status = mBus.connect();
              logStatus("BusAttachment.connect()", status);
              if (Status.OK != status) {
                finish();
                return;
              }

              /*
               * Now find an instance of the AllJoyn object we want to call.  We start by looking for
               * a name, then connecting to the device that is advertising that name.
               *
               * In this case, we are looking for the well-known SERVICE_NAME.
               */
              status = mBus.findAdvertisedName(SERVICE_NAME);
              logStatus(
                  String.format("BusAttachement.findAdvertisedName(%s)", SERVICE_NAME), status);
              if (Status.OK != status) {
                finish();
                return;
              }

              break;
            }
          case (JOIN_SESSION):
            {
              /*
               * If discovery is currently being stopped don't join to any other sessions.
               */
              if (mIsStoppingDiscovery) {
                break;
              }

              /*
               * In order to join the session, we need to provide the well-known
               * contact port.  This is pre-arranged between both sides as part
               * of the definition of the chat service.  As a result of joining
               * the session, we get a session identifier which we must use to
               * identify the created session communication channel whenever we
               * talk to the remote side.
               */
              short contactPort = CONTACT_PORT;
              SessionOpts sessionOpts = new SessionOpts();
              sessionOpts.transports = (short) msg.arg1;
              Mutable.IntegerValue sessionId = new Mutable.IntegerValue();

              Status status =
                  mBus.joinSession(
                      (String) msg.obj,
                      contactPort,
                      sessionId,
                      sessionOpts,
                      new SessionListener() {
                        @Override
                        public void sessionLost(int sessionId, int reason) {
                          mIsConnected = false;
                          logInfo(
                              String.format(
                                  "MyBusListener.sessionLost(sessionId = %d, reason = %d)",
                                  sessionId, reason));
                          mHandler.sendEmptyMessage(MESSAGE_ALLJOYN_START_PROGRESS_DIALOG);
                        }
                      });
              logStatus("BusAttachment.joinSession() - sessionId: " + sessionId.value, status);

              if (status == Status.OK) {
                /*
                 * To communicate with an AllJoyn object, we create a ProxyBusObject.
                 * A ProxyBusObject is composed of a name, path, sessionID and interfaces.
                 *
                 * This ProxyBusObject is located at the well-known SERVICE_NAME, under path
                 * "/sample", uses sessionID of CONTACT_PORT, and implements the BasicInterface.
                 */
                mProxyObj =
                    mBus.getProxyBusObject(
                        SERVICE_NAME,
                        OBJ_PATH,
                        sessionId.value,
                        new Class<?>[] {BasicInterface.class});

                /* We make calls to the methods of the AllJoyn object through one of its interfaces. */
                mBasicInterface = mProxyObj.getInterface(BasicInterface.class);

                mSessionId = sessionId.value;
                mIsConnected = true;
                mHandler.sendEmptyMessage(MESSAGE_ALLJOYN_STOP_PROGRESS_DIALOG);
              }
              break;
            }

            /* Release all resources acquired in the connect. */
          case DISCONNECT:
            {
              mIsStoppingDiscovery = true;
              if (mIsConnected) {
                Status status = mBus.leaveSession(mSessionId);
                logStatus("BusAttachment.leaveSession()", status);
              }
              mBus.disconnect();
              getLooper().quit();
              break;
            }

            /*
             * Call the service's Cat method through the ProxyBusObject.
             *
             * This will also print the String that was sent to the service and the String that was
             * received from the service to the user interface.
             */
          case GET_TOPIC:
            {
              try {
                if (mBasicInterface != null) {
                  // sendUiMessage(MESSAGE_PING, msg.obj + " and " + msg.obj);
                  // String reply = mBasicInterface.cat((String) msg.obj, (String) msg.obj);
                  String reply = mBasicInterface.get_topic((String) msg.obj);
                  sendUiMessage(MESSAGE_ALLJOYN_GETTOPIC_REPLY, reply);
                }
              } catch (BusException ex) {
                logException("BasicInterface.cat()", ex);
              }
              break;
            }
          default:
            break;
        }
      }
Пример #4
0
    @Override
    public void handleMessage(Message msg) {
      switch (msg.what) {
          /*
           * Connect to a remote instance of an object implementing the
           * SimpleInterface.
           */
        case CONNECT:
          {
            mBus = new BusAttachment(getPackageName(), BusAttachment.RemoteMessage.Receive);
            mBus.registerBusListener(
                new BusListener() {
                  @Override
                  public void foundAdvertisedName(String name, short transport, String namePrefix) {
                    logInfo(
                        String.format(
                            "MyBusListener.foundAdvertisedName(%s, 0x%04x, %s)",
                            name, transport, namePrefix));

                    if (!mIsConnected) {
                      Message msg = obtainMessage(JOIN_SESSION, name);
                      sendMessage(msg);
                    }
                  }
                });

            Status status = mBus.connect();
            logStatus("BusAttachment.connect()", status);
            if (Status.OK != status) {
              finish();
              return;
            }

            status = mBus.findAdvertisedName(SERVICE_NAME);
            logStatus(String.format("BusAttachement.findAdvertisedName(%s)", SERVICE_NAME), status);
            if (Status.OK != status) {
              finish();
              return;
            }

            break;
          }
        case (JOIN_SESSION):
          {
            if (mIsStoppingDiscovery) {
              break;
            }
            short contactPort = CONTACT_PORT;
            SessionOpts sessionOpts = new SessionOpts();
            Mutable.IntegerValue sessionId = new Mutable.IntegerValue();

            Status status =
                mBus.joinSession(
                    (String) msg.obj,
                    contactPort,
                    sessionId,
                    sessionOpts,
                    new SessionListener() {
                      @Override
                      public void sessionLost(int sessionId) {
                        mIsConnected = false;
                        logInfo(String.format("MyBusListener.sessionLost(%d)", sessionId));
                        mHandler.sendEmptyMessage(MESSAGE_START_PROGRESS_DIALOG);
                      }
                    });
            logStatus("BusAttachment.joinSession() - sessionId: " + sessionId.value, status);

            if (status == Status.OK) {
              mProxyObj =
                  mBus.getProxyBusObject(
                      SERVICE_NAME,
                      "/SimpleService",
                      sessionId.value,
                      new Class<?>[] {SimpleInterface.class});
              mSimpleInterface = mProxyObj.getInterface(SimpleInterface.class);

              mSessionId = sessionId.value;
              mIsConnected = true;
              mHandler.sendEmptyMessage(MESSAGE_STOP_PROGRESS_DIALOG);

              try {
                starttime = System.currentTimeMillis();
                timer_flag = 1;
                System.out.println("In Client!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                System.out.println("In Client!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                System.out.println("In Client!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                System.out.println("In Client!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                System.out.println("In Client!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                System.out.println("In Client!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                System.out.println("In Client!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                System.out.println("In Client!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

                double[] numbers = new double[capacity];

                int clientid = mSimpleInterface.getClientID();
                System.out.println("Client ID is " + clientid);
                System.out.println("Client ID is " + clientid);
                System.out.println("Client ID is " + clientid);
                System.out.println("Client ID is " + clientid);
                System.out.println("Client ID is " + clientid);
                System.out.println("Client ID is " + clientid);

                if (clientid != -1) {
                  double frequency = mSimpleInterface.getFrequencyToRunAt();
                  System.out.println("Frequency is " + frequency);
                  System.out.println("Frequency is " + frequency);
                  System.out.println("Frequency is " + frequency);
                  System.out.println("Frequency is " + frequency);
                  System.out.println("Frequency is " + frequency);
                  System.out.println("Frequency is " + frequency);

                  // then set scaling governor to userspace and then write the frequency to the file

                  // next 13 lines commented out for facebook hackathon
                  /*String f=(new Integer((int)frequency)).toString();
                  String min="echo 100000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq";
                  String max="echo 1400000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq";
                  String temp="echo "+f+" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed";
                  Process p;
                  p = Runtime.getRuntime().exec("su");
                  DataOutputStream os = new DataOutputStream(p.getOutputStream());
                  os.writeBytes("echo userspace > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"+"\n");
                  os.writeBytes(max+"\n");
                  os.writeBytes(min+"\n");
                  os.writeBytes(temp+"\n");
                  os.writeBytes("exit\n");
                  os.flush();*/

                  int no =
                      mSimpleInterface
                          .giveImageData(); // no specifies how many times the giveActualImageData
                                            // needs to be called
                  int size_of_each_chunk =
                      mSimpleInterface.getSizeOfEachChunk(); // get this value from the server
                  int[] imageData = new int[no * size_of_each_chunk];
                  // for(int e=0;e<12;e++)
                  // {
                  for (int i = 0; i < no; i++) {
                    System.out.println("hahaha");
                    int[] data_from_server = new int[size_of_each_chunk];
                    data_from_server = mSimpleInterface.giveActualImageData(clientid, i);
                    System.arraycopy(
                        data_from_server,
                        0,
                        imageData,
                        i * size_of_each_chunk,
                        data_from_server.length);
                  }
                  // }

                  // start smoothing
                  // for( int e=0;e<12;e++)
                  // {
                  for (int k = 0; k < 50; k++) {
                    System.out.println("client iteration no. " + k + " and freq is " + frequency);
                    for (int i = 1; i < imageData.length - 1; i++) {
                      // first extract argb values from pixels
                      int a0 = Color.alpha(imageData[i - 1]);
                      int r0 = Color.red(imageData[i - 1]);
                      int g0 = Color.green(imageData[i - 1]);
                      int b0 = Color.blue(imageData[i - 1]);
                      int a = Color.alpha(imageData[i]);
                      int r = Color.red(imageData[i]);
                      int g = Color.green(imageData[i]);
                      int b = Color.blue(imageData[i]);
                      int a1 = Color.alpha(imageData[i + 1]);
                      int r1 = Color.red(imageData[i + 1]);
                      int g1 = Color.green(imageData[i + 1]);
                      int b1 = Color.blue(imageData[i + 1]);

                      int afinal = (a0 + a + a1) / 3;
                      int rfinal = (r0 + r + r1) / 3;
                      int gfinal = (g0 + g + g1) / 3;
                      int bfinal = (b0 + b + b1) / 3;

                      imageData[i] = Color.argb(afinal, rfinal, gfinal, bfinal);
                    }

                    // facebook hackathon
                    // every 10 iterations you can send the smoothed data back to the server
                    if (k % 5 == 0) {
                      for (int i = 0; i < no; i++) {
                        int[] localarray = new int[size_of_each_chunk];
                        System.arraycopy(
                            imageData, i * size_of_each_chunk, localarray, 0, size_of_each_chunk);
                        mSimpleInterface.takeImageData(localarray, clientid, i);
                      }
                    }
                  } // this is the loop to repeat smoothing 20 30 40 50 times
                  // }
                  // for(int e=0;e<12;e++)
                  // {
                  //						   for(int i=0;i<no;i++)
                  //						   {
                  //							    int[] localarray=new int[size_of_each_chunk];
                  //								System.arraycopy(imageData, i*size_of_each_chunk, localarray, 0,
                  // size_of_each_chunk);
                  //							    mSimpleInterface.takeImageData(localarray,clientid,i);
                  //						   }
                  // }
                  timer_flag = 0;
                  File file1 = new File(Environment.getExternalStorageDirectory(), "done_file");
                  file1.createNewFile();
                } // end if clientid != -1
              } catch (Exception e) {
                // TODO Auto-generated catch block
                System.out.println("damn an error occurred");
                e.printStackTrace();
              }
            }

            break;
          }

        case DISCONNECT:
          {
            mIsStoppingDiscovery = true;
            if (mIsConnected) {
              Status status = mBus.leaveSession(mSessionId);
              logStatus("BusAttachment.leaveSession()", status);
            }
            mBus.disconnect();
            getLooper().quit();

            break;
          }

        case PING:
          {
            try {
              if (mSimpleInterface != null) {

                sendUiMessage(MESSAGE_PING, msg.obj);
                int[] values = {2, 4, 6, 8, 10, 255, 1024, 1056, 0, 9999};
                double[] array = new double[capacity];
                for (int t = 0; t < capacity; t++) array[t] = (int) (Math.random() * 10);
                String reply = null;
                for (int i = 0; i < no_of_times; i++)
                  reply = mSimpleInterface.Ping(array, "client2");
                sendUiMessage(MESSAGE_PING_REPLY, reply);
              }
            } catch (BusException ex) {
              logException("SimpleInterface.Ping()", ex);
            }
            break;
          }
        default:
          break;
      }
    }