Przeglądaj źródła

mqtt 3.1.1 protocol

Feng Lee 11 lat temu
rodzic
commit
c9d3e2d291

+ 5 - 0
CHANGELOG.md

@@ -1,6 +1,11 @@
 eMQTT ChangeLog
 ==================
 
+TODO: 0.2.1 (2014-12-31)
+-------------------
+
+Pass MQTT 3.1.1 Tests
+
 0.2.0 (2014-12-07)
 -------------------
 

+ 2 - 2
apps/emqtt/include/emqtt.hrl

@@ -1,5 +1,5 @@
 %%------------------------------------------------------------------------------
-%% Copyright (c) 2014, Feng Lee <feng@slimchat.io>
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
 %% 
 %% Permission is hereby granted, free of charge, to any person obtaining a copy
 %% of this software and associated documentation files (the "Software"), to deal
@@ -23,7 +23,7 @@
 %% ---------------------------------
 %% banner
 %% ---------------------------------
--define(COPYRIGHT, "Copyright (C) 2014, Feng Lee<feng@slimchat.io>").
+-define(COPYRIGHT, "Copyright (C) 2014, Feng Lee<feng@emqtt.io>").
 
 -define(LICENSE_MESSAGE, "Licensed under MIT"). 
 

+ 1 - 1
apps/emqtt/include/emqtt_topic.hrl

@@ -1,5 +1,5 @@
 %%-----------------------------------------------------------------------------
-%% Copyright (c) 2014, Feng Lee <feng@slimchat.io>
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
 %% 
 %% Permission is hereby granted, free of charge, to any person obtaining a copy
 %% of this software and associated documentation files (the "Software"), to deal

+ 1 - 1
apps/emqtt/src/emqtt.erl

@@ -1,5 +1,5 @@
 %%-----------------------------------------------------------------------------
-%% Copyright (c) 2014, Feng Lee <feng@slimchat.io>
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
 %% 
 %% Permission is hereby granted, free of charge, to any person obtaining a copy
 %% of this software and associated documentation files (the "Software"), to deal

+ 2 - 2
apps/emqtt/src/emqtt_app.erl

@@ -1,5 +1,5 @@
 %%-----------------------------------------------------------------------------
-%% Copyright (c) 2014, Feng Lee <feng@slimchat.io>
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
 %% 
 %% Permission is hereby granted, free of charge, to any person obtaining a copy
 %% of this software and associated documentation files (the "Software"), to deal
@@ -22,7 +22,7 @@
 
 -module(emqtt_app).
 
--author('feng@slimchat.io').
+-author('feng@emqtt.io').
 
 -behaviour(application).
 

+ 2 - 2
apps/emqtt/src/emqtt_auth.erl

@@ -1,5 +1,5 @@
 %%-----------------------------------------------------------------------------
-%% Copyright (c) 2014, Feng Lee <feng@slimchat.io>
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
 %% 
 %% Permission is hereby granted, free of charge, to any person obtaining a copy
 %% of this software and associated documentation files (the "Software"), to deal
@@ -22,7 +22,7 @@
 
 -module(emqtt_auth).
 
--author('feng@slimchat.io').
+-author('feng@emqtt.io').
 
 -include("emqtt.hrl").
 

+ 2 - 2
apps/emqtt/src/emqtt_auth_anonymous.erl

@@ -1,5 +1,5 @@
 %%-----------------------------------------------------------------------------
-%% Copyright (c) 2014, Feng Lee <feng@slimchat.io>
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
 %% 
 %% Permission is hereby granted, free of charge, to any person obtaining a copy
 %% of this software and associated documentation files (the "Software"), to deal
@@ -22,7 +22,7 @@
 
 -module(emqtt_auth_anonymous).
 
--author('feng@slimchat.io').
+-author('feng@emqtt.io').
 
 -export([init/1,
 		add/2,

+ 2 - 2
apps/emqtt/src/emqtt_auth_internal.erl

@@ -1,5 +1,5 @@
 %%-----------------------------------------------------------------------------
-%% Copyright (c) 2014, Feng Lee <feng@slimchat.io>
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
 %% 
 %% Permission is hereby granted, free of charge, to any person obtaining a copy
 %% of this software and associated documentation files (the "Software"), to deal
@@ -22,7 +22,7 @@
 
 -module(emqtt_auth_internal).
 
--author('feng@slimchat.io').
+-author('feng@emqtt.io').
 
 -include("emqtt.hrl").
 

+ 2 - 2
apps/emqtt/src/emqtt_client.erl

@@ -1,5 +1,5 @@
 %%-----------------------------------------------------------------------------
-%% Copyright (c) 2014, Feng Lee <feng@slimchat.io>
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
 %% 
 %% Permission is hereby granted, free of charge, to any person obtaining a copy
 %% of this software and associated documentation files (the "Software"), to deal
@@ -22,7 +22,7 @@
 
 -module(emqtt_client).
 
--author('feng@slimchat.io').
+-author('feng@emqtt.io').
 
 -behaviour(gen_server).
 

+ 2 - 2
apps/emqtt/src/emqtt_cm.erl

@@ -1,5 +1,5 @@
 %%-----------------------------------------------------------------------------
-%% Copyright (c) 2014, Feng Lee <feng@slimchat.io>
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
 %% 
 %% Permission is hereby granted, free of charge, to any person obtaining a copy
 %% of this software and associated documentation files (the "Software"), to deal
@@ -23,7 +23,7 @@
 %client manager
 -module(emqtt_cm).
 
--author('feng@slimchat.io').
+-author('feng@emqtt.io').
 
 -behaviour(gen_server).
 

+ 2 - 2
apps/emqtt/src/emqtt_ctl.erl

@@ -1,5 +1,5 @@
 %%-----------------------------------------------------------------------------
-%% Copyright (c) 2014, Feng Lee <feng@slimchat.io>
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
 %% 
 %% Permission is hereby granted, free of charge, to any person obtaining a copy
 %% of this software and associated documentation files (the "Software"), to deal
@@ -22,7 +22,7 @@
 
 -module(emqtt_ctl).
 
--author('feng@slimchat.io').
+-author('feng@emqtt.io').
 
 -include("emqtt.hrl").
 

+ 2 - 2
apps/emqtt/src/emqtt_db.erl

@@ -1,5 +1,5 @@
 %%-----------------------------------------------------------------------------
-%% Copyright (c) 2014, Feng Lee <feng@slimchat.io>
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
 %% 
 %% Permission is hereby granted, free of charge, to any person obtaining a copy
 %% of this software and associated documentation files (the "Software"), to deal
@@ -22,7 +22,7 @@
 
 -module(emqtt_db).
 
--author('feng@slimchat.io').
+-author('feng@emqtt.io').
 
 -export([init/0, stop/0]).
 

+ 1 - 1
apps/emqtt/src/emqtt_frame.erl

@@ -1,5 +1,5 @@
 %%------------------------------------------------------------------------------
-%% Copyright (c) 2014, Feng Lee <feng@slimchat.io>
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
 %% 
 %% Permission is hereby granted, free of charge, to any person obtaining a copy
 %% of this software and associated documentation files (the "Software"), to deal

+ 2 - 2
apps/emqtt/src/emqtt_http.erl

@@ -1,5 +1,5 @@
 %%------------------------------------------------------------------------------
-%% Copyright (c) 2014, Feng Lee <feng@slimchat.io>
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
 %% 
 %% Permission is hereby granted, free of charge, to any person obtaining a copy
 %% of this software and associated documentation files (the "Software"), to deal
@@ -22,7 +22,7 @@
 
 -module(emqtt_http).
 
--author('feng@slimchat.io').
+-author('feng@emqtt.io').
 
 -include("emqtt.hrl").
 

+ 2 - 2
apps/emqtt/src/emqtt_keep_alive.erl

@@ -1,5 +1,5 @@
 %%-----------------------------------------------------------------------------
-%% Copyright (c) 2014, Feng Lee <feng@slimchat.io>
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
 %% 
 %% Permission is hereby granted, free of charge, to any person obtaining a copy
 %% of this software and associated documentation files (the "Software"), to deal
@@ -22,7 +22,7 @@
 
 -module(emqtt_keep_alive).
 
--author('feng@slimchat.io').
+-author('feng@emqtt.io').
 
 -export([new/2,
 		state/1,

+ 2 - 2
apps/emqtt/src/emqtt_monitor.erl

@@ -1,5 +1,5 @@
 %%-----------------------------------------------------------------------------
-%% Copyright (c) 2014, Feng Lee <feng@slimchat.io>
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
 %% 
 %% Permission is hereby granted, free of charge, to any person obtaining a copy
 %% of this software and associated documentation files (the "Software"), to deal
@@ -22,7 +22,7 @@
 
 -module(emqtt_monitor).
 
--author('feng@slimchat.io').
+-author('feng@emqtt.io').
 
 -behavior(gen_server).
 

+ 2 - 2
apps/emqtt/src/emqtt_net.erl

@@ -1,5 +1,5 @@
 %%-----------------------------------------------------------------------------
-%% Copyright (c) 2014, Feng Lee <feng@slimchat.io>
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
 %% 
 %% Permission is hereby granted, free of charge, to any person obtaining a copy
 %% of this software and associated documentation files (the "Software"), to deal
@@ -22,7 +22,7 @@
 
 -module(emqtt_net).
 
--author('feng@slimchat.io').
+-author('feng@emqtt.io').
 
 -export([tcp_name/3, tcp_host/1, getopts/2, setopts/2, getaddr/2, port_to_listeners/1]).
 

+ 28 - 12
apps/emqtt/src/emqtt_protocol.erl

@@ -1,5 +1,5 @@
 %%-----------------------------------------------------------------------------
-%% Copyright (c) 2014, Feng Lee <feng@slimchat.io>
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
 %% 
 %% Permission is hereby granted, free of charge, to any person obtaining a copy
 %% of this software and associated documentation files (the "Software"), to deal
@@ -28,6 +28,7 @@
 
 -record(proto_state, {
 		socket,
+		connected = false, %received CONNECT action?
 		message_id,
 		client_id,
 		clean_sess,
@@ -43,8 +44,7 @@
 
 -export([info/1]).
 
--define(FRAME_TYPE(Frame, Type),
-        Frame = #mqtt_frame{ fixed = #mqtt_frame_fixed{ type = Type }}).
+-define(FRAME_TYPE(Type), #mqtt_frame{ fixed = #mqtt_frame_fixed{ type = Type }}).
 
 initial_state(Socket) ->
 	#proto_state{
@@ -71,6 +71,23 @@ info(#proto_state{ message_id	= MsgId,
 	State :: proto_state(),
 	NewState :: proto_state().
 
+
+%%CONNECT – Client requests a connection to a Server
+
+%%A Client can only send the CONNECT Packet once over a Network Connection. 369
+
+%%First CONNECT
+handle_frame(Frame = ?FRAME_TYPE(?CONNECT), State = #proto_state{connected = false}) ->
+		handle_connect(Frame, State#proto_state{connected = true});
+
+%%Sencond CONNECT
+handle_frame(?FRAME_TYPE(?CONNECT), State = #proto_state{connected = true}) ->
+		{error, bad_connect, State};
+
+%%Received other packets when CONNECT not arrived.
+handle_frame(_Frame, State = #proto_state{connected = false}) ->
+		{error, no_connected, State};
+
 handle_frame(Frame = #mqtt_frame{ fixed = #mqtt_frame_fixed{ type = Type }}, 
 				State = #proto_state{client_id = ClientId}) ->
 	lager:info("frame from ~s: ~p", [ClientId, Frame]),
@@ -81,14 +98,13 @@ handle_frame(Frame = #mqtt_frame{ fixed = #mqtt_frame_fixed{ type = Type }},
 		{error, Reason, State}
 	end.
 
-handle_request(?CONNECT,
-                #mqtt_frame{ variable = #mqtt_frame_connect{
-                                          username   = Username,
-                                          password   = Password,
-                                          proto_ver  = ProtoVersion,
-                                          clean_sess = CleanSess,
-										  keep_alive = AlivePeriod,
-                                          client_id  = ClientId } = Var}, State0 = #proto_state{socket = Sock}) ->
+handle_connect(#mqtt_frame{ variable = #mqtt_frame_connect{
+								username   = Username,
+								password   = Password,
+								proto_ver  = ProtoVersion,
+								clean_sess = CleanSess,
+								keep_alive = AlivePeriod,
+								client_id  = ClientId } = Var}, State0 = #proto_state{socket = Sock}) ->
     State = State0#proto_state{client_id = ClientId},
     {ReturnCode, State1} =
         case {lists:member(ProtoVersion, proplists:get_keys(?PROTOCOL_NAMES)),
@@ -117,7 +133,7 @@ handle_request(?CONNECT,
                 fixed = #mqtt_frame_fixed{ type = ?CONNACK },
 				    variable = #mqtt_frame_connack{
                         return_code = ReturnCode }}),
-    {ok, State1};
+    {ok, State1}.
 
 handle_request(?PUBLISH, Frame=#mqtt_frame{
 									fixed = #mqtt_frame_fixed{qos = ?QOS_0}}, State) ->

+ 2 - 2
apps/emqtt/src/emqtt_pubsub.erl

@@ -1,5 +1,5 @@
 %%-----------------------------------------------------------------------------
-%% Copyright (c) 2014, Feng Lee <feng@slimchat.io>
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
 %% 
 %% Permission is hereby granted, free of charge, to any person obtaining a copy
 %% of this software and associated documentation files (the "Software"), to deal
@@ -22,7 +22,7 @@
 
 -module(emqtt_pubsub).
 
--author('feng@slimchat.io').
+-author('feng@emqtt.io').
 
 -include("emqtt.hrl").
 

+ 1 - 1
apps/emqtt/src/emqtt_queue.erl

@@ -1,5 +1,5 @@
 %%-----------------------------------------------------------------------------
-%% Copyright (c) 2014, Feng Lee <feng@slimchat.io>
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
 %% 
 %% Permission is hereby granted, free of charge, to any person obtaining a copy
 %% of this software and associated documentation files (the "Software"), to deal

+ 2 - 2
apps/emqtt/src/emqtt_queue_sup.erl

@@ -1,5 +1,5 @@
 %%-----------------------------------------------------------------------------
-%% Copyright (c) 2014, Feng Lee <feng@slimchat.io>
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
 %% 
 %% Permission is hereby granted, free of charge, to any person obtaining a copy
 %% of this software and associated documentation files (the "Software"), to deal
@@ -21,7 +21,7 @@
 %%------------------------------------------------------------------------------
 -module(emqtt_queue_sup).
 
--author('feng@slimchat.io').
+-author('feng@emqtt.io').
 
 -behavior(supervisor).
 

+ 2 - 2
apps/emqtt/src/emqtt_retained.erl

@@ -1,5 +1,5 @@
 %%-----------------------------------------------------------------------------
-%% Copyright (c) 2014, Feng Lee <feng@slimchat.io>
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
 %% 
 %% Permission is hereby granted, free of charge, to any person obtaining a copy
 %% of this software and associated documentation files (the "Software"), to deal
@@ -22,7 +22,7 @@
 
 -module(emqtt_retained).
 
--author('feng@slimchat.io').
+-author('feng@emqtt.io').
 
 %%TODO: FIXME Later...
 

+ 2 - 1
apps/emqtt/src/emqtt_router.erl

@@ -1,5 +1,5 @@
 %%-----------------------------------------------------------------------------
-%% Copyright (c) 2014, Feng Lee <feng@slimchat.io>
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
 %% 
 %% Permission is hereby granted, free of charge, to any person obtaining a copy
 %% of this software and associated documentation files (the "Software"), to deal
@@ -89,3 +89,4 @@ retained(Msg = #mqtt_msg{retain = true, topic = Topic}) ->
 	emqtt_retained:insert(Topic, Msg), Msg;
 
 retained(Msg) -> Msg.
+

+ 24 - 0
apps/emqtt/src/emqtt_session.erl

@@ -0,0 +1,24 @@
+%%-----------------------------------------------------------------------------
+%% Copyright (c) 2015, Feng Lee <feng@emqtt.io>
+%% 
+%% Permission is hereby granted, free of charge, to any person obtaining a copy
+%% of this software and associated documentation files (the "Software"), to deal
+%% in the Software without restriction, including without limitation the rights
+%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+%% copies of the Software, and to permit persons to whom the Software is
+%% furnished to do so, subject to the following conditions:
+%% 
+%% The above copyright notice and this permission notice shall be included in all
+%% copies or substantial portions of the Software.
+%% 
+%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+%% SOFTWARE.
+%%------------------------------------------------------------------------------
+
+-module(emqtt_session).
+

+ 94 - 0
apps/emqtt/src/emqtt_sm.erl

@@ -1,5 +1,99 @@
+%%-----------------------------------------------------------------------------
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
+%% 
+%% Permission is hereby granted, free of charge, to any person obtaining a copy
+%% of this software and associated documentation files (the "Software"), to deal
+%% in the Software without restriction, including without limitation the rights
+%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+%% copies of the Software, and to permit persons to whom the Software is
+%% furnished to do so, subject to the following conditions:
+%% 
+%% The above copyright notice and this permission notice shall be included in all
+%% copies or substantial portions of the Software.
+%% 
+%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+%% SOFTWARE.
+%%------------------------------------------------------------------------------
+
+
+%%------------------------------------------------------------------------------
+%%
+%% The Session state in the Server consists of:
+%% The existence of a Session, even if the rest of the Session state is empty.
+%% The Client’s subscriptions.
+%% QoS 1 and QoS 2 messages which have been sent to the Client, but have not been completely
+%% acknowledged.
+%% QoS 1 and QoS 2 messages pending transmission to the Client.
+%% QoS 2 messages which have been received from the Client, but have not been completely
+%% acknowledged.
+%% Optionally, QoS 0 messages pending transmission to the Client.
+%%
+%%------------------------------------------------------------------------------
+
 -module(emqtt_sm).
 
 %%emqtt session manager...
 
 %%cleanSess: true | false
+
+-include("emqtt.hrl").
+
+-behaviour(gen_server).
+
+-define(SERVER, ?MODULE).
+
+%% ------------------------------------------------------------------
+%% API Function Exports
+%% ------------------------------------------------------------------
+
+-export([start_link/0]).
+
+-export([create/2, resume/2, destroy/1]).
+
+%% ------------------------------------------------------------------
+%% gen_server Function Exports
+%% ------------------------------------------------------------------
+
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
+         terminate/2, code_change/3]).
+
+%% ------------------------------------------------------------------
+%% API Function Definitions
+%% ------------------------------------------------------------------
+
+start_link() ->
+    gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
+
+create(ClientId, Pid) -> ok.
+
+resume(ClientId, Pid) -> ok.
+
+destroy(ClientId) -> ok.
+
+%% ------------------------------------------------------------------
+%% gen_server Function Definitions
+%% ------------------------------------------------------------------
+
+init(Args) ->
+    {ok, Args}.
+
+handle_call(_Request, _From, State) ->
+    {reply, ok, State}.
+
+handle_cast(_Msg, State) ->
+    {noreply, State}.
+
+handle_info(_Info, State) ->
+    {noreply, State}.
+
+terminate(_Reason, _State) ->
+    ok.
+
+code_change(_OldVsn, State, _Extra) ->
+    {ok, State}.
+

+ 2 - 2
apps/emqtt/src/emqtt_sup.erl

@@ -1,5 +1,5 @@
 %%-----------------------------------------------------------------------------
-%% Copyright (c) 2014, Feng Lee <feng@slimchat.io>
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
 %% 
 %% Permission is hereby granted, free of charge, to any person obtaining a copy
 %% of this software and associated documentation files (the "Software"), to deal
@@ -22,7 +22,7 @@
 
 -module(emqtt_sup).
 
--author('feng@slimchat.io').
+-author('feng@emqtt.io').
 
 -include("emqtt.hrl").
 

+ 2 - 2
apps/emqtt/src/emqtt_topic.erl

@@ -1,5 +1,5 @@
 %%-----------------------------------------------------------------------------
-%% Copyright (c) 2014, Feng Lee <feng@slimchat.io>
+%% Copyright (c) 2014, Feng Lee <feng@emqtt.io>
 %% 
 %% Permission is hereby granted, free of charge, to any person obtaining a copy
 %% of this software and associated documentation files (the "Software"), to deal
@@ -22,7 +22,7 @@
 
 -module(emqtt_topic).
 
--author('feng@slimchat.io').
+-author('feng@emqtt.io').
 
 -import(lists, [reverse/1]).
 

+ 1 - 0
data/.placeholder

@@ -0,0 +1 @@
+durable queue data...

+ 4 - 0
doc/protocol.md

@@ -30,3 +30,7 @@ Connection, others can span multiple consecutive Network Connections between a C
 
 An expression contained in a Subscription, to indicate an interest in one or more topics. A Topic Filter can include wildcard characters.
 
+
+## Packet Identifier
+
+