module JamRuby # creates messages (implementation: protocol buffer) objects cleanly class MessageFactory CLIENT_TARGET = "client" SERVER_TARGET = "server" SESSION_TARGET_PREFIX = "session:" USER_TARGET_PREFIX = "user:" CLIENT_TARGET_PREFIX = "client:" def initialize() @type_values = {} Jampb::ClientMessage::Type.constants.each do |constant| @type_values[Jampb::ClientMessage::Type.const_get(constant)] = constant end end # given a string (bytes) payload, return a client message def parse_client_msg(payload) return Jampb::ClientMessage.parse(payload) end # create a login message using user/pass def login_with_user_pass(username, password, options = {}) login = Jampb::Login.new( :username => username, :password => password, :client_id => options[:client_id] ) return Jampb::ClientMessage.new( :type => ClientMessage::Type::LOGIN, :route_to => SERVER_TARGET, :login => login ) end # create a login message using token (a cookie or similar) def login_with_token(token, options = {}) login = Jampb::Login.new( :token => token, :client_id => options[:client_id] ) return Jampb::ClientMessage.new( :type => ClientMessage::Type::LOGIN, :route_to => SERVER_TARGET, :login => login ) end # create a login ack (login was successful) def login_ack(public_ip, client_id, token, heartbeat_interval, music_session_id, reconnected, user_id) login_ack = Jampb::LoginAck.new( :public_ip => public_ip, :client_id => client_id, :token => token, :heartbeat_interval => heartbeat_interval, :music_session_id => music_session_id, :reconnected => reconnected, :user_id => user_id ) return Jampb::ClientMessage.new( :type => ClientMessage::Type::LOGIN_ACK, :route_to => CLIENT_TARGET, :login_ack => login_ack ) end def download_available download_available = Jampb::DownloadAvailable.new return Jampb::ClientMessage.new( :type => ClientMessage::Type::DOWNLOAD_AVAILABLE, :route_to => CLIENT_TARGET, :download_available => download_available ) end # create a music session login message def login_music_session(music_session) login_music_session = Jampb::LoginMusicSession.new(:music_session => music_session) return Jampb::ClientMessage.new( :type => ClientMessage::Type::LOGIN_MUSIC_SESSION, :route_to => SERVER_TARGET, :login_music_session => login_music_session ) end # create a music session login message ack (success or on failure) def login_music_session_ack(error, error_reason) login_music_session_ack = Jampb::LoginMusicSessionAck.new(:error => error, :error_reason => error_reason) return Jampb::ClientMessage.new( :type => ClientMessage::Type::LOGIN_MUSIC_SESSION_ACK, :route_to => CLIENT_TARGET, :login_music_session_ack => login_music_session_ack ) end # create a music session 'leave session' message def leave_music_session(music_session) leave_music_session = Jampb::LeaveMusicSession.new(:music_session => music_session) return Jampb::ClientMessage.new( :type => ClientMessage::Type::LEAVE_MUSIC_SESSION, :route_to => SERVER_TARGET, :leave_music_session => leave_music_session ) end # create a music session leave message ack (success or on failure) def leave_music_session_ack(error, error_reason) leave_music_session_ack = Jampb::LeaveMusicSessionAck.new(:error => error, :error_reason => error_reason) return Jampb::ClientMessage.new( :type => ClientMessage::Type::LEAVE_MUSIC_SESSION_ACK, :route_to => CLIENT_TARGET, :leave_music_session_ack => leave_music_session_ack ) end # create a heartbeat def heartbeat() heartbeat = Jampb::Heartbeat.new return Jampb::ClientMessage.new( :type => ClientMessage::Type::HEARTBEAT, :route_to => SERVER_TARGET, :heartbeat => heartbeat ) end # create a heartbeat ack def heartbeat_ack() heartbeat_ack = Jampb::HeartbeatAck.new return Jampb::ClientMessage.new( :type => ClientMessage::Type::HEARTBEAT_ACK, :route_to => CLIENT_TARGET, :heartbeat_ack => heartbeat_ack ) end # create a server bad state recovered msg def server_bad_state_recovered(original_message_id) recovered = Jampb::ServerBadStateRecovered.new() return Jampb::ClientMessage.new( :type => ClientMessage::Type::SERVER_BAD_STATE_RECOVERED, :route_to => CLIENT_TARGET, :server_bad_state_recovered => recovered, :in_reply_to => original_message_id ) end # create a server error def server_generic_error(error_msg) error = Jampb::ServerGenericError.new(:error_msg => error_msg) return Jampb::ClientMessage.new( :type => ClientMessage::Type::SERVER_GENERIC_ERROR, :route_to => CLIENT_TARGET, :server_generic_error => error ) end # create a server rejection error def server_rejection_error(error_msg) error = Jampb::ServerRejectionError.new(:error_msg => error_msg) return Jampb::ClientMessage.new( :type => ClientMessage::Type::SERVER_REJECTION_ERROR, :route_to => CLIENT_TARGET, :server_rejection_error => error ) end # create a server rejection error def server_permission_error(original_message_id, error_msg) error = Jampb::ServerPermissionError.new(:error_msg => error_msg) return Jampb::ClientMessage.new( :type => ClientMessage::Type::SERVER_PERMISSION_ERROR, :route_to => CLIENT_TARGET, :server_permission_error => error, :in_reply_to => original_message_id ) end # create a server bad state error def server_bad_state_error(original_message_id, error_msg) error = Jampb::ServerBadStateError.new(:error_msg => error_msg) return Jampb::ClientMessage.new( :type => ClientMessage::Type::SERVER_BAD_STATE_ERROR, :route_to => CLIENT_TARGET, :server_bad_state_error => error, :in_reply_to => original_message_id ) end ###################################### NOTIFICATIONS ###################################### # create a friend update message def friend_update(user_id, photo_url, online, msg) friend = Jampb::FriendUpdate.new( :user_id => user_id, :photo_url => photo_url, :online => online, :msg => msg ) return Jampb::ClientMessage.new( :type => ClientMessage::Type::FRIEND_UPDATE, :route_to => USER_TARGET_PREFIX + user_id, :friend_update => friend ) end # create a friend request message def friend_request(receiver_id, friend_request_id, photo_url, msg, notification_id, created_at) friend_request = Jampb::FriendRequest.new( :friend_request_id => friend_request_id, :photo_url => photo_url, :msg => msg, :notification_id => notification_id, :created_at => created_at ) return Jampb::ClientMessage.new( :type => ClientMessage::Type::FRIEND_REQUEST, :route_to => USER_TARGET_PREFIX + receiver_id, :friend_request => friend_request ) end # create a friend request acceptance message def friend_request_accepted(receiver_id, photo_url, msg, notification_id, created_at) friend_request_accepted = Jampb::FriendRequestAccepted.new( :photo_url => photo_url, :msg => msg, :notification_id => notification_id, :created_at => created_at ) return Jampb::ClientMessage.new( :type => ClientMessage::Type::FRIEND_REQUEST_ACCEPTED, :route_to => USER_TARGET_PREFIX + receiver_id, :friend_request_accepted => friend_request_accepted ) end def new_user_follower(receiver_id, photo_url, msg, notification_id, created_at) new_user_follower = Jampb::NewUserFollower.new( :photo_url => photo_url, :msg => msg, :notification_id => notification_id, :created_at => created_at ) return Jampb::ClientMessage.new( :type => ClientMessage::Type::NEW_USER_FOLLOWER, :route_to => USER_TARGET_PREFIX + receiver_id, :new_user_follower => new_user_follower ) end def new_band_follower(receiver_id, photo_url, msg, notification_id, created_at) new_band_follower = Jampb::NewBandFollower.new( :photo_url => photo_url, :msg => msg, :notification_id => notification_id, :created_at => created_at ) return Jampb::ClientMessage.new( :type => ClientMessage::Type::NEW_BAND_FOLLOWER, :route_to => USER_TARGET_PREFIX + receiver_id, :new_band_follower => new_band_follower ) end def session_invitation(receiver_id, session_id, msg, notification_id, created_at) session_invitation = Jampb::SessionInvitation.new( :session_id => session_id, :msg => msg, :notification_id => notification_id, :created_at => created_at ) return Jampb::ClientMessage.new( :type => ClientMessage::Type::SESSION_INVITATION, :route_to => USER_TARGET_PREFIX + receiver_id, :session_invitation => session_invitation ) end # create a join request session message def join_request(join_request_id, session_id, photo_url, msg, notification_id, created_at) req = Jampb::JoinRequest.new( :join_request_id => join_request_id, :session_id => session_id, :photo_url => photo_url, :msg => msg, :notification_id => notification_id, :created_at => created_at ) return Jampb::ClientMessage.new( :type => ClientMessage::Type::JOIN_REQUEST, :route_to => SESSION_TARGET_PREFIX + session_id, :join_request => req ) end # create a join request approved session message def join_request_approved(join_request_id, session_id, photo_url, msg, notification_id, created_at) req_approved = Jampb::JoinRequestApproved.new( :join_request_id => join_request_id, :session_id => session_id, :photo_url => photo_url, :msg => msg, :notification_id => notification_id, :created_at => created_at ) return Jampb::ClientMessage.new( :type => ClientMessage::Type::JOIN_REQUEST_APPROVED, :route_to => SESSION_TARGET_PREFIX + session_id, :join_request_approved => req_approved ) end # create a join request rejected session message def join_request_rejected(join_request_id, session_id, photo_url, msg, notification_id, created_at) req_rejected = Jampb::JoinRequestRejected.new( :join_request_id => join_request_id, :session_id => session_id, :photo_url => photo_url, :msg => msg, :notification_id => notification_id, :created_at => created_at ) return Jampb::ClientMessage.new( :type => ClientMessage::Type::JOIN_REQUEST_REJECTED, :route_to => SESSION_TARGET_PREFIX + session_id, :join_request_rejected => req_rejected ) end def session_join(session_id, photo_url, msg) join = Jampb::SessionJoin.new( :session_id => session_id, :photo_url => photo_url, :msg => msg ) return Jampb::ClientMessage.new( :type => ClientMessage::Type::SESSION_JOIN, :route_to => CLIENT_TARGET, :session_join => join ) end def session_depart(session_id, photo_url, msg, recording_id = nil) left = Jampb::SessionDepart.new( :session_id => session_id, :photo_url => photo_url, :msg => msg, :recording_id => recording_id ) return Jampb::ClientMessage.new( :type => ClientMessage::Type::SESSION_DEPART, :route_to => CLIENT_TARGET, :session_depart => left ) end def musician_session_join(receiver_id, session_id, photo_url, msg, notification_id, created_at) musician_session_join = Jampb::MusicianSessionJoin.new( :session_id => session_id, :photo_url => photo_url, :msg => msg, :notification_id => notification_id, :created_at => created_at ) return Jampb::ClientMessage.new( :type => ClientMessage::Type::MUSICIAN_SESSION_JOIN, :route_to => USER_TARGET_PREFIX + receiver_id, :musician_session_join => musician_session_join ) end def band_session_join(receiver_id, session_id, photo_url, msg, notification_id, created_at) band_session_join = Jampb::BandSessionJoin.new( :session_id => session_id, :photo_url => photo_url, :msg => msg, :notification_id => notification_id, :created_at => created_at ) return Jampb::ClientMessage.new( :type => ClientMessage::Type::BAND_SESSION_JOIN, :route_to => USER_TARGET_PREFIX + receiver_id, :band_session_join => band_session_join ) end def musician_recording_saved(receiver_id, recording_id, photo_url, msg, notification_id, created_at) musician_recording_saved = Jampb::MusicianRecordingSaved.new( :recording_id => recording_id, :photo_url => photo_url, :msg => msg, :notification_id => notification_id, :created_at => created_at ) return Jampb::ClientMessage.new( :type => ClientMessage::Type::MUSICIAN_RECORDING_SAVED, :route_to => USER_TARGET_PREFIX + receiver_id, :musician_recording_saved => musician_recording_saved ) end def band_recording_saved(receiver_id, recording_id, photo_url, msg, notification_id, created_at) band_recording_saved = Jampb::BandRecordingSaved.new( :recording_id => recording_id, :photo_url => photo_url, :msg => msg, :notification_id => notification_id, :created_at => created_at ) return Jampb::ClientMessage.new( :type => ClientMessage::Type::BAND_RECORDING_SAVED, :route_to => USER_TARGET_PREFIX + receiver_id, :band_recording_saved => band_recording_saved ) end def recording_started(receiver_id, photo_url, msg) recording_started = Jampb::RecordingStarted.new( :photo_url => photo_url, :msg => msg ) return Jampb::ClientMessage.new( :type => ClientMessage::Type::RECORDING_STARTED, :route_to => USER_TARGET_PREFIX + receiver_id, :recording_started => recording_started ) end def recording_ended(receiver_id, photo_url, msg) recording_ended = Jampb::RecordingEnded.new( :photo_url => photo_url, :msg => msg ) return Jampb::ClientMessage.new( :type => ClientMessage::Type::RECORDING_ENDED, :route_to => USER_TARGET_PREFIX + receiver_id, :recording_ended => recording_ended ) end def recording_master_mix_complete end # create a band invitation message def band_invitation(receiver_id, invitation_id, band_id, photo_url, msg, notification_id, created_at) band_invitation = Jampb::BandInvitation.new( :band_invitation_id => invitation_id, :band_id => band_id, :photo_url => photo_url, :msg => msg, :notification_id => notification_id, :created_at => created_at ) return Jampb::ClientMessage.new( :type => ClientMessage::Type::BAND_INVITATION, :route_to => USER_TARGET_PREFIX + receiver_id, :band_invitation => band_invitation ) end # create a band invitation acceptance message def band_invitation_accepted(receiver_id, invitation_id, photo_url, msg, notification_id, created_at) band_invitation_accepted = Jampb::BandInvitationAccepted.new( :band_invitation_id => invitation_id, :photo_url => photo_url, :msg => msg, :notification_id => notification_id, :created_at => created_at ) return Jampb::ClientMessage.new( :type => ClientMessage::Type::BAND_INVITATION_ACCEPTED, :route_to => USER_TARGET_PREFIX + receiver_id, :band_invitation_accepted => band_invitation_accepted ) end # create a musician fresh session message def musician_session_fresh(session_id, user_id, username, photo_url) fresh = Jampb::MusicianSessionFresh.new( :session_id => session_id, :user_id => user_id, :username => username, :photo_url => photo_url ) return Jampb::ClientMessage.new( :type => ClientMessage::Type::MUSICIAN_SESSION_FRESH, :route_to => CLIENT_TARGET, :musician_session_fresh => fresh ) end # create a musician stale session message def musician_session_stale(session_id, user_id, username, photo_url) stale = Jampb::MusicianSessionStale.new( :session_id => session_id, :user_id => user_id, :username => username, :photo_url => photo_url ) return Jampb::ClientMessage.new( :type => ClientMessage::Type::MUSICIAN_SESSION_STALE, :route_to => CLIENT_TARGET, :musician_session_stale => stale ) end # create a test message to send in session def test_session_message(session_id, msg) test = Jampb::TestSessionMessage.new(:msg => msg) return Jampb::ClientMessage.new( :type => ClientMessage::Type::TEST_SESSION_MESSAGE, :route_to => SESSION_TARGET_PREFIX + session_id, :test_session_message => test ) end ############## P2P CLIENT MESSAGES ################# # send a request to do a ping def ping_request(client_id, from) ping_request = Jampb::PingRequest.new() return Jampb::ClientMessage.new( :type => ClientMessage::Type::PING_REQUEST, :route_to => CLIENT_TARGET_PREFIX + client_id, :from => from, :ping_request => ping_request ) end # respond to a ping_request with an ack def ping_ack(client_id, from) ping_ack = Jampb::PingAck.new return Jampb::ClientMessage.new( :type => ClientMessage::Type::PING_ACK, :route_to => CLIENT_TARGET_PREFIX + client_id, :from => from, :ping_ack => ping_ack ) end # create a test message to send in session def test_client_message(client_id, from, msg) test = Jampb::TestClientMessage.new(:msg => msg) return Jampb::ClientMessage.new( :type => ClientMessage::Type::TEST_CLIENT_MESSAGE, :route_to => CLIENT_TARGET_PREFIX + client_id, :from => from, :test_client_message => test ) end #################################################### # is this message directed to the server? def server_directed? msg return msg.route_to == MessageFactory::SERVER_TARGET end # is this message directed to the client? def client_directed? msg return msg.route_to.start_with? MessageFactory::CLIENT_TARGET_PREFIX end # is this message directed to a (music) session? def session_directed? msg return msg.route_to.start_with? MessageFactory::SESSION_TARGET_PREFIX end # is this message directed to a user? def user_directed? msg return msg.route_to.start_with? MessageFactory::USER_TARGET_PREFIX end def extract_session(msg) return msg.route_to[MessageFactory::SESSION_TARGET_PREFIX..-1] end def get_message_type msg return @type_values[msg.type] end end end