Android Open Source - steamchat Steam Event Bus






From Project

Back to project page steamchat.

License

The source code is released under:

Apache License

If you think the Android project steamchat listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.kevelbreh.steamchat.steam2;
//from  ww  w. j  a v a  2s .  c o  m
import android.os.Bundle;

import com.kevelbreh.steamchat.SteamChat;

import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingQueue;


public class SteamEventBus extends Thread {

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD})
    public @interface SteamEvent {
        int event() default 0;
        boolean isProto() default true;
    }

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD})
    public @interface UserEvent {
        int event() default 0;
    }

    /**
     * Steam service.
     */
    private SteamService mSteamService;

    /**
     * Map of the cached handlers.
     */
    private ArrayList<Method> mSteamHandlers;
    private ArrayList<Method> mUserHandlers;


    private LinkedBlockingQueue<EventStructureInterface> mEventQueue;

    /**
     *
     * @param service that this event bus runs on.
     */
    public SteamEventBus(SteamService service) {
        super("SteamEventBus-Thread");
        mSteamService = service;

        mEventQueue = new LinkedBlockingQueue<EventStructureInterface>();

        mSteamHandlers = new ArrayList<Method>();
        mUserHandlers = new ArrayList<Method>();
    }

    /**
     *
     * @param handler that will handle certain events.
     */
    public void register(Class handler) {
        for (Method method : handler.getDeclaredMethods()) {
            Annotation steam = method.getAnnotation(SteamEvent.class);
            Annotation user = method.getAnnotation(UserEvent.class);

            // Only allowed to handle 1 event per method handle!
            if (steam != null && user != null) {
                continue;
            }

            if (steam != null) {
                mSteamHandlers.add(method);
                continue;
            }

            if (user != null) {
                mUserHandlers.add(method);
            }
        }
    }

    /**
     * Handle events fired from {@link com.kevelbreh.steamchat.steam2.SteamConnection}. Decided to
     * go against sending packet objects to reduce the object boiler plate. Primitive types will do
     * just fine.
     *
     * @param event {@link com.kevelbreh.steamchat.steam.language.Language.Message} of the event.
     * @param proto of flag if the data is protobuf backed or not.
     * @param data of the packet (full data).
     */
    public synchronized void handleSteamEvent(int event, boolean proto, final byte[] data) {
        SteamChat.debug(this, "SteamEvent: event=" + event + " proto=" + proto + " length="+data.length);
        mEventQueue.offer(new SteamEventStructure(event, proto, data));
    }

    /**
     * Handle events from the user or other events fired that are not from steam.
     *
     * @param event type.
     * @param data bundle which can be null.
     */
    public synchronized void handleUserEvent(int event, final Bundle bundle) {
        SteamChat.debug(this, "UserEvent: event=" + event);
        mEventQueue.offer(new UserEventStructure(event, bundle));
    }

    @Override
    public void run() {
        while (true) {
            try {
                while (!mEventQueue.isEmpty()) {
                    EventStructureInterface event = mEventQueue.take();

                    if (event.getType() == 1) {
                        SteamEventStructure steamEvent = (SteamEventStructure) event;
                        for (Method method : mSteamHandlers) {
                            SteamEvent annotation = method.getAnnotation(SteamEvent.class);
                            if (annotation.event() == steamEvent.event && annotation.isProto() == steamEvent.isProto) {
                                try {
                                    method.invoke(this, mSteamService, steamEvent.data);
                                }
                                catch(Exception e) {
                                    SteamChat.debug(this, "Failed to invoke handle", e);
                                }
                            }
                        }
                        continue;
                    }

                    if (event.getType() == 2) {
                        UserEventStructure userEvent = (UserEventStructure) event;
                        for (Method method : mUserHandlers) {
                            UserEvent annotation = method.getAnnotation(UserEvent.class);
                            if (annotation.event() == userEvent.event) {
                                try {
                                    method.invoke(this, mSteamService, userEvent.data);
                                }
                                catch(Exception e) {
                                    SteamChat.debug(this, "Failed to invoke handle", e);
                                }
                            }
                        }
                    }
                }

                sleep(1000);
            }
            catch(final InterruptedException e) {
                SteamChat.debug(this, e.getMessage(), e);
                break;
            }
        }
    }

    /**
     *
     */
    private class SteamEventStructure implements EventStructureInterface {
        public int event;
        public boolean isProto;
        public byte[] data;

        public SteamEventStructure(final int event, final boolean isProto, final byte[] data) {
            this.event = event;
            this.isProto = isProto;
            this.data = data;
        }

        @Override
        public int getType() {
            return 1;
        }
    }

    /**
     *
     */
    private class UserEventStructure implements EventStructureInterface {
        public int event;
        public Bundle data;

        public UserEventStructure(final int event, final Bundle data) {
            this.event = event;
            this.data = data;
        }

        @Override
        public int getType() {
            return 2;
        }
    }

    private interface EventStructureInterface {
        public int getType();
    }
}




Java Source Code List

com.kevelbreh.steamchat.SteamChat.java
com.kevelbreh.steamchat.account.AuthenticatorService.java
com.kevelbreh.steamchat.account.Authenticator.java
com.kevelbreh.steamchat.account.SteamAccount.java
com.kevelbreh.steamchat.activity.AuthenticationActivity.java
com.kevelbreh.steamchat.activity.ConversationActivity.java
com.kevelbreh.steamchat.activity.FriendsActivity.java
com.kevelbreh.steamchat.activity.InteractionsActivity.java
com.kevelbreh.steamchat.activity.LauncherActivity.java
com.kevelbreh.steamchat.activity.SettingsActivity.java
com.kevelbreh.steamchat.fragment.ConversationFragment.java
com.kevelbreh.steamchat.fragment.FriendsFragment.java
com.kevelbreh.steamchat.fragment.InteractionsFragment.java
com.kevelbreh.steamchat.fragment.SettingsFragment.java
com.kevelbreh.steamchat.provider.SteamProviderUtils.java
com.kevelbreh.steamchat.provider.SteamProvider.java
com.kevelbreh.steamchat.provider.content.InteractionContentItem.java
com.kevelbreh.steamchat.provider.content.PersonaContentItem.java
com.kevelbreh.steamchat.provider.content.UserContentItem.java
com.kevelbreh.steamchat.steam2.SteamConnection.java
com.kevelbreh.steamchat.steam2.SteamEventBus.java
com.kevelbreh.steamchat.steam2.SteamService.java
com.kevelbreh.steamchat.steam2.handler.ConnectionHandler.java
com.kevelbreh.steamchat.steam2.handler.FriendHandler.java
com.kevelbreh.steamchat.steam2.handler.MessageHandler.java
com.kevelbreh.steamchat.steam2.handler.UserHandler.java
com.kevelbreh.steamchat.steam2.packet.Packet.java
com.kevelbreh.steamchat.steam2.packet.ProtoPacket.java
com.kevelbreh.steamchat.steam.SteamClient.java
com.kevelbreh.steamchat.steam.SteamID.java
com.kevelbreh.steamchat.steam.SteamServiceHandler.java
com.kevelbreh.steamchat.steam.SteamService.java
com.kevelbreh.steamchat.steam.handler2.ConnectionHandler.java
com.kevelbreh.steamchat.steam.handler2.FriendHandler.java
com.kevelbreh.steamchat.steam.handler2.Handler.java
com.kevelbreh.steamchat.steam.handler2.UserHandler.java
com.kevelbreh.steamchat.steam.handler.AEventHandler.java
com.kevelbreh.steamchat.steam.handler.AuthenticationHandler.java
com.kevelbreh.steamchat.steam.handler.FriendsHandler.java
com.kevelbreh.steamchat.steam.handler.IEventHandler.java
com.kevelbreh.steamchat.steam.handler.IHandler.java
com.kevelbreh.steamchat.steam.handler.MessageDebugHandler.java
com.kevelbreh.steamchat.steam.language.Language.java
com.kevelbreh.steamchat.steam.language.Message.java
com.kevelbreh.steamchat.steam.network.TCPConnection.java
com.kevelbreh.steamchat.steam.network.packet.ChannelEncryptRequest.java
com.kevelbreh.steamchat.steam.network.packet.ChannelEncryptResponse.java
com.kevelbreh.steamchat.steam.network.packet.ChannelEncryptResult.java
com.kevelbreh.steamchat.steam.network.packet.ClientLogOnResponse.java
com.kevelbreh.steamchat.steam.network.packet.ClientLogOn.java
com.kevelbreh.steamchat.steam.network.packet.HeartBeat.java
com.kevelbreh.steamchat.steam.network.packet.MultiPacket.java
com.kevelbreh.steamchat.steam.network.packet.Packet.java
com.kevelbreh.steamchat.steam.network.packet.ProtoPacket.java
com.kevelbreh.steamchat.steam.proto.DescriptorsProto.java
com.kevelbreh.steamchat.steam.proto.EncryptedAppTicketProto.java
com.kevelbreh.steamchat.steam.proto.SteamMessagesBaseProto.java
com.kevelbreh.steamchat.steam.proto.SteamMessagesClientServerProto.java
com.kevelbreh.steamchat.steam.security.AsnKeyParser.java
com.kevelbreh.steamchat.steam.security.AsnParser.java
com.kevelbreh.steamchat.steam.security.BerDecodeException.java
com.kevelbreh.steamchat.steam.security.Cryptography.java
com.kevelbreh.steamchat.steam.security.NetEncryption.java
com.kevelbreh.steamchat.steam.security.PublicKey.java
com.kevelbreh.steamchat.steam.security.RSA.java
com.kevelbreh.steamchat.steam.util.BinaryReader.java
com.kevelbreh.steamchat.steam.util.BinaryWriter.java
com.kevelbreh.steamchat.steam.util.BitVector64.java
com.kevelbreh.steamchat.steam.util.JenkinsHash.java
com.kevelbreh.steamchat.util.AServiceActivity.java
com.kevelbreh.steamchat.util.Dump.java
com.kevelbreh.steamchat.util.MiscUtils.java
com.kevelbreh.steamchat.widget.adapter.ChatAdapter.java
com.kevelbreh.steamchat.widget.adapter.ConversationAdapter.java
com.kevelbreh.steamchat.widget.adapter.FriendAdapter.java
com.kevelbreh.steamchat.widget.view.AvatarView.java
com.kevelbreh.steamchat.widget.view.FriendInteractionsView.java