Skip to content

Commit

Permalink
Merge 21ba3d1 into 26ecaee
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaobin0860 committed Mar 22, 2017
2 parents 26ecaee + 21ba3d1 commit a1e7d8b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
26 changes: 13 additions & 13 deletions README.md
Expand Up @@ -219,18 +219,18 @@ end

## Advanced Struct Packaging

Often, thrift files will make use of `include` statements to share structs. This can present a namespacing problem if you're running several thrift servers or clients that all make use of a common thrift file. This is because each server or client will import the struct separately and produce incompatible structs.
Often, thrift files will make use of `include` statements to share structs. This can present a namespacing problem if you're running several thrift servers or clients that all make use of a common thrift file. This is because each server or client will import the struct separately and produce incompatible structs.

This can be mitigated by using shared structs in a common module and controlling how they're imported. To control the destination module, use the `dest_modules` keyword dict:

```elixir
defmodule Models do
use Riffed.Struct, dest_modules: [common_types: Common,
server_types: Server,
client_types: Client],
common_types: [:RequestContext, :User],
server_types: [:AccessControlList],
client_types: [:UserList]
defmodule Models do
use Rift.Struct, dest_modules: [common_types: Common,
server_types: Server,
client_types: Client],
common_types: [:RequestContext, :User],
server_types: [:AccessControlList],
client_types: [:UserList]

defenum Common.UserState do
:inactive -> 1
Expand All @@ -241,23 +241,23 @@ defmodule Models do
enumerize_struct Common.User, state: Common.UserState
end

defmodule Server do
use Riffed.Server, service: :server_thrift,
defmodule Server do
use Rift.Server, service: :server_thrift,
auto_import_structs: false,
structs: Models
...
end

defmodule Client do
use Riffed.Client,
use Rift.Client,
auto_import_structs: false,
structs: Models
...
end
```

The above configuration will produce three different modules, `Models.Common`, `Models.Server` and `Models.Client`. The `Models` module
is capable of serializing and deserializing all the types defined in the three submodules, and should be used as your `:structs` module in your client and servers.
The above configuration will produce three different modules, `Models.Common`, `Models.Server` and `Models.Client`. The `Models` module
is capable of serializing and deserializing all the types defined in the three submodules, and should be used as your `:structs` module in your client and servers.

As you can see above, you can also namespace enumerations.

Expand Down
12 changes: 6 additions & 6 deletions doc/GettingStarted.md
Expand Up @@ -2,7 +2,7 @@

This guide will bring you step-by-step through building your first Riffed server and client. The service will allow for registering, fetching, and banning users. An example of the completed tutorial can be found in `examples/tutorial/`.

We'll assume you already have a Mix project to work with called `riffed_tutorial`. Feel free to go create one if you don't, using `mix new riffed_tutorial --sup`. Then add Riffed as a dependency:
We'll assume you already have a Mix project to work with called `rift_tutorial`. Feel free to go create one if you don't, using `mix new rift_tutorial --sup`. Then add Riffed as a dependency:

```elixir
def deps do
Expand Down Expand Up @@ -50,7 +50,7 @@ service Tutorial {

## 2. Building the Server

Now go ahead and create the file `lib/riffed_tutorial/server.ex`. We'll start with the contents of the file:
Now go ahead and create the file `lib/rift_tutorial/server.ex`. We'll start with the contents of the file:

```elixir
defmodule RiffedTutorial.Server do
Expand Down Expand Up @@ -100,7 +100,7 @@ Lastly, there are two more macros `enumerize_struct` and `enumerize_function` wh

Now that the server is configured, the method calls need to be handled. This step is where you come in; you must build the actual methods that do the server logic. In this example, we'll implement a simple ETS (Erlang Term Storage) for a user database, though typically you would add logic for connecting to your own database. In case you are unfamiliar with ETS, it "allows us to store any Erlang/Elixir term in an in-memory table" (taken from the [Elixir documentation](http://elixir-lang.org/getting-started/mix-otp/ets.html)).

Create a file `lib/riffed_tutorial/handler.ex` with the following contents:
Create a file `lib/rift_tutorial/handler.ex` with the following contents:

```elixir
defmodule RiffedTutorial.Handler do
Expand Down Expand Up @@ -150,7 +150,7 @@ Notice the use of `Models.User.new` inside `register_user`. When creating instan

## 4. Building the Client

The next step is to build a client to connect to our server. The client will be part of the same project, and in fact running on the same host, but for example purposes this is fine. Create the file `lib/riffed_tutorial/client.ex` with the following contents:
The next step is to build a client to connect to our server. The client will be part of the same project, and in fact running on the same host, but for example purposes this is fine. Create the file `lib/rift_tutorial/client.ex` with the following contents:

```elixir
defmodule RiffedTutorial.Client do
Expand Down Expand Up @@ -178,7 +178,7 @@ It's important to notice that we have used `auto_import_structs: false`, since o

### Optional Step

If you really want, you can set `auto_import_structs: false` on both the client and server modules, and define a new file containing the models directly in `lib/riffed_tutorial/models.ex`:
If you really want, you can set `auto_import_structs: false` on both the client and server modules, and define a new file containing the models directly in `lib/rift_tutorial/models.ex`:

```elixir
defmodule RiffedTutorial.Models do
Expand All @@ -198,7 +198,7 @@ If you do this, you can remove the `defenum` and `enumerize_struct` parts of the

## 5. Setting up the Supervision Tree

The final step is to go into `lib/riffed_tutorial.ex` and add your server, client, and handler as children to be supervised:
The final step is to go into `lib/rift_tutorial.ex` and add your server, client, and handler as children to be supervised:

```elixir
children = [
Expand Down
8 changes: 4 additions & 4 deletions test/riffed/integration_test.exs
Expand Up @@ -49,7 +49,7 @@ defmodule IntegrationTest do
error_handler: &ServerWithErrorHandler.on_failure/2

def on_failure(_, _) do
File.write!("riffed_error_test.log", "The client left us in the dust")
File.write!("rift_error_test.log", "The client left us in the dust")
end
end

Expand Down Expand Up @@ -143,13 +143,13 @@ defmodule IntegrationTest do
end

test "Can attach own error handler for when client disconnects" do
refute File.exists?("riffed_error_test.log")
refute File.exists?("rift_error_test.log")
{:ok, client} = ErrorHandlerClient.start_link
ErrorHandlerClient.close(client)
# Sleep for a bit while the server writes to file
:timer.sleep(100)
assert File.read!("riffed_error_test.log") == "The client left us in the dust"
File.rm! "riffed_error_test.log"
assert File.read!("rift_error_test.log") == "The client left us in the dust"
File.rm! "rift_error_test.log"
end

test "Can reconnect client" do
Expand Down

0 comments on commit a1e7d8b

Please sign in to comment.